Skip to content

Commit

Permalink
Revert "Make output format configurable with --output flag (#31)"
Browse files Browse the repository at this point in the history
This reverts commit 52ef3af.
  • Loading branch information
plaffitt committed Jan 9, 2025
1 parent fc077c1 commit ac7d7c5
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 93 deletions.
9 changes: 0 additions & 9 deletions src/pvecontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pvecontrol import actions, node, vm, task, storage
from pvecontrol.cluster import PVECluster
from pvecontrol.config import set_config
from pvecontrol.utils import OutputFormats


def action_test(proxmox, _args):
Expand Down Expand Up @@ -76,14 +75,6 @@ def _parser():
parser = argparse.ArgumentParser(description="Proxmox VE control cli.", epilog="Made with love by Enix.io")
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("--debug", action="store_true")
parser.add_argument(
"-o",
"--output",
action="store",
type=OutputFormats,
default=OutputFormats.TEXT,
choices=list(OutputFormats),
)
parser.add_argument(
"-c", "--cluster", action="store", required=True, help="Proxmox cluster name as defined in configuration"
)
Expand Down
4 changes: 2 additions & 2 deletions src/pvecontrol/actions/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from pvecontrol.node import NodeStatus
from pvecontrol.vm import VmStatus
from pvecontrol.utils import print_output, print_task
from pvecontrol.utils import print_tableoutput, print_task


def action_nodelist(proxmox, args):
"""List proxmox nodes in the cluster using proxmoxer api"""
print_output(proxmox.nodes, columns=args.columns, sortby=args.sort_by, filters=args.filter, output=args.output)
print_tableoutput(proxmox.nodes, columns=args.columns, sortby=args.sort_by, filters=args.filter)


# pylint: disable=too-many-branches,too-many-statements
Expand Down
4 changes: 2 additions & 2 deletions src/pvecontrol/actions/storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pvecontrol.storage import StorageShared, COLUMNS
from pvecontrol.utils import print_output
from pvecontrol.utils import print_tableoutput


def action_storagelist(proxmox, args):
Expand All @@ -19,4 +19,4 @@ def action_storagelist(proxmox, args):
for _id, storage in storages.items():
storage["nodes"] = ", ".join(storage["nodes"])

print_output(storages.values(), COLUMNS, sortby=args.sort_by, filters=args.filter, output=args.output)
print_tableoutput(storages.values(), COLUMNS, sortby=args.sort_by, filters=args.filter)
5 changes: 2 additions & 3 deletions src/pvecontrol/actions/task.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from pvecontrol.utils import print_task, print_output
from pvecontrol.utils import print_task, print_tableoutput


def action_tasklist(proxmox, args):
print_output(
print_tableoutput(
proxmox.tasks,
columns=args.columns,
sortby=args.sort_by,
filters=args.filter,
output=args.output,
)


Expand Down
4 changes: 2 additions & 2 deletions src/pvecontrol/actions/vm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import sys

from pvecontrol.utils import print_task, print_output
from pvecontrol.utils import print_task, print_tableoutput


def _get_vm(proxmox, vmid):
Expand Down Expand Up @@ -64,4 +64,4 @@ def action_vmmigrate(proxmox, args):
def action_vmlist(proxmox, args):
"""List VMs in the Proxmox Cluster"""
vms = proxmox.vms()
print_output(vms, columns=args.columns, sortby=args.sort_by, filters=args.filter, output=args.output)
print_tableoutput(vms, columns=args.columns, sortby=args.sort_by, filters=args.filter)
55 changes: 9 additions & 46 deletions src/pvecontrol/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import sys
import re
import curses
import json

from collections import OrderedDict
from enum import Enum

import yaml

from humanize import naturalsize
from prettytable import PrettyTable

Expand All @@ -24,16 +20,6 @@ class Fonts:
END = "\033[0m"


class OutputFormats(Enum):
TEXT = "text"
JSON = "json"
CSV = "csv"
YAML = "yaml"

def __str__(self):
return self.value


def terminal_support_colors():
try:
_stdscr = curses.initscr()
Expand Down Expand Up @@ -62,7 +48,9 @@ def teminal_support_utf_8():
]


def render_output(table, columns=None, sortby=None, filters=None, output=OutputFormats.TEXT):
# Pretty output a table from a table of dicts
# We assume all dicts have the same keys and are sorted by key
def print_tableoutput(table, columns=None, sortby=None, filters=None):
if not columns:
columns = []
if not filters:
Expand All @@ -73,38 +61,17 @@ def render_output(table, columns=None, sortby=None, filters=None, output=OutputF
else:
table = [filter_keys(n.__dict__ if hasattr(n, "__dict__") else n, columns) for n in table]

x = prepare_prettytable(table, sortby, filters)

if sortby is not None:
sortby = "sortby"

if output == OutputFormats.TEXT:
return x.get_string(sortby=sortby, fields=columns)
if output == OutputFormats.CSV:
return x.get_csv_string(sortby=sortby, fields=columns)
if output in (OutputFormats.JSON, OutputFormats.YAML):
json_string = x.get_json_string(sortby=sortby, fields=columns)
data = json.loads(json_string)[1:]
if output == OutputFormats.JSON:
return json.dumps(data)
return yaml.dump(data)

return None


def prepare_prettytable(table, sortby, filters):
do_sort = sortby is not None
do_sort = not sortby is None

x = PrettyTable()
x.align = "l"
x.field_names = [*table[0].keys(), "sortby"] if do_sort else table[0].keys()

for line in table:
for key in line:
if isinstance(line[key], Enum):
line[key] = str(line[key])
if do_sort:
line["sortby"] = line[sortby]
if isinstance(line[sortby], Enum):
line["sortby"] = str(line[sortby])
for key in NATURALSIZE_KEYS:
if key in line:
line[key] = naturalsize(line[key], binary=True)
Expand All @@ -116,11 +83,7 @@ def prepare_prettytable(table, sortby, filters):
for line in table:
x.add_row(line.values())

return x


def print_output(table, columns=None, sortby=None, filters=None, output=OutputFormats.TEXT):
print(render_output(table, columns, sortby, filters, output))
print(x.get_string(sortby="sortby" if do_sort else None, fields=columns))


def filter_keys(input_d, keys):
Expand All @@ -141,7 +104,7 @@ def print_taskstatus(task):
"user",
"starttime",
]
print_output([task], columns)
print_tableoutput([task], columns)


def print_task(proxmox, upid, follow=False, wait=False):
Expand Down Expand Up @@ -179,6 +142,6 @@ def print_task(proxmox, upid, follow=False, wait=False):
time.sleep(1)
print("")
elif not wait:
print_output([{"log output": task.decode_log()}])
print_tableoutput([{"log output": task.decode_log()}])

print_taskstatus(task)
29 changes: 0 additions & 29 deletions src/tests/test_utils.py

This file was deleted.

0 comments on commit ac7d7c5

Please sign in to comment.