Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize PR #84 and improve --output helptext #92

Merged
merged 6 commits into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 21 additions & 17 deletions synadm/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
from synadm import api


output_format_help = """The 'human' mode gives a tabular or list view depending
on the fetched data, but often needs a lot of horizontal space to display
correctly. 'json' returns formatted json. 'minified' is minified json, suitable
for scripting purposes. 'pprint' shows a formatted output with the help of
Python's built-in pprint module. 'yaml' is a compromise between human- and
machine-readable output, it doesn't need as much terminal width as 'human' does
and is the default on fresh installations."""

JacksonChen666 marked this conversation as resolved.
Show resolved Hide resolved

def humanize(data):
""" Try to display data in a human-readable form:
- Lists of dicts are displayed as tables.
Expand All @@ -50,18 +59,15 @@ def humanize(data):
return str(data)


def json_pretty(data):
return json.dumps(data, indent=4)


class APIHelper:
""" API client enriched with CLI-level functions, used as a proxy to the
client object.
"""

FORMATTERS = {
"pprint": pprint.pformat,
"json": json_pretty,
"json": lambda data: json.dumps(data, indent=4),
"minified": lambda data: json.dumps(data, separators=(",", ":")),
"yaml": yaml.dump,
"human": humanize
}
Expand Down Expand Up @@ -300,10 +306,10 @@ def generate_mxid(self, user_id):
""")
@click.option(
"--output", "-o", default="",
type=click.Choice(["yaml", "json", "human", "pprint",
"y", "j", "h", "p", ""]),
type=click.Choice(["yaml", "json", "minified", "human", "pprint",
"y", "j", "m", "h", "p", ""]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is an empty string an option? That doesn't seem useful

Copy link
Owner Author

@JOJ0 JOJ0 Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was aksing that myself when I went over that code and recall something very very dusty in my brain that there once was a reason. It might be no reason anymore. It needs to be tested thoroughly whether or not in can be removed...if you find the time go ahead, otherwise I'm too afraid to remove it at the moment.

show_choices=True,
help="""Override default output format.""")
help=f"Override default output format. {output_format_help}")
@click.option(
"--config-file", "-c", type=click.Path(),
default="~/.config/synadm.yaml",
Expand Down Expand Up @@ -348,14 +354,11 @@ def root(ctx, verbose, batch, output, config_file):
help="""The time in seconds synadm should wait for responses from admin
API's or Matrix API's. The default is 7 seconds. """)
@click.option(
"--output", "-o", type=click.Choice(["yaml", "json", "human", "pprint"]),
help="""How synadm displays data by default. 'human' gives a tabular or
list view depending on the fetched data. This mode needs your terminal to
be quite wide! 'json' displays exactly as the API responded. 'pprint' shows
nicely formatted json. 'yaml' is the currently recommended output format.
It doesn't need as much terminal width as 'human' does. Note that the
default output format can always be overridden by using global switch -o
(eg 'synadm -o pprint user list').""")
"--output", "-o", type=click.Choice([
"yaml", "json", "minified", "human", "pprint"]),
help=f"""How synadm displays data by default. {output_format_help} The
default output format can always be overridden by using the global
--output/-o switch (eg 'synadm -o pprint user list').""")
@click.option(
"--server-discovery", "-d", type=click.Choice(["well-known", "dns"]),
help="""The method used for discovery of "the own homeserver name". Since
Expand Down Expand Up @@ -446,7 +449,8 @@ def get_redacted_token_prompt(cli_token):
"format": click.prompt(
"Default output format",
default=output if output else helper.config.get("format", output),
type=click.Choice(["yaml", "json", "human", "pprint"])),
type=click.Choice([
"yaml", "json", "minified", "human", "pprint"])),
JacksonChen666 marked this conversation as resolved.
Show resolved Hide resolved
"timeout": click.prompt(
"Default http timeout",
default=timeout if timeout else helper.config.get(
Expand Down