diff options
-rw-r--r-- | buildstream/__init__.py | 2 | ||||
-rw-r--r-- | buildstream/context.py | 33 | ||||
-rw-r--r-- | buildstream/exceptions.py | 8 | ||||
-rw-r--r-- | buildstream/utils.py | 27 |
4 files changed, 37 insertions, 33 deletions
diff --git a/buildstream/__init__.py b/buildstream/__init__.py index 392157e6f..d5def99ae 100644 --- a/buildstream/__init__.py +++ b/buildstream/__init__.py @@ -19,7 +19,7 @@ # Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> # Exceptions first -from .exceptions import PluginError, ContextError +from .exceptions import PluginError, LoadError # Common stuff from .utils import dictionary_override diff --git a/buildstream/context.py b/buildstream/context.py index da0a29422..6d9f773c1 100644 --- a/buildstream/context.py +++ b/buildstream/context.py @@ -18,12 +18,8 @@ # Authors: # Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> -import ruamel.yaml -from ruamel.yaml.scanner import ScannerError - from ._site import _site_info -from .utils import dictionary_override -from .exceptions import ContextError +from .utils import dictionary_override, load_yaml_dict class Context(): """Context of how BuildStream was invoked @@ -63,21 +59,18 @@ class Context(): config (filename): The user specified configuration file, if any Raises: - :class:`.ContextError` + :class:`.LoadError` This will first load the BuildStream default configuration and then - override that configuration with a user provided configuration file. - - If the config parameter is specified the user configuration will be - loaded from that file. Otherwise an attempt to read a file at - ``$XDG_CONFIG_HOME/buildstream.yaml`` will be made. + override that configuration with the configuration file indicated + by *config*, if any was specified. """ # Load default config # - defaults = self._load_config(_site_info['default_config']) + defaults = load_yaml_dict(_site_info['default_config']) if config: - user_config = self._load_config(config) + user_config = load_yaml_dict(config) defaults = dictionary_override(defaults, user_config) # Should have a loop here, but we suck @@ -87,17 +80,3 @@ class Context(): self.deploydir = defaults.get('deploydir') self.artifactdir = defaults.get('artifactdir') self.ccachedir = defaults.get('ccachedir') - - def _load_config(self, filename): - try: - with open(filename) as f: - contents = ruamel.yaml.safe_load(f) - except FileNotFoundError as e: - raise ContextError("Could not find configuration file at %s" % filename) from e - except ScannerError as e: - raise ContextError("Problem loading malformed configuration file:\n\n%s\n\n%s\n" % (e.problem, e.problem_mark)) from e - - if not isinstance(contents, dict): - raise ContextError("Loading configuration file did not specify a dictionary: %s" % filename) - - return contents diff --git a/buildstream/exceptions.py b/buildstream/exceptions.py index 45ce2168c..c95bc4208 100644 --- a/buildstream/exceptions.py +++ b/buildstream/exceptions.py @@ -27,11 +27,9 @@ class PluginError(Exception): """ pass -class ContextError(Exception): - """Raised while interpreting invocation context. +class LoadError(Exception): + """Raised while loading some YAML. - This exception is raised when creating the :class:`.Context` - when the user configuration is missing or malformed or if some - of the context is nonsensical + This exception is raised when loading or parsing YAML. """ pass diff --git a/buildstream/utils.py b/buildstream/utils.py index 3cba2f967..94800f697 100644 --- a/buildstream/utils.py +++ b/buildstream/utils.py @@ -20,6 +20,10 @@ import collections import copy +import ruamel.yaml +from ruamel.yaml.scanner import ScannerError + +from .exceptions import LoadError def dictionary_override(dictionary, override): """Overrides values in *dictionary* with values from *override* @@ -48,3 +52,26 @@ def dictionary_override(dictionary, override): result[k] = override[k] return result + + +def load_yaml_dict(filename): + """Loads a dictionary from some YAML + + Args: + filename (str): The YAML file to load + + Raises: + :class:`.LoadError` + """ + try: + with open(filename) as f: + contents = ruamel.yaml.safe_load(f) + except FileNotFoundError as e: + raise LoadError("Could not find file at %s" % filename) from e + except ScannerError as e: + raise LoadError("Malformed YAML:\n\n%s\n\n%s\n" % (e.problem, e.problem_mark)) from e + + if not isinstance(contents, dict): + raise LoadError("Loading YAML file did not specify a dictionary: %s" % filename) + + return contents |