Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions gnmi_cli_converter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
GNMI CLI Converter

A framework for converting GNMI JSON to CLI format output.

Main interfaces:
- CLIConverter: Main converter class
- CommandInfo: Command information dataclass

Usage example:
>>> from gnmi_cli_converter import CLIConverter
>>> converter = CLIConverter()
>>> json_data = {
... "egress_lossless_pool": {"Bytes": "12345"},
... "ingress_lossless_pool": {"Bytes": "67890"}
... }
>>> output = converter.convert(["buffer_pool", "watermark"], json_data)
>>> print(output)
Shared pool maximum occupancy:
Pool Bytes
--------------------- -----
egress_lossless_pool 12345
ingress_lossless_pool 67890
"""

# Import commands module to trigger all command registrations
from . import commands

# Export main interfaces
from .converter import CLIConverter, CommandInfo
from .exceptions import (
CLIConverterError,
PathNotFoundError,
InvalidJSONError,
RenderError,
MissingFieldError
)

__all__ = [
'CLIConverter',
'CommandInfo',
'CLIConverterError',
'PathNotFoundError',
'InvalidJSONError',
'RenderError',
'MissingFieldError',
]

__version__ = '1.0.0'
16 changes: 16 additions & 0 deletions gnmi_cli_converter/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Commands Module

Import all command modules to trigger @register decorator registration.
"""

# Import all command modules to trigger @register decorators
from . import buffer_pool
from . import uptime

# More command modules can be added in the future:
# from . import interfaces
# from . import queue
# from . import ipv6
# from . import processes
# ...
82 changes: 82 additions & 0 deletions gnmi_cli_converter/commands/buffer_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
Buffer Pool Related Command Render Functions

Contains:
- show buffer_pool watermark
- show buffer_pool persistent-watermark
"""

from typing import Any, List, Dict
from natsort import natsorted
from ..utils import tabulate_dict
from ..registry import register


@register(["buffer_pool", "watermark"])
def render_buffer_pool_watermark(json_data: Any, path_elems: List[str], options: Dict[str, Any]) -> str:
"""
show buffer_pool watermark

Args:
json_data: JSON data returned by GNMI
path_elems: Path element array (not used by this command)
options: Options dict (not used by this command)

Input JSON example:
{
"egress_lossless_pool": {"Bytes": "12345"},
"egress_lossy_pool": {"Bytes": "67890"},
"ingress_lossless_pool": {"Bytes": "24680"}
}

Output example:
Shared pool maximum occupancy:
Pool Bytes
--------------------- -----
egress_lossless_pool 12345
egress_lossy_pool 67890
ingress_lossless_pool 24680
"""
if not json_data:
return "Shared pool maximum occupancy:\nNo data available"

# Use natsorted for natural sorting
rows = [
(pool, data.get("Bytes", "N/A"))
for pool, data in natsorted(json_data.items())
]

return tabulate_dict(
title="Shared pool maximum occupancy:",
headers=["Pool", "Bytes"],
rows=rows,
stralign="right"
)


@register(["buffer_pool", "persistent-watermark"])
def render_buffer_pool_persistent_watermark(json_data: Any, path_elems: List[str], options: Dict[str, Any]) -> str:
"""
show buffer_pool persistent-watermark

Format is the same as watermark, only the data source is different.

Args:
json_data: JSON data returned by GNMI
path_elems: Path element array (not used by this command)
options: Options dict (not used by this command)
"""
if not json_data:
return "Shared pool maximum occupancy:\nNo data available"

rows = [
(pool, data.get("Bytes", "N/A"))
for pool, data in natsorted(json_data.items())
]

return tabulate_dict(
title="Shared pool maximum occupancy:",
headers=["Pool", "Bytes"],
rows=rows,
stralign="right"
)
29 changes: 29 additions & 0 deletions gnmi_cli_converter/commands/uptime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Uptime Command Render Function

Contains:
- show uptime
"""

from typing import Any, List, Dict
from ..utils import passthrough
from ..registry import register


@register(["uptime"])
def render_uptime(json_data: Any, path_elems: List[str], options: Dict[str, Any]) -> str:
"""
show uptime

Args:
json_data: JSON data returned by GNMI
path_elems: Path element array (not used by this command)
options: Options dict (not used by this command)

Input JSON example:
{"uptime": "up 3 weeks, 4 days, 10 hours, 15 minutes"}

Output example:
up 3 weeks, 4 days, 10 hours, 15 minutes
"""
return passthrough(json_data, key="uptime")
Loading
Loading