diff options
author | bst-marge-bot <marge-bot@buildstream.build> | 2019-06-06 16:48:33 +0000 |
---|---|---|
committer | bst-marge-bot <marge-bot@buildstream.build> | 2019-06-06 16:48:33 +0000 |
commit | 492b2ad16419c77a6ab33b7608897f5863ff1104 (patch) | |
tree | 5ffe62bc0a4e0ec40f59757e532c03c3bf575e63 | |
parent | 1e3ef2d7a9885a5514cf66d12df5890a3c6e04b0 (diff) | |
parent | d1748ec332a41c8d03a852f0a906bae00104001a (diff) | |
download | buildstream-492b2ad16419c77a6ab33b7608897f5863ff1104.tar.gz |
Merge branch 'bschubert/optimize-loader-types' into 'master'
Optimize _loader/types.py
See merge request BuildStream/buildstream!1379
-rw-r--r-- | .pylintrc | 1 | ||||
-rwxr-xr-x | setup.py | 1 | ||||
-rw-r--r-- | src/buildstream/_loader/types.pyx (renamed from src/buildstream/_loader/types.py) | 40 | ||||
-rw-r--r-- | src/buildstream/_yaml.pxd | 1 | ||||
-rw-r--r-- | src/buildstream/_yaml.pyx | 13 |
5 files changed, 35 insertions, 21 deletions
@@ -5,6 +5,7 @@ # run arbitrary code extension-pkg-whitelist= buildstream._loader._loader, + buildstream._loader.types, buildstream._variables, buildstream._yaml @@ -399,6 +399,7 @@ def register_cython_module(module_name, dependencies=None): BUILD_EXTENSIONS = [] register_cython_module("buildstream._loader._loader") +register_cython_module("buildstream._loader.types", dependencies=["buildstream._yaml"]) register_cython_module("buildstream._yaml") register_cython_module("buildstream._variables", dependencies=["buildstream._yaml"]) diff --git a/src/buildstream/_loader/types.py b/src/buildstream/_loader/types.pyx index f9dd38ca0..d3cd06253 100644 --- a/src/buildstream/_loader/types.py +++ b/src/buildstream/_loader/types.pyx @@ -18,7 +18,7 @@ # Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> from .._exceptions import LoadError, LoadErrorReason -from .. import _yaml +from .. cimport _yaml # Symbol(): @@ -51,32 +51,42 @@ class Symbol(): # A simple object describing a dependency # # Args: -# name (str): The element name +# name (str or Node): The element name # dep_type (str): The type of dependency, can be # Symbol.ALL, Symbol.BUILD, or Symbol.RUNTIME # junction (str): The element name of the junction, or None -# provenance (Provenance): The YAML node provenance of where this -# dependency was declared +# provenance (ProvenanceInformation): The YAML node provenance of where this +# dependency was declared # -class Dependency(): - def __init__(self, dep, provenance, default_dep_type=None): +cdef class Dependency: + cdef public _yaml.ProvenanceInformation provenance + cdef public str name + cdef public str dep_type + cdef public str junction + + def __init__(self, + object dep, + _yaml.ProvenanceInformation provenance, + str default_dep_type=None): + cdef str dep_type + self.provenance = provenance - if isinstance(dep, str): - self.name = dep + if type(dep) is str: + self.name = <str> dep self.dep_type = default_dep_type self.junction = None - elif _yaml.is_node(dep): + elif type(dep) is _yaml.Node and type(dep.value) is dict: if default_dep_type: - _yaml.node_validate(dep, ['filename', 'junction']) + _yaml.node_validate(<_yaml.Node> dep, ['filename', 'junction']) dep_type = default_dep_type else: - _yaml.node_validate(dep, ['filename', 'type', 'junction']) + _yaml.node_validate(<_yaml.Node> dep, ['filename', 'type', 'junction']) # Make type optional, for this we set it to None - dep_type = _yaml.node_get(dep, str, Symbol.TYPE, default_value=None) - if dep_type is None or dep_type == Symbol.ALL: + dep_type = <str> _yaml.node_get(<_yaml.Node> dep, str, <str> Symbol.TYPE, None, None) + if dep_type is None or dep_type == <str> Symbol.ALL: dep_type = None elif dep_type not in [Symbol.BUILD, Symbol.RUNTIME]: provenance = _yaml.node_get_provenance(dep, key=Symbol.TYPE) @@ -84,9 +94,9 @@ class Dependency(): "{}: Dependency type '{}' is not 'build', 'runtime' or 'all'" .format(provenance, dep_type)) - self.name = _yaml.node_get(dep, str, Symbol.FILENAME) + self.name = <str> _yaml.node_get(<_yaml.Node> dep, str, <str> Symbol.FILENAME) self.dep_type = dep_type - self.junction = _yaml.node_get(dep, str, Symbol.JUNCTION, default_value=None) + self.junction = <str> _yaml.node_get(<_yaml.Node> dep, str, <str> Symbol.JUNCTION, None, None) else: raise LoadError(LoadErrorReason.INVALID_DATA, diff --git a/src/buildstream/_yaml.pxd b/src/buildstream/_yaml.pxd index 27a1a888e..6a12fa7b3 100644 --- a/src/buildstream/_yaml.pxd +++ b/src/buildstream/_yaml.pxd @@ -39,6 +39,7 @@ cdef class ProvenanceInformation: cpdef object node_get(Node node, object expected_type, str key, list indices=*, object default_value=*, bint allow_none=*) +cpdef void node_validate(Node node, list valid_keys) except * cpdef void node_set(Node node, object key, object value, list indices=*) except * cpdef list node_keys(object node) cpdef ProvenanceInformation node_get_provenance(Node node, str key=*, list indices=*) diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx index 4505e2f95..f14b55e51 100644 --- a/src/buildstream/_yaml.pyx +++ b/src/buildstream/_yaml.pyx @@ -1119,16 +1119,17 @@ cpdef object node_sanitize(object node, object dict_type=OrderedDict): # LoadError: In the case that the specified node contained # one or more invalid keys # -def node_validate(Node node, list valid_keys): +cpdef void node_validate(Node node, list valid_keys) except *: # Probably the fastest way to do this: https://stackoverflow.com/a/23062482 cdef set valid_keys_set = set(valid_keys) - invalid = next((key for key in node.value if key not in valid_keys_set), None) + cdef str key - if invalid: - provenance = node_get_provenance(node, key=invalid) - raise LoadError(LoadErrorReason.INVALID_DATA, - "{}: Unexpected key: {}".format(provenance, invalid)) + for key in node.value: + if key not in valid_keys_set: + provenance = node_get_provenance(node, key=key) + raise LoadError(LoadErrorReason.INVALID_DATA, + "{}: Unexpected key: {}".format(provenance, key)) # Node copying |