Skip to content

Commit

Permalink
Format in a tabular manner
Browse files Browse the repository at this point in the history
  • Loading branch information
arka-pramanik-hpe committed Feb 4, 2025
1 parent 3fd9741 commit 17eecf8
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions cray/modules/rrs/cli.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
#
# MIT License
#
# (C) Copyright 2020-2024 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
""" rrs """
# pylint: disable=invalid-name
from cray.generator import generate
import click
from cray import formatting
import json
from tabulate import tabulate

def print_response(resp):
# Intercept the response by printing it.
# print(resp)
print("I am printing")
# click.echo(resp)
click.echo("I am using click to echo")
# Return the response unchanged (or modify if needed).
return resp
def format_rrs_response(result, **kwargs):
"""
Callback function to format RRS output into a table.
Expects 'result' to be a dict mapping rack names to lists of node dictionaries.
"""
# If result is a requests.Response, try to decode as JSON.
if hasattr(result, "json"):
try:
result = result.json()
print(result)
except Exception:
result = result.text

# def _rrs_format_response(cb):
# Ensure result is a dict.
if not isinstance(result, dict):
raise ValueError("Expected a dict as the response for RRS zones list")

# def _cb(ctx, param, value):
# data = value.read()
# print(data)
# print("I am printing")
# # click.echo(data)
# # click.echo("I am using click to echo")
# if cb:
# return cb(ctx, param, data)
# return data
output_lines = []
# Define the order and headers for columns
headers = ["Name", "Age", "CPU", "Memory", "Roles", "Status", "Version"]

# Iterate over each rack and its node list.
for rack, nodes in result.items():
output_lines.append(f"Rack: {rack}")
table = []
for node in nodes:
# Create a row; use get() so missing keys don’t crash
row = [
node.get("name", ""),
node.get("age", ""),
node.get("cpu", ""),
node.get("memory", ""),
node.get("roles", ""),
node.get("status", ""),
node.get("version", ""),
]
table.append(row)
# Format the table (using tabulate for pretty printing)
output_lines.append(tabulate(table, headers=headers, tablefmt="grid"))
output_lines.append("") # blank line between racks
# Join all lines and return
return "\n".join(output_lines)

# return _cb


# Pass the callback to generate
cli = generate(__file__, callback=print_response)
# Pass the callback to generate()
cli = generate(__file__, callback=format_rrs_response)

0 comments on commit 17eecf8

Please sign in to comment.