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