diff options
author | Kevin <apainintheneck@gmail.com> | 2022-09-24 12:46:49 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-24 12:46:49 -0700 |
commit | 2df7acf4880ddb8a8b300a5aaeea08705d265b2f (patch) | |
tree | bee6641a1e876c67003b03331314f0c6e51f103c /jrnl | |
parent | 057f31407a97c8b6e17df083905b949d50cedd29 (diff) |
Add machine readable --list output (#1592)
* Add machine readable --list output
* Refactor list_journals()
Create three new methods for each journal list format.
- JSON, YAML, STDOUT
* Added journal list export test
* Update test regex to handle windows filepaths
Diffstat (limited to 'jrnl')
-rw-r--r-- | jrnl/args.py | 8 | ||||
-rw-r--r-- | jrnl/commands.py | 4 | ||||
-rw-r--r-- | jrnl/output.py | 43 |
3 files changed, 46 insertions, 9 deletions
diff --git a/jrnl/args.py b/jrnl/args.py index 4b2ba8ce..c6b0f1a0 100644 --- a/jrnl/args.py +++ b/jrnl/args.py @@ -85,7 +85,13 @@ def parse_args(args=[]): action="store_const", const=postconfig_list, dest="postconfig_cmd", - help="List all configured journals", + help=""" + List all configured journals. + + Optional parameters: + + --format [json or yaml] + """, ) standalone.add_argument( "--ls", diff --git a/jrnl/commands.py b/jrnl/commands.py index a96a2956..6100422b 100644 --- a/jrnl/commands.py +++ b/jrnl/commands.py @@ -56,10 +56,10 @@ def preconfig_version(_): print(output) -def postconfig_list(config, **kwargs): +def postconfig_list(args, config, **kwargs): from jrnl.output import list_journals - print(list_journals(config)) + print(list_journals(config, args.export)) def postconfig_import(args, config, **kwargs): diff --git a/jrnl/output.py b/jrnl/output.py index b82ec122..2db0362e 100644 --- a/jrnl/output.py +++ b/jrnl/output.py @@ -25,19 +25,50 @@ def deprecated_cmd(old_cmd, new_cmd, callback=None, **kwargs): callback(**kwargs) -def list_journals(configuration): - from jrnl import config +def journal_list_to_json(journal_list): + import json - """List the journals specified in the configuration file""" - result = f"Journals defined in config ({config.get_config_path()})\n" - ml = min(max(len(k) for k in configuration["journals"]), 20) - for journal, cfg in configuration["journals"].items(): + return json.dumps(journal_list) + + +def journal_list_to_yaml(journal_list): + from io import StringIO + + from ruamel.yaml import YAML + + output = StringIO() + YAML().dump(journal_list, output) + return output.getvalue() + + +def journal_list_to_stdout(journal_list): + result = f"Journals defined in config ({journal_list['config_path']})\n" + ml = min(max(len(k) for k in journal_list["journals"]), 20) + for journal, cfg in journal_list["journals"].items(): result += " * {:{}} -> {}\n".format( journal, ml, cfg["journal"] if isinstance(cfg, dict) else cfg ) return result +def list_journals(configuration, format=None): + from jrnl import config + + """List the journals specified in the configuration file""" + + journal_list = { + "config_path": config.get_config_path(), + "journals": configuration["journals"], + } + + if format == "json": + return journal_list_to_json(journal_list) + elif format == "yaml": + return journal_list_to_yaml(journal_list) + else: + return journal_list_to_stdout(journal_list) + + def print_msg(msg: Message, **kwargs) -> Union[None, str]: """Helper function to print a single message""" kwargs["style"] = msg.style |