|
7 | 7 | import json
|
8 | 8 | import logging
|
9 | 9 | from typing import Optional
|
| 10 | +import yaml |
10 | 11 |
|
11 | 12 | import transformer_class
|
12 | 13 |
|
@@ -55,18 +56,24 @@ def load_metadata(metadata_path: str) -> dict:
|
55 | 56 | won't contain metadata but will contain an error message under an 'error' key.
|
56 | 57 | """
|
57 | 58 | try:
|
| 59 | + if os.path.splitext(metadata_path)[1] in ('.yml', '.yaml'): |
| 60 | + load_func = yaml.safe_load |
| 61 | + else: |
| 62 | + load_func = json.load |
58 | 63 | with open(metadata_path, 'r') as in_file:
|
59 |
| - md_load = json.load(in_file) |
| 64 | + md_load = load_func(in_file) |
60 | 65 | if md_load is not None:
|
61 | 66 | md_return = {'metadata': md_load}
|
62 | 67 | else:
|
63 |
| - msg = 'Invalid JSON specified in metadata file "%s"' % metadata_path |
| 68 | + msg = 'Invalid JSON/YAML specified in metadata file "%s"' % metadata_path |
64 | 69 | logging.error(msg)
|
65 | 70 | md_return = {'error': msg}
|
66 | 71 | except Exception as ex:
|
67 |
| - msg = 'Unable to load metadata file "%s"' % metadata_path |
| 72 | + msg = "Unable to load metadata file '%s'" % metadata_path |
68 | 73 | logging.error(msg)
|
69 | 74 | logging.error('Exception caught: %s', str(ex))
|
| 75 | + if logging.getLogger().level == logging.DEBUG: |
| 76 | + logging.exception(msg) |
70 | 77 | md_return = {'error': msg}
|
71 | 78 |
|
72 | 79 | return md_return
|
@@ -123,6 +130,24 @@ def check_retrieve_results_error(transformer_retrieve: tuple) -> Optional[dict]:
|
123 | 130 |
|
124 | 131 | return None
|
125 | 132 |
|
| 133 | + @staticmethod |
| 134 | + def check_metadata_needed() -> bool: |
| 135 | + """Checks if metadata is required |
| 136 | + Return: |
| 137 | + Returns True if metadata is required (the default is that it's required), or False if not |
| 138 | + """ |
| 139 | + # Disable the following check since it's not a valid test here (METADATA_NEEDED is an optional variable) |
| 140 | + # pylint: disable=no-member |
| 141 | + |
| 142 | + # If we have a variable defined, check the many ways of determining False |
| 143 | + if hasattr(configuration, "METADATA_NEEDED"): |
| 144 | + if not configuration.METADATA_NEEDED: |
| 145 | + return False |
| 146 | + if isinstance(configuration.METADATA_NEEDED, str): |
| 147 | + if configuration.METADATA_NEEDED.lower().strip() == 'false': |
| 148 | + return False |
| 149 | + return True |
| 150 | + |
126 | 151 | @staticmethod
|
127 | 152 | def load_metadata_files(metadata_files: list) -> dict:
|
128 | 153 | """Loads the specified metadata files
|
@@ -358,7 +383,7 @@ def do_work(parser: argparse.ArgumentParser, **kwargs) -> dict:
|
358 | 383 | logging.getLogger().setLevel(args.debug if args.debug == logging.DEBUG else args.info)
|
359 | 384 |
|
360 | 385 | # Check that we have mandatory metadata
|
361 |
| - if not args.metadata: |
| 386 | + if not args.metadata and __internal__.check_metadata_needed(): |
362 | 387 | result = __internal__.handle_error(-1, "No metadata paths were specified.")
|
363 | 388 | else:
|
364 | 389 | md_results = __internal__.load_metadata_files(args.metadata)
|
|
0 commit comments