summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schubert <contact@benschubert.me>2019-06-11 22:55:29 +0100
committerBenjamin Schubert <ben.c.schubert@gmail.com>2019-06-13 16:05:52 +0100
commit4ace73c0a9c7e15e544b40eb378d7c4712ab0872 (patch)
tree1e999672d53e738489be276314a9edd5e403b633
parentd792c7be809d3e37f440c4cf012f353f76ba69e9 (diff)
downloadbuildstream-bschubert/node-api.tar.gz
replace node_set for MappingNodebschubert/node-api
-rw-r--r--src/buildstream/_options/optionpool.py2
-rw-r--r--src/buildstream/_project.py8
-rw-r--r--src/buildstream/_projectrefs.py6
-rw-r--r--src/buildstream/_workspaces.py2
-rw-r--r--src/buildstream/_yaml.pyx39
-rw-r--r--src/buildstream/element.py16
-rw-r--r--tests/artifactcache/junctions.py4
-rw-r--r--tests/elements/filter/basic/element_plugins/dynamic.py2
-rw-r--r--tests/internals/yaml.py6
-rw-r--r--tests/sources/git.py16
-rw-r--r--tests/sources/previous_source_access.py2
11 files changed, 65 insertions, 38 deletions
diff --git a/src/buildstream/_options/optionpool.py b/src/buildstream/_options/optionpool.py
index 1b8683186..1d676e54a 100644
--- a/src/buildstream/_options/optionpool.py
+++ b/src/buildstream/_options/optionpool.py
@@ -152,7 +152,7 @@ class OptionPool():
def export_variables(self, variables):
for _, option in self._options.items():
if option.variable:
- _yaml.node_set(variables, option.variable, option.get_value())
+ variables[option.variable] = option.get_value()
# printable_variables()
#
diff --git a/src/buildstream/_project.py b/src/buildstream/_project.py
index 43bdc73c9..70ebb6e63 100644
--- a/src/buildstream/_project.py
+++ b/src/buildstream/_project.py
@@ -797,7 +797,7 @@ class Project():
output.base_variables = config.get_mapping('variables')
# Add the project name as a default variable
- _yaml.node_set(output.base_variables, 'project-name', self.name)
+ output.base_variables['project-name'] = self.name
# Extend variables with automatic variables and option exports
# Initialize it as a string as all variables are processed as strings.
@@ -805,7 +805,7 @@ class Project():
# max-jobs value seems to be around 8-10 if we have enough cores
# users should set values based on workload and build infrastructure
platform = Platform.get_platform()
- _yaml.node_set(output.base_variables, 'max-jobs', str(platform.get_cpu_count(8)))
+ output.base_variables['max-jobs'] = platform.get_cpu_count(8)
# Export options into variables, if that was requested
output.options.export_variables(output.base_variables)
@@ -947,7 +947,7 @@ class Project():
if plugin_group in node_keys:
origin_node = origin.copy()
plugins = origin.get_mapping(plugin_group, default={})
- _yaml.node_set(origin_node, 'plugins', [k for k in _yaml.node_keys(plugins)])
+ origin_node['plugins'] = [k for k in _yaml.node_keys(plugins)]
for group in expected_groups:
if group in origin_node:
_yaml.node_del(origin_node, group)
@@ -956,7 +956,7 @@ class Project():
path = self.get_path_from_node(origin, 'path',
check_is_dir=True)
# paths are passed in relative to the project, but must be absolute
- _yaml.node_set(origin_node, 'path', os.path.join(self.directory, path))
+ origin_node['path'] = os.path.join(self.directory, path)
destination.append(origin_node)
# _warning_is_fatal():
diff --git a/src/buildstream/_projectrefs.py b/src/buildstream/_projectrefs.py
index f296858cf..a0f930ecf 100644
--- a/src/buildstream/_projectrefs.py
+++ b/src/buildstream/_projectrefs.py
@@ -87,7 +87,7 @@ class ProjectRefs():
# Ensure we create our toplevel entry point on the fly here
for node in [self._toplevel_node, self._toplevel_save]:
if 'projects' not in node:
- _yaml.node_set(node, 'projects', _yaml.new_empty_node(ref_node=node))
+ node['projects'] = _yaml.new_empty_node(ref_node=node)
# lookup_ref()
#
@@ -129,7 +129,7 @@ class ProjectRefs():
if not ensure:
return None
project_node = _yaml.new_empty_node(ref_node=projects)
- _yaml.node_set(projects, project, project_node)
+ projects[project] = project_node
# Fetch the element
try:
@@ -138,7 +138,7 @@ class ProjectRefs():
if not ensure:
return None
element_list = _yaml.new_empty_list_node()
- _yaml.node_set(project_node, element, element_list)
+ project_node[element] = element_list
# Fetch the source index
try:
diff --git a/src/buildstream/_workspaces.py b/src/buildstream/_workspaces.py
index 21944c799..2095ce755 100644
--- a/src/buildstream/_workspaces.py
+++ b/src/buildstream/_workspaces.py
@@ -594,7 +594,7 @@ class Workspaces():
raise LoadError(LoadErrorReason.INVALID_DATA,
detail.format(element, self._get_filename()))
- _yaml.node_set(workspaces, element, sources[0][1])
+ workspaces[element] = sources[0][1]
else:
raise LoadError(LoadErrorReason.INVALID_DATA,
diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx
index 6030e285e..ae2347255 100644
--- a/src/buildstream/_yaml.pyx
+++ b/src/buildstream/_yaml.pyx
@@ -83,6 +83,14 @@ cdef class ScalarNode(Node):
def __init__(self, object value, int file_index, int line, int column):
if type(value) is str:
value = value.strip()
+ elif type(value) is bool:
+ if value:
+ value = "True"
+ else:
+ value = "False"
+ elif type(value) is int:
+ value = str(value)
+
self.value = value
self.file_index = file_index
self.line = line
@@ -132,6 +140,17 @@ cdef class MappingNode(Node):
self.line = line
self.column = column
+ def __setitem__(self, str key, object value):
+ if type(value) in [MappingNode, ScalarNode, SequenceNode]:
+ self.value[key] = value
+ else:
+ old_value = self.value.get(key, None)
+
+ if old_value is None:
+ self.value[key] = _create_node(value, self.file_index, self.line, next_synthetic_counter())
+ else:
+ self.value[key] = _create_node(value, old_value.file_index, old_value.line, old_value.column)
+
cdef Node get(self, str key, default, default_constructor):
value = self.value.get(key, _sentinel)
@@ -532,13 +551,21 @@ cdef class Representer:
cdef Node _create_node(object value, int file_index, int line, int column):
- if type(value) in [bool, str, type(None), int]:
+ cdef value_type = type(value)
+
+ if value_type in [bool, str, type(None), int]:
return ScalarNode(value, file_index, line, column)
- elif type(value) is dict:
- return MappingNode(value, file_index, line, column)
- elif type(value) is list:
- return SequenceNode(value, file_index, line, column)
- return Node(value, file_index, line, column)
+ elif value_type is dict:
+ new_node = MappingNode({}, file_index, line, column)
+ for key, entry in (<dict> value).items():
+ (<MappingNode> new_node)[key] = entry
+ return new_node
+ elif value_type is list:
+ value = __new_node_from_list(value).value
+ new_node = SequenceNode(value, file_index, line, column)
+ return new_node
+
+ raise ValueError("Can't create a new node for type {}".format(value_type))
# Loads a dictionary from some YAML
diff --git a/src/buildstream/element.py b/src/buildstream/element.py
index 006d42d6b..7509914e4 100644
--- a/src/buildstream/element.py
+++ b/src/buildstream/element.py
@@ -251,7 +251,7 @@ class Element(Plugin):
# Collect the composited variables and resolve them
variables = self.__extract_variables(project, meta)
- _yaml.node_set(variables, 'element-name', self.name)
+ variables['element-name'] = self.name
self.__variables = Variables(variables)
# Collect the composited environment now that we have variables
@@ -920,7 +920,7 @@ class Element(Plugin):
if data is not None:
data = data.copy()
- _yaml.node_set(self.__dynamic_public, domain, data)
+ self.__dynamic_public[domain] = data
def get_environment(self):
"""Fetch the environment suitable for running in the sandbox
@@ -2536,9 +2536,9 @@ class Element(Plugin):
# Extend project wide split rules with any split rules defined by the element
_yaml.composite(splits, element_splits)
- _yaml.node_set(element_bst, 'split-rules', splits)
- _yaml.node_set(element_public, 'bst', element_bst)
- _yaml.node_set(defaults, 'public', element_public)
+ element_bst['split-rules'] = splits
+ element_public['bst'] = element_bst
+ defaults['public'] = element_public
@classmethod
def __init_defaults(cls, project, plugin_conf, kind, is_junction):
@@ -2717,8 +2717,8 @@ class Element(Plugin):
# element specific defaults
_yaml.composite(base_splits, element_splits)
- _yaml.node_set(element_bst, 'split-rules', base_splits)
- _yaml.node_set(element_public, 'bst', element_bst)
+ element_bst['split-rules'] = base_splits
+ element_public['bst'] = element_bst
_yaml.node_final_assertions(element_public)
@@ -2735,7 +2735,7 @@ class Element(Plugin):
self.__variables.subst(split.strip())
for split in splits
]
- _yaml.node_set(element_splits, domain, splits)
+ element_splits[domain] = splits
return element_public
diff --git a/tests/artifactcache/junctions.py b/tests/artifactcache/junctions.py
index 52d721baf..58a23fca1 100644
--- a/tests/artifactcache/junctions.py
+++ b/tests/artifactcache/junctions.py
@@ -31,10 +31,10 @@ def assert_shared(cli, share, project_name, project, element_name):
def project_set_artifacts(project, url):
project_conf_file = os.path.join(project, 'project.conf')
project_config = _yaml.load(project_conf_file)
- _yaml.node_set(project_config, 'artifacts', {
+ project_config['artifacts'] = {
'url': url,
'push': True
- })
+ }
_yaml.dump(_yaml.node_sanitize(project_config), filename=project_conf_file)
diff --git a/tests/elements/filter/basic/element_plugins/dynamic.py b/tests/elements/filter/basic/element_plugins/dynamic.py
index b4a556244..eb462ceb1 100644
--- a/tests/elements/filter/basic/element_plugins/dynamic.py
+++ b/tests/elements/filter/basic/element_plugins/dynamic.py
@@ -25,7 +25,7 @@ class DynamicElement(Element):
dep.stage_artifact(sandbox)
bstdata = self.get_public_data("bst")
- self.node_set_member(bstdata, "split-rules", self.split_rules)
+ bstdata["split-rules"] = self.split_rules
self.set_public_data("bst", bstdata)
return ""
diff --git a/tests/internals/yaml.py b/tests/internals/yaml.py
index 2f87d471b..4fb46cd89 100644
--- a/tests/internals/yaml.py
+++ b/tests/internals/yaml.py
@@ -127,7 +127,7 @@ def test_node_set(datafiles):
base = _yaml.load(filename)
assert 'mother' not in base
- _yaml.node_set(base, 'mother', 'snow white')
+ base['mother'] = 'snow white'
assert base.get_scalar('mother').as_str() == 'snow white'
@@ -142,12 +142,12 @@ def test_node_set_overwrite(datafiles):
# Overwrite a string
assert base.get_scalar('kind').as_str() == 'pony'
- _yaml.node_set(base, 'kind', 'cow')
+ base['kind'] = 'cow'
assert base.get_scalar('kind').as_str() == 'cow'
# Overwrite a list as a string
assert _yaml.node_get(base, list, 'moods') == ['happy', 'sad']
- _yaml.node_set(base, 'moods', 'unemotional')
+ base['moods'] = 'unemotional'
assert base.get_scalar('moods').as_str() == 'unemotional'
diff --git a/tests/sources/git.py b/tests/sources/git.py
index e4c6111f1..25da5c225 100644
--- a/tests/sources/git.py
+++ b/tests/sources/git.py
@@ -785,7 +785,7 @@ def test_git_describe(cli, tmpdir, datafiles, ref_storage, tag_type):
project = str(datafiles)
project_config = _yaml.load(os.path.join(project, 'project.conf'))
- _yaml.node_set(project_config, 'ref-storage', ref_storage)
+ project_config['ref-storage'] = ref_storage
_yaml.dump(_yaml.node_sanitize(project_config), os.path.join(project, 'project.conf'))
repofiles = os.path.join(str(tmpdir), 'repofiles')
@@ -899,7 +899,7 @@ def test_git_describe_head_is_tagged(cli, tmpdir, datafiles, ref_storage, tag_ty
project = str(datafiles)
project_config = _yaml.load(os.path.join(project, 'project.conf'))
- _yaml.node_set(project_config, 'ref-storage', ref_storage)
+ project_config['ref-storage'] = ref_storage
_yaml.dump(_yaml.node_sanitize(project_config), os.path.join(project, 'project.conf'))
repofiles = os.path.join(str(tmpdir), 'repofiles')
@@ -1014,7 +1014,7 @@ def test_git_describe_relevant_history(cli, tmpdir, datafiles):
project = str(datafiles)
project_config = _yaml.load(os.path.join(project, 'project.conf'))
- _yaml.node_set(project_config, 'ref-storage', 'project.refs')
+ project_config['ref-storage'] = 'project.refs'
_yaml.dump(_yaml.node_sanitize(project_config), os.path.join(project, 'project.conf'))
repofiles = os.path.join(str(tmpdir), 'repofiles')
@@ -1094,7 +1094,7 @@ def test_default_do_not_track_tags(cli, tmpdir, datafiles):
project = str(datafiles)
project_config = _yaml.load(os.path.join(project, 'project.conf'))
- _yaml.node_set(project_config, 'ref-storage', 'inline')
+ project_config['ref-storage'] = 'inline'
_yaml.dump(_yaml.node_sanitize(project_config), os.path.join(project, 'project.conf'))
repofiles = os.path.join(str(tmpdir), 'repofiles')
@@ -1151,17 +1151,17 @@ def test_overwrite_rogue_tag_multiple_remotes(cli, tmpdir, datafiles):
repodir, reponame = os.path.split(repo.repo)
project_config = _yaml.load(os.path.join(project, 'project.conf'))
- _yaml.node_set(project_config, 'aliases', _yaml.new_node_from_dict({
+ project_config['aliases'] = _yaml.new_node_from_dict({
'repo': 'http://example.com/'
- }))
- _yaml.node_set(project_config, 'mirrors', [
+ })
+ project_config['mirrors'] = [
{
'name': 'middle-earth',
'aliases': {
'repo': ['file://{}/'.format(repodir)]
}
}
- ])
+ ]
_yaml.dump(_yaml.node_sanitize(project_config), os.path.join(project, 'project.conf'))
repo.add_annotated_tag('tag', 'tag')
diff --git a/tests/sources/previous_source_access.py b/tests/sources/previous_source_access.py
index 50bfe383c..916cd5a6f 100644
--- a/tests/sources/previous_source_access.py
+++ b/tests/sources/previous_source_access.py
@@ -25,7 +25,7 @@ def test_custom_transform_source(cli, datafiles):
project_config_path = os.path.join(project, "project.conf")
project_config = _yaml.load(project_config_path)
aliases = project_config.get_mapping("aliases")
- _yaml.node_set(aliases, "project_dir", "file://{}".format(project))
+ aliases["project_dir"] = "file://{}".format(project)
_yaml.dump(project_config, project_config_path)
# Ensure we can track