diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-09-04 14:32:46 -0400 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-09-04 14:33:52 -0400 |
commit | 841339371c26da4b44fb6d0e3ee06e7a9fe9d254 (patch) | |
tree | 2d0591c63d0c8734832152b827997d7ae738a031 | |
parent | 32ea8e4f305d5968f50df59798e6eb0342b05bc2 (diff) | |
download | buildstream-841339371c26da4b44fb6d0e3ee06e7a9fe9d254.tar.gz |
_yaml.py: Added load_data()
Shared with _yaml.load(), but does not require a file on disk.
-rw-r--r-- | buildstream/_yaml.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py index 2a1073fa9..527f5371c 100644 --- a/buildstream/_yaml.py +++ b/buildstream/_yaml.py @@ -197,13 +197,23 @@ class CompositePolicy(Enum): # Raises: LoadError # def load(filename, shortname=None, copy_tree=False): + if not shortname: + shortname = filename try: with open(filename) as f: - contents = yaml.load(f, yaml.loader.RoundTripLoader) + return load_data(f, shortname=shortname, copy_tree=copy_tree) except FileNotFoundError as e: raise LoadError(LoadErrorReason.MISSING_FILE, "Could not find file at %s" % filename) from e + + +# Like load(), but doesnt require the data to be in a file +# +def load_data(data, shortname=None, copy_tree=False): + + try: + contents = yaml.load(data, yaml.loader.RoundTripLoader) except (yaml.scanner.ScannerError, yaml.composer.ComposerError, yaml.parser.ParserError) as e: raise LoadError(LoadErrorReason.INVALID_YAML, "Malformed YAML:\n\n%s\n\n%s\n" % (e.problem, e.problem_mark)) from e @@ -215,10 +225,7 @@ def load(filename, shortname=None, copy_tree=False): else: raise LoadError(LoadErrorReason.INVALID_YAML, "YAML file has content of type '%s' instead of expected type 'dict': %s" % - (type(contents).__name__, filename)) - - if not shortname: - shortname = filename + (type(contents).__name__, shortname)) return node_decorated_copy(shortname, contents, copy_tree=copy_tree) |