From 803e802c18bb333f964ea94bb2d3c213ccb9d229 Mon Sep 17 00:00:00 2001 From: "Colin B. Macdonald" Date: Sat, 25 Feb 2023 10:46:06 -0800 Subject: [PATCH] Refactor yaml --- req2flatpak.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/req2flatpak.py b/req2flatpak.py index f05941c..0c299c2 100755 --- a/req2flatpak.py +++ b/req2flatpak.py @@ -728,16 +728,6 @@ def main(): parser = cli_parser() options = parser.parse_args() - def _get_yaml_module_or_exit(): - try: - # optional dependency, not imported at top - import yaml - except ImportError: - parser.error( - "Outputing YAML requires 'pyyaml' package: try 'pip install pyyaml'" - ) - return yaml - # stream output to a file or to stdout if hasattr(options.outfile, "write"): output_stream = options.outfile @@ -746,17 +736,29 @@ def _get_yaml_module_or_exit(): else: output_stream = sys.stdout + if options.yaml: + try: + # optional dependency, not imported at top + import yaml + except ImportError: + parser.error( + "Outputing YAML requires 'pyyaml' package: try 'pip install pyyaml'" + ) + # print platform info if requested, and exit - if options.platform_info and options.yaml: - yaml = _get_yaml_module_or_exit() - yaml.dump( - asdict(PlatformFactory.from_current_interpreter()), output_stream, indent=2 - ) - parser.exit() if options.platform_info: - json.dump( - asdict(PlatformFactory.from_current_interpreter()), output_stream, indent=4 - ) + if options.yaml: + yaml.dump( + asdict(PlatformFactory.from_current_interpreter()), + output_stream, + indent=2, + ) + else: + json.dump( + asdict(PlatformFactory.from_current_interpreter()), + output_stream, + indent=4, + ) parser.exit() # print installed packages if requested, and exit @@ -816,7 +818,6 @@ def _get_yaml_module_or_exit(): build_module = FlatpakGenerator.build_module(requirements, downloads) if options.yaml: - yaml = _get_yaml_module_or_exit() yaml.dump(build_module, output_stream, indent=2) parser.exit()