Skip to content

Commit

Permalink
Refactor yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
cbm755 committed Feb 25, 2023
1 parent db2918c commit 2aa87c4
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions req2flatpak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -746,17 +736,22 @@ 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
)
info = asdict(PlatformFactory.from_current_interpreter())
if options.yaml:
yaml.dump(info, output_stream, indent=2)
else:
json.dump(info, output_stream, indent=4)
parser.exit()

# print installed packages if requested, and exit
Expand Down Expand Up @@ -816,7 +811,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()

Expand Down

0 comments on commit 2aa87c4

Please sign in to comment.