refactor table variant handling

This commit is contained in:
Stefan Bühler 2023-04-28 19:18:28 +02:00
parent c412af3de0
commit dbaf301911

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import argparse import argparse
import csv import csv
import dataclasses import dataclasses
import enum
import subprocess import subprocess
import sys import sys
import typing import typing
@ -12,6 +13,11 @@ from ldaptool._utils import argclasses
from ldaptool._utils.ldap import Result, SizeLimitExceeded from ldaptool._utils.ldap import Result, SizeLimitExceeded
class TableOutput(enum.StrEnum):
MARKDOWN = "markdown"
CSV = "csv"
@dataclasses.dataclass(slots=True, kw_only=True) @dataclasses.dataclass(slots=True, kw_only=True)
class Arguments(search.Arguments): class Arguments(search.Arguments):
raw: bool = dataclasses.field( raw: bool = dataclasses.field(
@ -28,6 +34,7 @@ class Arguments(search.Arguments):
help="Markdown table output - requires list of attributes", help="Markdown table output - requires list of attributes",
), ),
) )
table_output: typing.Optional[TableOutput] = None
sort: bool = dataclasses.field( sort: bool = dataclasses.field(
default=False, default=False,
metadata=argclasses.arg( metadata=argclasses.arg(
@ -38,29 +45,29 @@ class Arguments(search.Arguments):
def __post_init__(self) -> None: def __post_init__(self) -> None:
super(Arguments, self).__post_init__() # super() not working here, unclear why. super(Arguments, self).__post_init__() # super() not working here, unclear why.
# can't print both csv and markdown # pick at most one in csv, (markdown) table
if self.csv and self.table: if [self.csv, self.table].count(True) > 1:
raise SystemExit("Can't use both --table and --csv") raise SystemExit("Can't use more than one table output type")
if self.sort:
if not self.table and not self.csv:
# default to markdown table
self.table = True
if self.table:
# markdown requires underlying csv
self.csv = True
if self.csv: if self.csv:
self.table_output = TableOutput.CSV
elif self.table:
self.table_output = TableOutput.MARKDOWN
if self.sort and self.table_output is None:
# default to markdown table
self.table_output = TableOutput.MARKDOWN
if self.table_output:
if not self.columns: if not self.columns:
raise SystemExit("Table output requires attributes") raise SystemExit("Table output requires attributes")
if self.json: if self.json:
raise SystemExit("Can't use both --table / --csv / --sort and --json") raise SystemExit("Can't use both table output and --json")
if self.human: if self.human:
raise SystemExit("Can't use both --table / --csv / --sort and --human") raise SystemExit("Can't use both table output and --human")
if self.raw: if self.raw:
if self.csv: if self.table_output:
raise SystemExit("Table output requires decode; --raw not allowed") raise SystemExit("Table output requires decode; --raw not allowed")
if self.json or self.human: if self.json or self.human:
raise SystemExit("Decode options require decode; --raw not allowed") raise SystemExit("Decode options require decode; --raw not allowed")
@ -96,9 +103,9 @@ class _Context:
output = proc.stdin output = proc.stdin
try: try:
if self.arguments.table: if self.arguments.table_output == TableOutput.MARKDOWN:
add_filter(["csvlook"]) add_filter(["csvlook"])
if self.arguments.csv: if self.arguments.table_output:
self._table_output(search_iterator, stream=output) self._table_output(search_iterator, stream=output)
else: else:
self._ldif_or_json_output(search_iterator, stream=output) self._ldif_or_json_output(search_iterator, stream=output)