From e62347655a5445a621701b6e13c7abd813d954c9 Mon Sep 17 00:00:00 2001 From: Benjamin Schubert Date: Mon, 10 Jun 2019 07:51:57 +0100 Subject: _yaml: Add 'as_str()' on ScalarNode and 'get_scalar()' on MappingNode - 'get_scalar()' allows retrieving a scalar node from a mapping. - 'as_str()' casts a ScalarNode into a string (thus removing the node information). Both together, those replace 'node_get(mapping, key, type=str)' but also allow retrieving the 'Node' itself, which will allow in the future lazier provenance computation. --- src/buildstream/_cas/casremote.py | 10 +++---- src/buildstream/_context.py | 10 +++---- src/buildstream/_frontend/cli.py | 2 +- src/buildstream/_gitsourcebase.py | 16 +++++----- src/buildstream/_loader/loader.py | 10 +++---- src/buildstream/_loader/types.pyx | 6 ++-- src/buildstream/_options/option.py | 5 ++-- src/buildstream/_options/optionbool.py | 2 +- src/buildstream/_options/optionenum.py | 4 +-- src/buildstream/_options/optionpool.py | 4 +-- src/buildstream/_plugincontext.py | 10 +++---- src/buildstream/_project.py | 34 +++++++++++----------- src/buildstream/_variables.pyx | 2 +- src/buildstream/_workspaces.py | 4 +-- src/buildstream/_yaml.pxd | 6 ++++ src/buildstream/_yaml.pyx | 27 +++++++++++++++++ src/buildstream/element.py | 6 ++-- src/buildstream/plugins/elements/junction.py | 4 +-- .../plugins/sources/_downloadablefilesource.py | 8 ++--- src/buildstream/plugins/sources/bzr.py | 6 ++-- src/buildstream/plugins/sources/deb.py | 2 +- src/buildstream/plugins/sources/pip.py | 4 +-- src/buildstream/plugins/sources/remote.py | 2 +- src/buildstream/plugins/sources/tar.py | 3 +- src/buildstream/plugins/sources/zip.py | 3 +- tests/elements/filter.py | 10 +++---- tests/format/include.py | 20 ++++++------- tests/format/include_composition.py | 4 +-- tests/format/optionarch.py | 2 +- tests/format/optionbool.py | 4 +-- tests/format/optioneltmask.py | 4 +-- tests/format/optionenum.py | 4 +-- tests/format/optionexports.py | 2 +- tests/format/optionflags.py | 4 +-- tests/format/optionos.py | 2 +- tests/format/optionoverrides.py | 2 +- tests/format/options.py | 12 ++++---- tests/format/project.py | 10 +++---- tests/format/variables.py | 4 +-- tests/frontend/cross_junction_workspace.py | 2 +- tests/frontend/init.py | 22 +++++++------- tests/frontend/project/sources/fetch_source.py | 2 +- tests/frontend/workspace.py | 8 ++--- tests/internals/yaml.py | 26 ++++++++--------- tests/sources/git.py | 10 +++---- .../plugins/sources/foo_transform.py | 2 +- 46 files changed, 189 insertions(+), 157 deletions(-) diff --git a/src/buildstream/_cas/casremote.py b/src/buildstream/_cas/casremote.py index cd46e9c38..0195e217a 100644 --- a/src/buildstream/_cas/casremote.py +++ b/src/buildstream/_cas/casremote.py @@ -32,24 +32,24 @@ class CASRemoteSpec(namedtuple('CASRemoteSpec', 'url push server_cert client_key @staticmethod def _new_from_config_node(spec_node, basedir=None): _yaml.node_validate(spec_node, ['url', 'push', 'server-cert', 'client-key', 'client-cert', 'instance-name']) - url = _yaml.node_get(spec_node, str, 'url') + url = spec_node.get_str('url') push = _yaml.node_get(spec_node, bool, 'push', default_value=False) if not url: provenance = _yaml.node_get_provenance(spec_node, 'url') raise LoadError(LoadErrorReason.INVALID_DATA, "{}: empty artifact cache URL".format(provenance)) - instance_name = _yaml.node_get(spec_node, str, 'instance-name', default_value=None) + instance_name = spec_node.get_str('instance-name', default=None) - server_cert = _yaml.node_get(spec_node, str, 'server-cert', default_value=None) + server_cert = spec_node.get_str('server-cert', default=None) if server_cert and basedir: server_cert = os.path.join(basedir, server_cert) - client_key = _yaml.node_get(spec_node, str, 'client-key', default_value=None) + client_key = spec_node.get_str('client-key', default=None) if client_key and basedir: client_key = os.path.join(basedir, client_key) - client_cert = _yaml.node_get(spec_node, str, 'client-cert', default_value=None) + client_cert = spec_node.get_str('client-cert', default=None) if client_cert and basedir: client_cert = os.path.join(basedir, client_cert) diff --git a/src/buildstream/_context.py b/src/buildstream/_context.py index c44962d7e..e285f6b6a 100644 --- a/src/buildstream/_context.py +++ b/src/buildstream/_context.py @@ -222,7 +222,7 @@ class Context(): # Allow the ~ tilde expansion and any environment variables in # path specification in the config files. # - path = _yaml.node_get(defaults, str, directory) + path = defaults.get_str(directory) path = os.path.expanduser(path) path = os.path.expandvars(path) path = os.path.normpath(path) @@ -254,7 +254,7 @@ class Context(): cache = defaults.get_mapping('cache') _yaml.node_validate(cache, ['quota', 'pull-buildtrees', 'cache-buildtrees']) - self.config_cache_quota_string = _yaml.node_get(cache, str, 'quota') + self.config_cache_quota_string = cache.get_str('quota') try: self.config_cache_quota = utils._parse_size(self.config_cache_quota_string, self.casdir) @@ -291,8 +291,8 @@ class Context(): self.log_verbose = _yaml.node_get(logging, bool, 'verbose') self.log_error_lines = _yaml.node_get(logging, int, 'error-lines') self.log_message_lines = _yaml.node_get(logging, int, 'message-lines') - self.log_element_format = _yaml.node_get(logging, str, 'element-format') - self.log_message_format = _yaml.node_get(logging, str, 'message-format') + self.log_element_format = logging.get_str('element-format') + self.log_message_format = logging.get_str('message-format') # Load scheduler config scheduler = defaults.get_mapping('scheduler') @@ -756,7 +756,7 @@ class Context(): # LoadError, when the value is not of the expected type, or is not found. # def _node_get_option_str(node, key, allowed_options): - result = _yaml.node_get(node, str, key) + result = node.get_str(key) if result not in allowed_options: provenance = _yaml.node_get_provenance(node, key) raise LoadError(LoadErrorReason.INVALID_DATA, diff --git a/src/buildstream/_frontend/cli.py b/src/buildstream/_frontend/cli.py index 53adb188c..f2f149403 100644 --- a/src/buildstream/_frontend/cli.py +++ b/src/buildstream/_frontend/cli.py @@ -100,7 +100,7 @@ def complete_target(args, incomplete): return [] # The project is not required to have an element-path - element_directory = _yaml.node_get(project, str, 'element-path', default_value='') + element_directory = project.get_str('element-path', default='') # If a project was loaded, use its element-path to # adjust our completion's base directory diff --git a/src/buildstream/_gitsourcebase.py b/src/buildstream/_gitsourcebase.py index 8c6b54e2d..eefa36b1d 100644 --- a/src/buildstream/_gitsourcebase.py +++ b/src/buildstream/_gitsourcebase.py @@ -376,7 +376,7 @@ class _GitSourceBase(Source): BST_MIRROR_CLASS = _GitMirror def configure(self, node): - ref = self.node_get_member(node, str, 'ref', None) + ref = node.get_str('ref', None) config_keys = ['url', 'track', 'ref', 'submodules', 'checkout-submodules', 'ref-format', @@ -390,11 +390,11 @@ class _GitSourceBase(Source): tags = self._load_tags(node) self.track_tags = self.node_get_member(node, bool, 'track-tags', False) - self.original_url = self.node_get_member(node, str, 'url') + self.original_url = node.get_str('url') self.mirror = self.BST_MIRROR_CLASS(self, '', self.original_url, ref, tags=tags, primary=True) - self.tracking = self.node_get_member(node, str, 'track', None) + self.tracking = node.get_str('track', None) - self.ref_format = self.node_get_member(node, str, 'ref-format', 'sha1') + self.ref_format = node.get_str('ref-format', 'sha1') if self.ref_format not in ['sha1', 'git-describe']: provenance = self.node_provenance(node, member_name='ref-format') raise SourceError("{}: Unexpected value for ref-format: {}".format(provenance, self.ref_format)) @@ -415,7 +415,7 @@ class _GitSourceBase(Source): modules = node.get_mapping('submodules', {}) for path, _ in self.node_items(modules): submodule = modules.get_mapping(path) - url = self.node_get_member(submodule, str, 'url', None) + url = submodule.get_str('url', None) # Make sure to mark all URLs that are specified in the configuration if url: @@ -464,7 +464,7 @@ class _GitSourceBase(Source): return Consistency.INCONSISTENT def load_ref(self, node): - self.mirror.ref = self.node_get_member(node, str, 'ref', None) + self.mirror.ref = node.get_str('ref', None) self.mirror.tags = self._load_tags(node) def get_ref(self): @@ -665,8 +665,8 @@ class _GitSourceBase(Source): tags = [] tags_node = self.node_get_member(node, list, 'tags', []) for tag_node in tags_node: - tag = self.node_get_member(tag_node, str, 'tag') - commit_ref = self.node_get_member(tag_node, str, 'commit') + tag = tag_node.get_str('tag') + commit_ref = tag_node.get_str('commit') annotated = self.node_get_member(tag_node, bool, 'annotated') tags.append((tag, commit_ref, annotated)) return tags diff --git a/src/buildstream/_loader/loader.py b/src/buildstream/_loader/loader.py index 6036f4f07..4ac598faf 100644 --- a/src/buildstream/_loader/loader.py +++ b/src/buildstream/_loader/loader.py @@ -227,7 +227,7 @@ class Loader(): message, detail=detail) from e else: raise - kind = _yaml.node_get(node, str, Symbol.KIND) + kind = node.get_str(Symbol.KIND) if kind == "junction": self._first_pass_options.process_node(node) else: @@ -306,7 +306,7 @@ class Loader(): dep_deps = extract_depends_from_node(dep_element.node) loader_queue.append((dep_element, list(reversed(dep_deps)), [])) - if _yaml.node_get(dep_element.node, str, Symbol.KIND) == 'junction': + if dep_element.node.get_str(Symbol.KIND) == 'junction': raise LoadError(LoadErrorReason.INVALID_DATA, "{}: Cannot depend on junction" .format(dep.provenance)) @@ -467,17 +467,17 @@ class Loader(): meta_sources = [] sources = _yaml.node_get(node, list, Symbol.SOURCES, default_value=[]) - element_kind = _yaml.node_get(node, str, Symbol.KIND) + element_kind = node.get_str(Symbol.KIND) # Safe loop calling into _yaml.node_get() for each element ensures # we have good error reporting for i in range(len(sources)): source = _yaml.node_get(node, dict, Symbol.SOURCES, indices=[i]) - kind = _yaml.node_get(source, str, Symbol.KIND) + kind = source.get_str(Symbol.KIND) _yaml.node_del(source, Symbol.KIND) # Directory is optional - directory = _yaml.node_get(source, str, Symbol.DIRECTORY, default_value=None) + directory = source.get_str(Symbol.DIRECTORY, default=None) if directory: _yaml.node_del(source, Symbol.DIRECTORY) diff --git a/src/buildstream/_loader/types.pyx b/src/buildstream/_loader/types.pyx index c100b77b2..a3e49b040 100644 --- a/src/buildstream/_loader/types.pyx +++ b/src/buildstream/_loader/types.pyx @@ -85,7 +85,7 @@ cdef class Dependency: _yaml.node_validate(<_yaml.Node> dep, ['filename', 'type', 'junction']) # Make type optional, for this we set it to None - dep_type = _yaml.node_get(<_yaml.Node> dep, str, Symbol.TYPE, None, None) + dep_type = (<_yaml.MappingNode> dep).get_str( Symbol.TYPE, None) if dep_type is None or dep_type == Symbol.ALL: dep_type = None elif dep_type not in [Symbol.BUILD, Symbol.RUNTIME]: @@ -94,9 +94,9 @@ cdef class Dependency: "{}: Dependency type '{}' is not 'build', 'runtime' or 'all'" .format(provenance, dep_type)) - self.name = _yaml.node_get(<_yaml.Node> dep, str, Symbol.FILENAME) + self.name = (<_yaml.MappingNode> dep).get_str( Symbol.FILENAME) self.dep_type = dep_type - self.junction = _yaml.node_get(<_yaml.Node> dep, str, Symbol.JUNCTION, None, None) + self.junction = (<_yaml.MappingNode> dep).get_str( Symbol.JUNCTION, None) else: raise LoadError(LoadErrorReason.INVALID_DATA, diff --git a/src/buildstream/_options/option.py b/src/buildstream/_options/option.py index ffdb4d272..6aa4cdc01 100644 --- a/src/buildstream/_options/option.py +++ b/src/buildstream/_options/option.py @@ -60,8 +60,9 @@ class Option(): # node (dict): The loaded YAML dictionary describing # the option def load(self, node): - self.description = _yaml.node_get(node, str, 'description') - self.variable = _yaml.node_get(node, str, 'variable', default_value=None) + self.description = node.get_str('description') + variable_node = node.get_str('variable', default=None) + self.variable = variable_node # Assert valid symbol name for variable name if self.variable is not None: diff --git a/src/buildstream/_options/optionbool.py b/src/buildstream/_options/optionbool.py index bdbb1d32a..caeff9711 100644 --- a/src/buildstream/_options/optionbool.py +++ b/src/buildstream/_options/optionbool.py @@ -38,7 +38,7 @@ class OptionBool(Option): def load_value(self, node, *, transform=None): if transform: - self.set_value(transform(_yaml.node_get(node, str, self.name))) + self.set_value(transform(node.get_str(self.name))) else: self.value = _yaml.node_get(node, bool, self.name) diff --git a/src/buildstream/_options/optionenum.py b/src/buildstream/_options/optionenum.py index 889db965c..04a19395b 100644 --- a/src/buildstream/_options/optionenum.py +++ b/src/buildstream/_options/optionenum.py @@ -56,7 +56,7 @@ class OptionEnum(Option): self.value = self.load_default_value(node) def load_value(self, node, *, transform=None): - self.value = _yaml.node_get(node, str, self.name) + self.value = node.get_str(self.name) if transform: self.value = transform(self.value) self.validate(self.value, _yaml.node_get_provenance(node, self.name)) @@ -79,6 +79,6 @@ class OptionEnum(Option): "Valid values: {}".format(", ".join(self.values))) def load_default_value(self, node): - value = _yaml.node_get(node, str, 'default') + value = node.get_str('default') self.validate(value, _yaml.node_get_provenance(node, 'default')) return value diff --git a/src/buildstream/_options/optionpool.py b/src/buildstream/_options/optionpool.py index de3af3e15..25b96fa98 100644 --- a/src/buildstream/_options/optionpool.py +++ b/src/buildstream/_options/optionpool.py @@ -71,7 +71,7 @@ class OptionPool(): p = _yaml.node_get_provenance(options, option_name) _yaml.assert_symbol_name(p, option_name, "option name", allow_dashes=False) - opt_type_name = _yaml.node_get(option_definition, str, 'type') + opt_type_name = option_definition.get_str('type') try: opt_type = _OPTION_TYPES[opt_type_name] except KeyError: @@ -249,7 +249,7 @@ class OptionPool(): # def _process_one_node(self, node): conditions = _yaml.node_get(node, list, '(?)', default_value=None) - assertion = _yaml.node_get(node, str, '(!)', default_value=None) + assertion = node.get_str('(!)', default=None) # Process assersions first, we want to abort on the first encountered # assertion in a given dictionary, and not lose an assertion due to diff --git a/src/buildstream/_plugincontext.py b/src/buildstream/_plugincontext.py index 7a5407cf6..c6b6526cb 100644 --- a/src/buildstream/_plugincontext.py +++ b/src/buildstream/_plugincontext.py @@ -141,16 +141,16 @@ class PluginContext(): if kind not in _yaml.node_get(origin, list, 'plugins'): continue - if _yaml.node_get(origin, str, 'origin') == 'local': - local_path = _yaml.node_get(origin, str, 'path') + if origin.get_str('origin') == 'local': + local_path = origin.get_str('path') source = self._get_local_plugin_source(local_path) - elif _yaml.node_get(origin, str, 'origin') == 'pip': - package_name = _yaml.node_get(origin, str, 'package-name') + elif origin.get_str('origin') == 'pip': + package_name = origin.get_str('package-name') source, defaults = self._get_pip_plugin_source(package_name, kind) else: raise PluginError("Failed to load plugin '{}': " "Unexpected plugin origin '{}'" - .format(kind, _yaml.node_get(origin, str, 'origin'))) + .format(kind, origin.get_str('origin'))) loaded_dependency = True break diff --git a/src/buildstream/_project.py b/src/buildstream/_project.py index e23689d99..bb1b06b43 100644 --- a/src/buildstream/_project.py +++ b/src/buildstream/_project.py @@ -84,7 +84,7 @@ class ProjectConfig: self.source_overrides = {} # Source specific configurations self.mirrors = OrderedDict() # contains dicts of alias-mappings to URIs. self.default_mirror = None # The name of the preferred mirror. - self._aliases = {} # Aliases dictionary + self._aliases = None # Aliases dictionary # Project() @@ -200,7 +200,7 @@ class Project(): if url and utils._ALIAS_SEPARATOR in url: url_alias, url_body = url.split(utils._ALIAS_SEPARATOR, 1) - alias_url = _yaml.node_get(config._aliases, str, url_alias, default_value=None) + alias_url = config._aliases.get_str(url_alias, default=None) if alias_url: url = alias_url + url_body @@ -264,7 +264,7 @@ class Project(): # def get_path_from_node(self, node, key, *, check_is_file=False, check_is_dir=False): - path_str = _yaml.node_get(node, str, key) + path_str = node.get_str(key) path = Path(path_str) full_path = self._absolute_directory_path / path @@ -404,7 +404,7 @@ class Project(): else: config = self.config - return _yaml.node_get(config._aliases, str, alias, default_value=None) + return config._aliases.get_str(alias, default=None) # get_alias_uris() # @@ -419,7 +419,7 @@ class Project(): else: config = self.config - if not alias or alias not in config._aliases: + if not alias or alias not in config._aliases: # pylint: disable=unsupported-membership-test return [None] mirror_list = [] @@ -429,7 +429,7 @@ class Project(): mirror_list = alias_mapping[alias] + mirror_list else: mirror_list += alias_mapping[alias] - mirror_list.append(_yaml.node_get(config._aliases, str, alias)) + mirror_list.append(config._aliases.get_str(alias)) return mirror_list # load_elements() @@ -585,7 +585,7 @@ class Project(): # The project name, element path and option declarations # are constant and cannot be overridden by option conditional statements - self.name = _yaml.node_get(self._project_conf, str, 'name') + self.name = self._project_conf.get_str('name') # Validate that project name is a valid symbol name _yaml.assert_symbol_name(_yaml.node_get_provenance(pre_config_node, 'name'), @@ -621,7 +621,7 @@ class Project(): ignore_unknown=True) # Use separate file for storing source references - self.ref_storage = _yaml.node_get(pre_config_node, str, 'ref-storage') + self.ref_storage = pre_config_node.get_str('ref-storage') if self.ref_storage not in [ProjectRefStorage.INLINE, ProjectRefStorage.PROJECT_REFS]: p = _yaml.node_get_provenance(pre_config_node, 'ref-storage') raise LoadError(LoadErrorReason.INVALID_DATA, @@ -715,7 +715,7 @@ class Project(): # Perform environment expansion right away shell_environment = shell_options.get_mapping('environment', default={}) for key in _yaml.node_keys(shell_environment): - value = _yaml.node_get(shell_environment, str, key) + value = shell_environment.get_str(key) self._shell_environment[key] = os.path.expandvars(value) # Host files is parsed as a list for convenience @@ -730,8 +730,8 @@ class Project(): _yaml.node_validate(host_file_desc, ['path', 'host_path', 'optional']) # Parse the host mount - path = _yaml.node_get(host_file_desc, str, 'path') - host_path = _yaml.node_get(host_file_desc, str, 'host_path', default_value=None) + path = host_file_desc.get_str('path') + host_path = host_file_desc.get_str('host_path', default=None) optional = _yaml.node_get(host_file_desc, bool, 'optional', default_value=False) mount = HostMount(path, host_path, optional) @@ -806,8 +806,8 @@ class Project(): output.options.export_variables(output.base_variables) # Override default_mirror if not set by command-line - output.default_mirror = self._default_mirror or _yaml.node_get(overrides, str, - 'default-mirror', default_value=None) + output.default_mirror = self._default_mirror or overrides.get_str( + 'default-mirror', default=None) mirrors = _yaml.node_get(config, list, 'mirrors', default_value=[]) for mirror in mirrors: @@ -815,7 +815,7 @@ class Project(): 'name', 'aliases' ] _yaml.node_validate(mirror, allowed_mirror_fields) - mirror_name = _yaml.node_get(mirror, str, 'name') + mirror_name = mirror.get_str('name') alias_mappings = {} for alias_mapping, uris in _yaml.node_items(mirror.get_mapping('aliases')): assert isinstance(uris, list) @@ -880,7 +880,7 @@ class Project(): allowed_origins = ['core', 'local', 'pip'] _yaml.node_validate(origin, allowed_origin_fields) - origin_value = _yaml.node_get(origin, str, 'origin') + origin_value = origin.get_str('origin') if origin_value not in allowed_origins: raise LoadError( LoadErrorReason.INVALID_YAML, @@ -907,7 +907,7 @@ class Project(): # Store the origins if they're not 'core'. # core elements are loaded by default, so storing is unnecessary. - if _yaml.node_get(origin, str, 'origin') != 'core': + if origin.get_str('origin') != 'core': self._store_origin(origin, 'sources', plugin_source_origins) self._store_origin(origin, 'elements', plugin_element_origins) @@ -947,7 +947,7 @@ class Project(): if group in origin_node: _yaml.node_del(origin_node, group) - if _yaml.node_get(origin_node, str, 'origin') == 'local': + if origin_node.get_str('origin') == 'local': path = self.get_path_from_node(origin, 'path', check_is_dir=True) # paths are passed in relative to the project, but must be absolute diff --git a/src/buildstream/_variables.pyx b/src/buildstream/_variables.pyx index 9b8b5a902..c41ff5c3f 100644 --- a/src/buildstream/_variables.pyx +++ b/src/buildstream/_variables.pyx @@ -128,7 +128,7 @@ cdef class Variables: cdef str value for key in _yaml.node_keys(node): - value = _yaml.node_get(node, str, key) + value = node.get_str(key) ret[sys.intern(key)] = _parse_expstr(value) return ret diff --git a/src/buildstream/_workspaces.py b/src/buildstream/_workspaces.py index 1adc52ac2..a4f7abc76 100644 --- a/src/buildstream/_workspaces.py +++ b/src/buildstream/_workspaces.py @@ -631,8 +631,8 @@ class Workspaces(): def _load_workspace(self, node): dictionary = { 'prepared': _yaml.node_get(node, bool, 'prepared', default_value=False), - 'path': _yaml.node_get(node, str, 'path'), - 'last_successful': _yaml.node_get(node, str, 'last_successful', default_value=None), + 'path': node.get_str('path'), + 'last_successful': node.get_str('last_successful', default=None), 'running_files': _yaml.node_sanitize( node.get_mapping('running_files', default=None), dict_type=dict), diff --git a/src/buildstream/_yaml.pxd b/src/buildstream/_yaml.pxd index 7d7e87d8d..b2237f075 100644 --- a/src/buildstream/_yaml.pxd +++ b/src/buildstream/_yaml.pxd @@ -31,6 +31,12 @@ cdef class Node: cdef class MappingNode(Node): cdef Node get(self, str key, default, default_constructor) cpdef MappingNode get_mapping(self, str key, default=*) + cpdef ScalarNode get_scalar(self, str key, default=*) + cpdef str get_str(self, str key, object default=*) + + +cdef class ScalarNode(Node): + cpdef str as_str(self) cdef class ProvenanceInformation: diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx index 9d9a12400..a7779c41e 100644 --- a/src/buildstream/_yaml.pyx +++ b/src/buildstream/_yaml.pyx @@ -78,11 +78,19 @@ cdef class Node: cdef class ScalarNode(Node): def __init__(self, object value, int file_index, int line, int column): + if type(value) is str: + value = value.strip() self.value = value self.file_index = file_index self.line = line self.column = column + cpdef str as_str(self): + # We keep 'None' as 'None' to simplify the API's usage and allow chaining for users + if self.value is None: + return None + return str(self.value) + cdef class MappingNode(Node): @@ -119,6 +127,25 @@ cdef class MappingNode(Node): return value + cpdef ScalarNode get_scalar(self, str key, object default=_sentinel): + value = self.get(key, default, ScalarNode) + + if type(value) is not ScalarNode: + if value is None: + value = ScalarNode(None, _SYNTHETIC_FILE_INDEX, 0, next_synthetic_counter()) + else: + provenance = node_get_provenance(value) + raise LoadError(LoadErrorReason.INVALID_DATA, + "{}: Value of '{}' is not of the expected type 'Scalar'" + .format(provenance, key)) + + return value + + cpdef str get_str(self, str key, object default=_sentinel): + # TODO: don't go through 'get_scalar', we can directly get everything and optimize + cdef ScalarNode scalar = self.get_scalar(key, default) + return scalar.as_str() + class SequenceNode(Node): def __init__(self, list value, int file_index, int line, int column): diff --git a/src/buildstream/element.py b/src/buildstream/element.py index aaf8577a5..05e590823 100644 --- a/src/buildstream/element.py +++ b/src/buildstream/element.py @@ -512,7 +512,7 @@ class Element(Plugin): # variables in the returned string name = self.node_subst_member(node, 'name') """ - value = self.node_get_member(node, str, member_name, default) + value = node.get_str(member_name, default) try: return self.__variables.subst(value) except LoadError as e: @@ -2702,7 +2702,7 @@ class Element(Plugin): # Sandbox config, unlike others, has fixed members so we should validate them _yaml.node_validate(sandbox_config, ['build-uid', 'build-gid', 'build-os', 'build-arch']) - build_arch = _yaml.node_get(sandbox_config, str, 'build-arch', default_value=None) + build_arch = sandbox_config.get_str('build-arch', default=None) if build_arch: build_arch = Platform.canonicalize_arch(build_arch) else: @@ -2711,7 +2711,7 @@ class Element(Plugin): return SandboxConfig( _yaml.node_get(sandbox_config, int, 'build-uid'), _yaml.node_get(sandbox_config, int, 'build-gid'), - _yaml.node_get(sandbox_config, str, 'build-os', default_value=host_os), + sandbox_config.get_str('build-os', default=host_os), build_arch) # This makes a special exception for the split rules, which diff --git a/src/buildstream/plugins/elements/junction.py b/src/buildstream/plugins/elements/junction.py index 849df72be..d30e34828 100644 --- a/src/buildstream/plugins/elements/junction.py +++ b/src/buildstream/plugins/elements/junction.py @@ -175,9 +175,9 @@ class JunctionElement(Element): BST_FORBID_RDEPENDS = True def configure(self, node): - self.path = self.node_get_member(node, str, 'path', default='') + self.path = node.get_str('path', default='') self.options = node.get_mapping('options', default={}) - self.target = self.node_get_member(node, str, 'target', default=None) + self.target = node.get_str('target', default=None) self.target_element = None self.target_junction = None diff --git a/src/buildstream/plugins/sources/_downloadablefilesource.py b/src/buildstream/plugins/sources/_downloadablefilesource.py index b9b15e268..7b1785979 100644 --- a/src/buildstream/plugins/sources/_downloadablefilesource.py +++ b/src/buildstream/plugins/sources/_downloadablefilesource.py @@ -77,8 +77,8 @@ class DownloadableFileSource(Source): __urlopener = None def configure(self, node): - self.original_url = self.node_get_member(node, str, 'url') - self.ref = self.node_get_member(node, str, 'ref', None) + self.original_url = node.get_str('url') + self.ref = node.get_str('ref', None) self.url = self.translate_url(self.original_url) self._warn_deprecated_etag(node) @@ -99,7 +99,7 @@ class DownloadableFileSource(Source): return Consistency.RESOLVED def load_ref(self, node): - self.ref = self.node_get_member(node, str, 'ref', None) + self.ref = node.get_str('ref', None) self._warn_deprecated_etag(node) def get_ref(self): @@ -143,7 +143,7 @@ class DownloadableFileSource(Source): .format(self.url, sha256, self.ref)) def _warn_deprecated_etag(self, node): - etag = self.node_get_member(node, str, 'etag', None) + etag = node.get_str('etag', None) if etag: provenance = self.node_provenance(node, member_name='etag') self.warn('{} "etag" is deprecated and ignored.'.format(provenance)) diff --git a/src/buildstream/plugins/sources/bzr.py b/src/buildstream/plugins/sources/bzr.py index e59986da6..082d11dec 100644 --- a/src/buildstream/plugins/sources/bzr.py +++ b/src/buildstream/plugins/sources/bzr.py @@ -69,9 +69,9 @@ class BzrSource(Source): def configure(self, node): self.node_validate(node, ['url', 'track', 'ref', *Source.COMMON_CONFIG_KEYS]) - self.original_url = self.node_get_member(node, str, 'url') - self.tracking = self.node_get_member(node, str, 'track') - self.ref = self.node_get_member(node, str, 'ref', None) + self.original_url = node.get_str('url') + self.tracking = node.get_str('track') + self.ref = node.get_str('ref', None) self.url = self.translate_url(self.original_url) def preflight(self): diff --git a/src/buildstream/plugins/sources/deb.py b/src/buildstream/plugins/sources/deb.py index e45994951..cc88cf53c 100644 --- a/src/buildstream/plugins/sources/deb.py +++ b/src/buildstream/plugins/sources/deb.py @@ -61,7 +61,7 @@ class DebSource(TarSource): def configure(self, node): super().configure(node) - self.base_dir = self.node_get_member(node, str, 'base-dir', None) + self.base_dir = node.get_str('base-dir', None) def preflight(self): return diff --git a/src/buildstream/plugins/sources/pip.py b/src/buildstream/plugins/sources/pip.py index 9d6c40d74..db0537497 100644 --- a/src/buildstream/plugins/sources/pip.py +++ b/src/buildstream/plugins/sources/pip.py @@ -111,8 +111,8 @@ class PipSource(Source): def configure(self, node): self.node_validate(node, ['url', 'packages', 'ref', 'requirements-files'] + Source.COMMON_CONFIG_KEYS) - self.ref = self.node_get_member(node, str, 'ref', None) - self.original_url = self.node_get_member(node, str, 'url', _PYPI_INDEX_URL) + self.ref = node.get_str('ref', None) + self.original_url = node.get_str('url', _PYPI_INDEX_URL) self.index_url = self.translate_url(self.original_url) self.packages = self.node_get_member(node, list, 'packages', []) self.requirements_files = self.node_get_member(node, list, 'requirements-files', []) diff --git a/src/buildstream/plugins/sources/remote.py b/src/buildstream/plugins/sources/remote.py index 562a8f226..4e9fe4f7d 100644 --- a/src/buildstream/plugins/sources/remote.py +++ b/src/buildstream/plugins/sources/remote.py @@ -62,7 +62,7 @@ class RemoteSource(DownloadableFileSource): def configure(self, node): super().configure(node) - self.filename = self.node_get_member(node, str, 'filename', os.path.basename(self.url)) + self.filename = node.get_str('filename', os.path.basename(self.url)) self.executable = self.node_get_member(node, bool, 'executable', False) if os.sep in self.filename: diff --git a/src/buildstream/plugins/sources/tar.py b/src/buildstream/plugins/sources/tar.py index c90de74ea..ef2ee1d85 100644 --- a/src/buildstream/plugins/sources/tar.py +++ b/src/buildstream/plugins/sources/tar.py @@ -72,8 +72,7 @@ class TarSource(DownloadableFileSource): def configure(self, node): super().configure(node) - self.base_dir = self.node_get_member(node, str, 'base-dir', '*') - + self.base_dir = node.get_str('base-dir', '*') self.node_validate(node, DownloadableFileSource.COMMON_CONFIG_KEYS + ['base-dir']) def preflight(self): diff --git a/src/buildstream/plugins/sources/zip.py b/src/buildstream/plugins/sources/zip.py index 9981f1260..1724f0587 100644 --- a/src/buildstream/plugins/sources/zip.py +++ b/src/buildstream/plugins/sources/zip.py @@ -72,8 +72,7 @@ class ZipSource(DownloadableFileSource): def configure(self, node): super().configure(node) - self.base_dir = self.node_get_member(node, str, 'base-dir', '*') - + self.base_dir = node.get_str('base-dir', '*') self.node_validate(node, DownloadableFileSource.COMMON_CONFIG_KEYS + ['base-dir']) def get_unique_key(self): diff --git a/tests/elements/filter.py b/tests/elements/filter.py index d89c834e0..8292c09bb 100644 --- a/tests/elements/filter.py +++ b/tests/elements/filter.py @@ -240,7 +240,7 @@ def test_filter_track(datafiles, cli, tmpdir): # Now check that a ref field exists new_input = _yaml.load(input_file) source_node = _yaml.node_get(new_input, dict, 'sources', indices=[0]) - new_input_ref = _yaml.node_get(source_node, str, 'ref') + new_input_ref = source_node.get_str('ref') assert new_input_ref == ref @@ -349,7 +349,7 @@ def test_filter_track_multi_to_one(datafiles, cli, tmpdir): # Now check that a ref field exists new_input = _yaml.load(input_file) source_node = _yaml.node_get(new_input, dict, 'sources', indices=[0]) - new_ref = _yaml.node_get(source_node, str, 'ref') + new_ref = source_node.get_str('ref') assert new_ref == ref @@ -414,12 +414,12 @@ def test_filter_track_multi(datafiles, cli, tmpdir): # Now check that a ref field exists new_input = _yaml.load(input_file) source_node = _yaml.node_get(new_input, dict, 'sources', indices=[0]) - new_ref = _yaml.node_get(source_node, str, 'ref') + new_ref = source_node.get_str('ref') assert new_ref == ref new_input2 = _yaml.load(input2_file) source_node2 = _yaml.node_get(new_input2, dict, 'sources', indices=[0]) - new_ref2 = _yaml.node_get(source_node2, str, 'ref') + new_ref2 = source_node2.get_str('ref') assert new_ref2 == ref @@ -487,7 +487,7 @@ def test_filter_track_multi_exclude(datafiles, cli, tmpdir): new_input2 = _yaml.load(input2_file) source_node2 = _yaml.node_get(new_input2, dict, 'sources', indices=[0]) - new_ref2 = _yaml.node_get(source_node2, str, 'ref') + new_ref2 = source_node2.get_str('ref') assert new_ref2 == ref diff --git a/tests/format/include.py b/tests/format/include.py index f2815ca31..82667f19f 100644 --- a/tests/format/include.py +++ b/tests/format/include.py @@ -102,7 +102,7 @@ def test_include_junction_options(cli, datafiles): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'build_arch') == 'x86_64' + assert loaded.get_str('build_arch') == 'x86_64' @pytest.mark.datafiles(DATA_DIR) @@ -135,7 +135,7 @@ def test_junction_element_partial_project_project(cli, tmpdir, datafiles): 'junction.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'included', default_value=None) is None + assert loaded.get_str('included', default=None) is None @pytest.mark.datafiles(DATA_DIR) @@ -168,7 +168,7 @@ def test_junction_element_not_partial_project_file(cli, tmpdir, datafiles): 'junction.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'included', default_value=None) is not None + assert loaded.get_str('included', default=None) is not None @pytest.mark.datafiles(DATA_DIR) @@ -182,8 +182,8 @@ def test_include_element_overrides(cli, datafiles): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'manual_main_override', default_value=None) is not None - assert _yaml.node_get(loaded, str, 'manual_included_override', default_value=None) is not None + assert loaded.get_str('manual_main_override', default=None) is not None + assert loaded.get_str('manual_included_override', default=None) is not None @pytest.mark.datafiles(DATA_DIR) @@ -229,7 +229,7 @@ def test_include_element_overrides_sub_include(cli, datafiles): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'included', default_value=None) is not None + assert loaded.get_str('included', default=None) is not None @pytest.mark.datafiles(DATA_DIR) @@ -248,8 +248,8 @@ def test_junction_do_not_use_included_overrides(cli, tmpdir, datafiles): 'junction.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'main_override', default_value=None) is not None - assert _yaml.node_get(loaded, str, 'included_override', default_value=None) is None + assert loaded.get_str('main_override', default=None) is not None + assert loaded.get_str('included_override', default=None) is None @pytest.mark.datafiles(DATA_DIR) @@ -264,7 +264,7 @@ def test_conditional_in_fragment(cli, datafiles): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'size') == '8' + assert loaded.get_str('size') == '8' @pytest.mark.datafiles(DATA_DIR) @@ -278,7 +278,7 @@ def test_inner(cli, datafiles): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'build_arch') == 'x86_64' + assert loaded.get_str('build_arch') == 'x86_64' @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/format/include_composition.py b/tests/format/include_composition.py index 4afde817d..2051f02d5 100644 --- a/tests/format/include_composition.py +++ b/tests/format/include_composition.py @@ -128,7 +128,7 @@ def test_main_keeps_keys(tmpdir): includes.process(main) assert _yaml.node_get(main, list, 'test') == ['a'] - assert _yaml.node_get(main, str, 'something') == 'else' + assert main.get_str('something') == 'else' def test_overwrite_directive_on_later_composite(tmpdir): @@ -152,4 +152,4 @@ def test_overwrite_directive_on_later_composite(tmpdir): includes.process(main) assert _yaml.node_get(main, list, 'test') == ['Overwritten'] - assert _yaml.node_get(main, str, 'foo') == 'should be present' + assert main.get_str('foo') == 'should be present' diff --git a/tests/format/optionarch.py b/tests/format/optionarch.py index a35ac685d..f347e27ae 100644 --- a/tests/format/optionarch.py +++ b/tests/format/optionarch.py @@ -48,7 +48,7 @@ def test_conditional(cli, datafiles, machine, value, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'result') == expected + assert loaded.get_str('result') == expected @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/format/optionbool.py b/tests/format/optionbool.py index 0d1ee601e..d772b483c 100644 --- a/tests/format/optionbool.py +++ b/tests/format/optionbool.py @@ -42,7 +42,7 @@ def test_conditional_cli(cli, datafiles, target, option, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'thepony') == expected + assert loaded.get_str('thepony') == expected # Test configuration of boolean option in the config file @@ -71,7 +71,7 @@ def test_conditional_config(cli, datafiles, target, option, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'thepony') == expected + assert loaded.get_str('thepony') == expected @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/format/optioneltmask.py b/tests/format/optioneltmask.py index d33b5771c..75265fdd7 100644 --- a/tests/format/optioneltmask.py +++ b/tests/format/optioneltmask.py @@ -28,7 +28,7 @@ def test_conditional_cli(cli, datafiles, target, value, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'debug') == expected + assert loaded.get_str('debug') == expected @pytest.mark.datafiles(DATA_DIR) @@ -56,7 +56,7 @@ def test_conditional_config(cli, datafiles, target, value, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'debug') == expected + assert loaded.get_str('debug') == expected @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/format/optionenum.py b/tests/format/optionenum.py index b8a96b0c2..f9aff503f 100644 --- a/tests/format/optionenum.py +++ b/tests/format/optionenum.py @@ -33,7 +33,7 @@ def test_conditional_cli(cli, datafiles, target, option, value, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'result') == expected + assert loaded.get_str('result') == expected @pytest.mark.datafiles(DATA_DIR) @@ -66,7 +66,7 @@ def test_conditional_config(cli, datafiles, target, option, value, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'result') == expected + assert loaded.get_str('result') == expected @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/format/optionexports.py b/tests/format/optionexports.py index 5df7522b5..104abcf83 100644 --- a/tests/format/optionexports.py +++ b/tests/format/optionexports.py @@ -36,4 +36,4 @@ def test_export(cli, datafiles, option_name, option_value, var_name, var_value): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, var_name) == var_value + assert loaded.get_str(var_name) == var_value diff --git a/tests/format/optionflags.py b/tests/format/optionflags.py index e28c54236..29bb7ec2c 100644 --- a/tests/format/optionflags.py +++ b/tests/format/optionflags.py @@ -39,7 +39,7 @@ def test_conditional_cli(cli, datafiles, target, option, value, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'result') == expected + assert loaded.get_str('result') == expected @pytest.mark.datafiles(DATA_DIR) @@ -69,7 +69,7 @@ def test_conditional_config(cli, datafiles, target, option, value, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'result') == expected + assert loaded.get_str('result') == expected @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/format/optionos.py b/tests/format/optionos.py index 57277b106..f915d889e 100644 --- a/tests/format/optionos.py +++ b/tests/format/optionos.py @@ -47,7 +47,7 @@ def test_conditionals(cli, datafiles, system, value, expected): result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'result') == expected + assert loaded.get_str('result') == expected @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/format/optionoverrides.py b/tests/format/optionoverrides.py index 60d02b3a3..d4ed257dd 100644 --- a/tests/format/optionoverrides.py +++ b/tests/format/optionoverrides.py @@ -29,4 +29,4 @@ def test_override(cli, datafiles, arch): expected_value = '--host={}-unknown-linux-gnu'.format(arch) loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'conf-global') == expected_value + assert loaded.get_str('conf-global') == expected_value diff --git a/tests/format/options.py b/tests/format/options.py index 3a8210dc3..e902b8b86 100644 --- a/tests/format/options.py +++ b/tests/format/options.py @@ -136,7 +136,7 @@ def test_simple_conditional(cli, datafiles, opt_option, expected_prefix): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'prefix') == expected_prefix + assert loaded.get_str('prefix') == expected_prefix @pytest.mark.datafiles(DATA_DIR) @@ -159,7 +159,7 @@ def test_nested_conditional(cli, datafiles, debug, logging, expected): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'debug') == expected + assert loaded.get_str('debug') == expected @pytest.mark.datafiles(DATA_DIR) @@ -182,7 +182,7 @@ def test_compound_and_conditional(cli, datafiles, debug, logging, expected): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'debug') == expected + assert loaded.get_str('debug') == expected @pytest.mark.datafiles(DATA_DIR) @@ -205,7 +205,7 @@ def test_compound_or_conditional(cli, datafiles, debug, logging, expected): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, str, 'logging') == expected + assert loaded.get_str('logging') == expected @pytest.mark.datafiles(DATA_DIR) @@ -226,7 +226,7 @@ def test_deep_nesting_level1(cli, datafiles, option, expected): shallow_list = _yaml.node_get(loaded, list, 'shallow-nest') first_dict = shallow_list[0] - assert _yaml.node_get(first_dict, str, 'animal') == expected + assert first_dict.get_str('animal') == expected @pytest.mark.datafiles(DATA_DIR) @@ -248,4 +248,4 @@ def test_deep_nesting_level2(cli, datafiles, option, expected): deeper_list = shallow_list[0] first_dict = deeper_list[0] - assert _yaml.node_get(first_dict, str, 'animal') == expected + assert first_dict.get_str('animal') == expected diff --git a/tests/format/project.py b/tests/format/project.py index fbb742d47..697fe7e3d 100644 --- a/tests/format/project.py +++ b/tests/format/project.py @@ -82,8 +82,8 @@ def test_load_default_project(cli, datafiles): # Read back some of our project defaults from the env env = _yaml.load_data(result.output) - assert _yaml.node_get(env, str, 'USER') == "tomjon" - assert _yaml.node_get(env, str, 'TERM') == "dumb" + assert env.get_str('USER') == "tomjon" + assert env.get_str('TERM') == "dumb" @pytest.mark.datafiles(os.path.join(DATA_DIR)) @@ -97,8 +97,8 @@ def test_load_project_from_subdir(cli, datafiles): # Read back some of our project defaults from the env env = _yaml.load_data(result.output) - assert _yaml.node_get(env, str, 'USER') == "tomjon" - assert _yaml.node_get(env, str, 'TERM') == "dumb" + assert env.get_str('USER') == "tomjon" + assert env.get_str('TERM') == "dumb" @pytest.mark.datafiles(os.path.join(DATA_DIR)) @@ -111,7 +111,7 @@ def test_override_project_path(cli, datafiles): # Read back the overridden path env = _yaml.load_data(result.output) - assert _yaml.node_get(env, str, 'PATH') == "/bin:/sbin" + assert env.get_str('PATH') == "/bin:/sbin" @pytest.mark.datafiles(os.path.join(DATA_DIR)) diff --git a/tests/format/variables.py b/tests/format/variables.py index 87e3b2903..ed5407b83 100644 --- a/tests/format/variables.py +++ b/tests/format/variables.py @@ -49,7 +49,7 @@ def test_defaults(cli, datafiles, target, varname, expected): ]) result.assert_success() result_vars = _yaml.load_data(result.output) - assert _yaml.node_get(result_vars, str, varname) == expected + assert result_vars.get_str(varname) == expected ################################################################ @@ -75,7 +75,7 @@ def test_overrides(cli, datafiles, target, varname, expected): ]) result.assert_success() result_vars = _yaml.load_data(result.output) - assert _yaml.node_get(result_vars, str, varname) == expected + assert result_vars.get_str(varname) == expected @pytest.mark.datafiles(os.path.join(DATA_DIR, 'missing_variables')) diff --git a/tests/frontend/cross_junction_workspace.py b/tests/frontend/cross_junction_workspace.py index 81fd43487..14039186c 100644 --- a/tests/frontend/cross_junction_workspace.py +++ b/tests/frontend/cross_junction_workspace.py @@ -79,7 +79,7 @@ def test_list_cross_junction(cli, tmpdir): workspaces = _yaml.node_get(loaded, list, 'workspaces') assert len(workspaces) == 1 assert 'element' in workspaces[0] - assert _yaml.node_get(workspaces[0], str, 'element') == element + assert workspaces[0].get_str('element') == element def test_close_cross_junction(cli, tmpdir): diff --git a/tests/frontend/init.py b/tests/frontend/init.py index 0eac5d528..e9d92be90 100644 --- a/tests/frontend/init.py +++ b/tests/frontend/init.py @@ -19,9 +19,9 @@ def test_defaults(cli, tmpdir): result.assert_success() project_conf = _yaml.load(project_path) - assert _yaml.node_get(project_conf, str, 'name') == 'foo' - assert _yaml.node_get(project_conf, str, 'format-version') == str(BST_FORMAT_VERSION) - assert _yaml.node_get(project_conf, str, 'element-path') == 'elements' + assert project_conf.get_str('name') == 'foo' + assert project_conf.get_str('format-version') == str(BST_FORMAT_VERSION) + assert project_conf.get_str('element-path') == 'elements' def test_all_options(cli, tmpdir): @@ -37,9 +37,9 @@ def test_all_options(cli, tmpdir): result.assert_success() project_conf = _yaml.load(project_path) - assert _yaml.node_get(project_conf, str, 'name') == 'foo' - assert _yaml.node_get(project_conf, str, 'format-version') == str(2) - assert _yaml.node_get(project_conf, str, 'element-path') == 'ponies' + assert project_conf.get_str('name') == 'foo' + assert project_conf.get_str('format-version') == str(2) + assert project_conf.get_str('element-path') == 'ponies' elements_dir = os.path.join(project, 'ponies') assert os.path.isdir(elements_dir) @@ -70,8 +70,8 @@ def test_force_overwrite_project(cli, tmpdir): result.assert_success() project_conf = _yaml.load(project_path) - assert _yaml.node_get(project_conf, str, 'name') == 'foo' - assert _yaml.node_get(project_conf, str, 'format-version') == str(BST_FORMAT_VERSION) + assert project_conf.get_str('name') == 'foo' + assert project_conf.get_str('format-version') == str(BST_FORMAT_VERSION) @pytest.mark.parametrize("project_name", [('Micheal Jackson'), ('one+one')]) @@ -122,6 +122,6 @@ def test_element_path_interactive(cli, tmp_path, monkeypatch, element_path): assert full_element_path.exists() project_conf = _yaml.load(str(project_conf_path)) - assert _yaml.node_get(project_conf, str, 'name') == 'project_name' - assert _yaml.node_get(project_conf, str, 'format-version') == '0' - assert _yaml.node_get(project_conf, str, 'element-path') == element_path + assert project_conf.get_str('name') == 'project_name' + assert project_conf.get_str('format-version') == '0' + assert project_conf.get_str('element-path') == element_path diff --git a/tests/frontend/project/sources/fetch_source.py b/tests/frontend/project/sources/fetch_source.py index 4c265f99b..9a873d1fe 100644 --- a/tests/frontend/project/sources/fetch_source.py +++ b/tests/frontend/project/sources/fetch_source.py @@ -39,7 +39,7 @@ class FetchSource(Source): # Read config to know which URLs to fetch def configure(self, node): self.original_urls = self.node_get_member(node, list, 'urls') - self.output_file = self.node_get_member(node, str, 'output-text') + self.output_file = node.get_str('output-text') self.fetch_succeeds = {} if 'fetch-succeeds' in node: fetch_succeeds_node = node.get_mapping('fetch-succeeds') diff --git a/tests/frontend/workspace.py b/tests/frontend/workspace.py index 5267f8e56..7ca06bdd4 100644 --- a/tests/frontend/workspace.py +++ b/tests/frontend/workspace.py @@ -186,9 +186,9 @@ def test_open_bzr_customize(cli, tmpdir, datafiles): element_config = _yaml.load(os.path.join(project, "elements", element_name)) source_config = _yaml.node_get(element_config, dict, 'sources', [0]) output = subprocess.check_output(["bzr", "info"], cwd=workspace) - stripped_url = _yaml.node_get(source_config, str, 'url').lstrip("file:///") + stripped_url = source_config.get_str('url').lstrip("file:///") expected_output_str = ("checkout of branch: /{}/{}" - .format(stripped_url, _yaml.node_get(source_config, str, 'track'))) + .format(stripped_url, source_config.get_str('track'))) assert expected_output_str in str(output) @@ -613,8 +613,8 @@ def test_list(cli, tmpdir, datafiles): assert len(workspaces) == 1 space = workspaces[0] - assert _yaml.node_get(space, str, 'element') == element_name - assert _yaml.node_get(space, str, 'directory') == workspace + assert space.get_str('element') == element_name + assert space.get_str('directory') == workspace @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/internals/yaml.py b/tests/internals/yaml.py index a553cbf0f..012449ee4 100644 --- a/tests/internals/yaml.py +++ b/tests/internals/yaml.py @@ -128,7 +128,7 @@ def test_node_set(datafiles): assert 'mother' not in base _yaml.node_set(base, 'mother', 'snow white') - assert _yaml.node_get(base, str, 'mother') == 'snow white' + assert base.get_str('mother') == 'snow white' @pytest.mark.datafiles(os.path.join(DATA_DIR)) @@ -141,14 +141,14 @@ def test_node_set_overwrite(datafiles): base = _yaml.load(filename) # Overwrite a string - assert _yaml.node_get(base, str, 'kind') == 'pony' + assert base.get_str('kind') == 'pony' _yaml.node_set(base, 'kind', 'cow') - assert _yaml.node_get(base, str, 'kind') == 'cow' + assert base.get_str('kind') == 'cow' # Overwrite a list as a string assert _yaml.node_get(base, list, 'moods') == ['happy', 'sad'] _yaml.node_set(base, 'moods', 'unemotional') - assert _yaml.node_get(base, str, 'moods') == 'unemotional' + assert base.get_str('moods') == 'unemotional' @pytest.mark.datafiles(os.path.join(DATA_DIR)) @@ -192,10 +192,10 @@ def test_composite_preserve_originals(datafiles): orig_extra = base.get_mapping('extra') # Test that the node copy has the overridden value... - assert _yaml.node_get(copy_extra, str, 'old') == 'override' + assert copy_extra.get_str('old') == 'override' # But the original node is not effected by the override. - assert _yaml.node_get(orig_extra, str, 'old') == 'new' + assert orig_extra.get_str('old') == 'new' # Tests for list composition @@ -258,7 +258,7 @@ def test_list_composition(datafiles, filename, tmpdir, assert len(children) == length child = children[index] - assert _yaml.node_get(child, str, 'mood') == mood + assert child.get_str('mood') == mood assert_provenance(prov_file, prov_line, prov_col, child, 'mood') @@ -394,7 +394,7 @@ def test_list_composition_twice(datafiles, tmpdir, filename1, filename2, assert len(children) == length child = children[index] - assert _yaml.node_get(child, str, 'mood') == mood + assert child.get_str('mood') == mood assert_provenance(prov_file, prov_line, prov_col, child, 'mood') ##################### @@ -411,7 +411,7 @@ def test_list_composition_twice(datafiles, tmpdir, filename1, filename2, assert len(children) == length child = children[index] - assert _yaml.node_get(child, str, 'mood') == mood + assert child.get_str('mood') == mood assert_provenance(prov_file, prov_line, prov_col, child, 'mood') @@ -424,19 +424,19 @@ def test_convert_value_to_string(datafiles): # Run file through yaml to convert it test_dict = _yaml.load(conf_file) - user_config = _yaml.node_get(test_dict, str, "Test1") + user_config = test_dict.get_str("Test1") assert isinstance(user_config, str) assert user_config == "1_23_4" - user_config = _yaml.node_get(test_dict, str, "Test2") + user_config = test_dict.get_str("Test2") assert isinstance(user_config, str) assert user_config == "1.23.4" - user_config = _yaml.node_get(test_dict, str, "Test3") + user_config = test_dict.get_str("Test3") assert isinstance(user_config, str) assert user_config == "1.20" - user_config = _yaml.node_get(test_dict, str, "Test4") + user_config = test_dict.get_str("Test4") assert isinstance(user_config, str) assert user_config == "OneTwoThree" diff --git a/tests/sources/git.py b/tests/sources/git.py index 45a2b827a..2b8a12997 100644 --- a/tests/sources/git.py +++ b/tests/sources/git.py @@ -760,7 +760,7 @@ def test_track_fetch(cli, tmpdir, datafiles, ref_format, tag, extra_commit): result.assert_success() element = _yaml.load(element_path) - new_ref = _yaml.node_get(_yaml.node_get(element, dict, 'sources', [0]), str, 'ref') + new_ref = _yaml.node_get(element, dict, 'sources', [0]).get_str('ref') if ref_format == 'git-describe' and tag: # Check and strip prefix @@ -857,8 +857,8 @@ def test_git_describe(cli, tmpdir, datafiles, ref_storage, tag_type): assert 'annotated' in tag assert _yaml.node_get(tag, bool, 'annotated') == (tag_type == 'annotated') - assert {(_yaml.node_get(tag, str, 'tag'), - _yaml.node_get(tag, str, 'commit')) + assert {(tag.get_str('tag'), + tag.get_str('commit')) for tag in tags} == {('tag1', repo.rev_parse('tag1^{commit}')), ('tag2', repo.rev_parse('tag2^{commit}'))} @@ -972,8 +972,8 @@ def test_git_describe_head_is_tagged(cli, tmpdir, datafiles, ref_storage, tag_ty assert 'annotated' in tag assert _yaml.node_get(tag, bool, 'annotated') == (tag_type == 'annotated') - tag_name = _yaml.node_get(tag, str, 'tag') - commit = _yaml.node_get(tag, str, 'commit') + tag_name = tag.get_str('tag') + commit = tag.get_str('commit') assert (tag_name, commit) == ('tag', repo.rev_parse('tag^{commit}')) checkout = os.path.join(str(tmpdir), 'checkout') diff --git a/tests/sources/previous_source_access/plugins/sources/foo_transform.py b/tests/sources/previous_source_access/plugins/sources/foo_transform.py index bec4f9913..cd0f621d5 100644 --- a/tests/sources/previous_source_access/plugins/sources/foo_transform.py +++ b/tests/sources/previous_source_access/plugins/sources/foo_transform.py @@ -32,7 +32,7 @@ class FooTransformSource(Source): def configure(self, node): self.node_validate(node, ['ref', *Source.COMMON_CONFIG_KEYS]) - self.ref = self.node_get_member(node, str, 'ref', None) + self.ref = node.get_str('ref', None) def preflight(self): pass -- cgit v1.2.1