-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbase.py
More file actions
399 lines (326 loc) · 13.1 KB
/
Copy pathbase.py
File metadata and controls
399 lines (326 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
Base output formatters using the Strategy Pattern.
This module provides:
- OutputFormatter: Abstract base class for all formatters
- TableFormatter: Generic table formatter for watermark-style data
- TextFormatter: Plain text formatter for single values
- ListFormatter: List formatter for newline-separated items
"""
from abc import ABC, abstractmethod
from typing import Dict, List, Optional, Tuple, Union
from tabulate import tabulate
from gnmi_cli_lib.common.types import CommandConfig
from gnmi_cli_lib.utils.sorting import natural_sort_key
class OutputFormatter(ABC):
"""
Abstract base class for output formatters.
This class defines the interface that all formatters must implement.
Each formatter is responsible for converting structured data into
human-readable CLI-style output.
The Strategy Pattern allows different formatting strategies to be
selected at runtime based on the command type.
Example:
>>> class MyFormatter(OutputFormatter):
... def format(self, data, config, table_format="simple"):
... return "Formatted output"
"""
@abstractmethod
def format(
self,
data: Dict,
config: CommandConfig,
table_format: str = "simple",
argument: str = "",
options: Optional[Dict] = None,
) -> str:
"""
Format data according to the command configuration.
Args:
data: The data to format (usually a dict)
config: Command configuration with formatting options
table_format: Tabulate format (simple, grid, plain, etc.)
options: Additional options (e.g., period for counters detailed)
Returns:
Formatted string output
"""
pass
class TableFormatter(OutputFormatter):
"""
Formats data as a table.
This is the primary formatter for watermark commands and other
tabular data. It supports both simple key-value tables and
dynamic column tables.
Simple table example (buffer_pool):
Pool Bytes
-------------------- -------
egress_lossless_pool 12345
ingress_lossless_pool 67890
Dynamic column example (priority-group):
Port PG0 PG1 PG2 PG3
------- ----- ----- ----- -----
Ethernet0 100 101 102 103
"""
def format(
self,
data: Union[Dict, List],
config: CommandConfig,
table_format: str = "simple",
argument: str = "",
options: Optional[Dict] = None,
) -> str:
"""Format data as a table with headers."""
if not data:
return ""
lines = []
# Build title
title = config.title
if title:
lines.append(title)
# Handle list input - convert to table directly
if isinstance(data, list):
table_data, headers = self._build_list_table(data, config)
elif config.dynamic_columns:
table_data, headers = self._build_dynamic_table(data)
else:
table_data = self._build_simple_table(data, config.value_fields)
headers = config.headers
if not table_data:
return ""
# Render table
table_str = tabulate(
table_data,
headers=headers,
tablefmt=table_format,
numalign="right" if config.alignment == "right" else "left",
stralign="right" if config.alignment == "right" else "left",
)
lines.append(table_str)
return "\n".join(lines)
def _build_list_table(self, data: List[Dict], config: CommandConfig) -> Tuple[List[List[str]], List[str]]:
"""
Build table from list of dicts.
Args:
data: List of dicts where each dict is a row
config: CommandConfig for headers and column order
Returns:
Tuple of (table_data, headers)
"""
if not data:
return [], []
# Get headers from config or from first row's keys
if config.headers:
headers = config.headers
else:
# Collect all keys from all rows to ensure we have complete headers
all_keys = set()
for row_dict in data:
if isinstance(row_dict, dict):
all_keys.update(row_dict.keys())
headers = sorted(list(all_keys))
# Build rows
table_data = []
for row_dict in data:
if isinstance(row_dict, dict):
row = [row_dict.get(h, "") for h in headers]
table_data.append(row)
return table_data, headers
def _build_simple_table(self, data: Dict, value_fields: List[str]) -> List[List[str]]:
"""
Build table for simple key-value structures.
Args:
data: Dict mapping keys to value dicts
value_fields: List of field names to extract
Returns:
List of rows, each row is [key, value1, value2, ...]
"""
table_data = []
for key in sorted(data.keys()):
row = [key]
value = data[key]
for field_name in value_fields:
if isinstance(value, dict):
cell_value = value.get(field_name, "N/A")
else:
cell_value = str(value)
row.append(cell_value)
table_data.append(row)
return table_data
def _build_dynamic_table(self, data: Dict) -> Tuple[List[List[str]], List[str]]:
"""
Build table for dynamic column structures.
This is used for priority-group and queue watermarks where
columns are determined by the data keys (PG0, PG1, UC0, UC1, etc.).
Args:
data: Dict mapping ports to column dicts
Returns:
Tuple of (table_data, headers)
"""
# Collect all unique columns from the data
all_columns = set()
for port_data in data.values():
if isinstance(port_data, dict):
all_columns.update(port_data.keys())
# Natural sort for columns (PG0, PG1, ... or UC0, UC1, ...)
sorted_columns = sorted(all_columns, key=natural_sort_key)
headers = ["Port"] + sorted_columns
# Build rows
table_data = []
for port in sorted(data.keys(), key=natural_sort_key):
row = [port]
port_data = data[port]
for col in sorted_columns:
if isinstance(port_data, dict):
cell_value = port_data.get(col, "N/A")
else:
cell_value = "N/A"
row.append(cell_value)
table_data.append(row)
return table_data, headers
class TextFormatter(OutputFormatter):
"""
Formats data as plain text.
This formatter extracts a single value from the data dict
and returns it as a string. Used for commands like "show clock".
Example:
Input: {"date": "Mon Mar 25 20:25:16 UTC 2019"}
Output: Mon Mar 25 20:25:16 UTC 2019
"""
def format(
self,
data: Dict,
config: CommandConfig,
table_format: str = "simple",
argument: str = "",
options: Optional[Dict] = None,
) -> str:
"""Return the value of the specified key field."""
key_field = config.key_field or "date"
return str(data.get(key_field, ""))
class ListFormatter(OutputFormatter):
"""
Formats data as a newline-separated list.
Used for commands that return a list of items, like
"show clock timezones".
Example:
Input: {"timezones": ["America/Anchorage", "UTC", "Universal"]}
Output:
America/Anchorage
UTC
Universal
"""
def format(
self,
data: Dict,
config: CommandConfig,
table_format: str = "simple",
argument: str = "",
options: Optional[Dict] = None,
) -> str:
"""Return items as newline-separated list."""
key_field = config.key_field or "timezones"
items = data.get(key_field, [])
if isinstance(items, list):
return "\n".join(str(item) for item in items)
return str(items)
class CommandHelpFormatter(OutputFormatter):
"""
Formats command help/usage information.
Used for commands that return subcommand information rather than
actual data, like "show queue watermark" (without unicast/multicast/all).
Example:
Input: {"subcommands": {"all": "...", "multicast": "...", "unicast": "..."}}
Output:
Usage: show queue persistent-watermark [OPTIONS] COMMAND [ARGS]...
Show persistent WM for queues
Options:
-?, -h, --help Show this message and exit.
Commands:
all Show persistent WM for all queues
multicast Show persistent WM for multicast queues
unicast Show persistent WM for unicast queues
"""
# Mapping from internal command names to CLI-style names
COMMAND_NAME_MAP = {
# Single-segment intermediates (from device real output)
"processes": "processes",
"buffer_pool": "buffer_pool",
"headroom-pool": "headroom-pool",
"priority-group": "priority-group",
"queue": "queue",
"dropcounters": "dropcounters",
"watermark": "watermark",
"lldp": "lldp",
"vlan": "vlan",
"interfaces": "interfaces",
"srv6": "srv6",
"ipv6": "ipv6",
# Multi-segment intermediates
"queue watermark": "queue watermark",
"queue persistent-watermark": "queue persistent-watermark",
"priority-group watermark": "priority-group watermark",
"priority-group persistent-watermark":
"priority-group persistent-watermark",
"interfaces neighbor": "interfaces neighbor",
"interfaces fec": "interfaces fec",
"interfaces transceiver": "interfaces transceiver",
"interfaces switchport": "interfaces switchport",
"ipv6 bgp": "ipv6 bgp",
}
# Mapping from command type to description text
DESCRIPTION_MAP = {
# Single-segment intermediates — text matches the device's
# "Show <description>" line verbatim.
"processes": "processes information",
"buffer_pool": "details of the buffer pools",
"headroom-pool": "details of headroom pool",
"priority-group": "details of the PGs",
"queue": "details of the queues",
"dropcounters": "drop counter related information",
"watermark": "details of watermark",
"lldp": "LLDP information",
"vlan": "VLAN information",
"interfaces": "details of the network interfaces",
"srv6": "SRv6 related information",
"ipv6": "IPv6 commands",
# Multi-segment intermediates
"queue watermark": "user WM for queues",
"queue persistent-watermark": "persistent WM for queues",
"priority-group watermark": "priority-group user WM",
"priority-group persistent-watermark": "priority-group persistent WM",
"interfaces neighbor": "neighbor related information",
"interfaces fec": "interface fec information",
"interfaces transceiver": "SFP Transceiver information",
"interfaces switchport": "interface switchport information",
"ipv6 bgp": "IPv6 BGP (Border Gateway Protocol) information",
}
def format(
self,
data: Dict,
config: CommandConfig,
table_format: str = "simple",
argument: str = "",
options: Optional[Dict] = None,
) -> str:
"""Format command help output from subcommands data."""
subcommands = data.get("subcommands", {})
if not subcommands:
return "No subcommands available."
# Build a help-style output
lines = []
raw_name = config.description.replace(" help", "") if config.description else "command"
# Map to CLI-style name
command_name = self.COMMAND_NAME_MAP.get(raw_name, raw_name.lower())
description = self.DESCRIPTION_MAP.get(raw_name, raw_name)
lines.append(f"Usage: show {command_name} [OPTIONS] COMMAND [ARGS]...")
lines.append("")
lines.append(f" Show {description}")
lines.append("")
lines.append("Options:")
lines.append(" -?, -h, --help Show this message and exit.")
lines.append("")
lines.append("Commands:")
for subcmd in sorted(subcommands.keys()):
# Build subcmd description
subcmd_desc = f"{description.replace('queues', subcmd + ' queues')}" if 'queues' in description else f"{description} {subcmd}"
lines.append(f" {subcmd:<10} Show {subcmd_desc}")
return "\n".join(lines)