This is an automated email from the ASF dual-hosted git repository. tvb pushed a commit to branch bschubert/more-mypy in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit ee17afe82dea94fe38008fadf2458a0015cbd95e Author: Benjamin Schubert <[email protected]> AuthorDate: Thu Oct 10 18:31:25 2019 +0100 element.py: change 'substitute_variables' to take a 'ScalarNode' Previously 'substitute_variable' would take a str, which would prevent us from doing nice error reporting. Using a 'ScalarNode' allows us to get our errors nicely. Also rename it to 'node_substitute_variables'. --- src/buildstream/buildelement.py | 4 ++-- src/buildstream/element.py | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/buildstream/buildelement.py b/src/buildstream/buildelement.py index b33acfe..08b0794 100644 --- a/src/buildstream/buildelement.py +++ b/src/buildstream/buildelement.py @@ -281,9 +281,9 @@ class BuildElement(Element): # Private Local Methods # ############################################################# def __get_commands(self, node, name): - raw_commands = node.get_str_list(name, []) + raw_commands = node.get_sequence(name, []) return [ - self.substitute_variables(command) + self.node_substitute_variables(command) for command in raw_commands ] diff --git a/src/buildstream/element.py b/src/buildstream/element.py index 966f0f7..4e579ff 100644 --- a/src/buildstream/element.py +++ b/src/buildstream/element.py @@ -112,7 +112,7 @@ from .storage._casbaseddirectory import CasBasedDirectory from .storage.directory import VirtualDirectoryError if TYPE_CHECKING: - from .node import MappingNode + from .node import MappingNode, ScalarNode from .types import SourceRef from typing import Set, Tuple @@ -536,8 +536,24 @@ class Element(Plugin): return None - def substitute_variables(self, value): - return self.__variables.subst(value) + def node_substitute_variables(self, node: 'ScalarNode'): + """Replace any variables in the string contained in the node and returns it. + + Args: + node: A ScalarNode loaded from YAML + + Returns: + The value with all variables replaced + + **Example:** + + .. code:: python + + # Expect a string 'name' in 'node', substituting any + # variables in the returned string + name = self.node_substitute_variables(node.get_str('name')) + """ + return self.__variables.subst(node.as_str()) def node_subst_member(self, node: 'MappingNode[str, Any]', member_name: str, default: str = _node_sentinel) -> Any: """Fetch the value of a string node member, substituting any variables @@ -877,9 +893,9 @@ class Element(Plugin): if bstdata is not None: with sandbox.batch(SandboxFlags.NONE): - commands = bstdata.get_str_list('integration-commands', []) + commands = bstdata.get_sequence('integration-commands', []) for command in commands: - cmd = self.substitute_variables(command) + cmd = self.node_substitute_variables(command) sandbox.run(['sh', '-e', '-c', cmd], 0, env=environment, cwd='/', label=cmd)
