diff --git a/tests/test_model.py b/tests/test_model.py index 89d406984b32b6658b5e0828c1cd753e41635adb..759a32718a1413c79fedcd726ff4f4c32890bf34 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -123,10 +123,10 @@ def instance(data_model): def test_context(data_model): assert len(Context.implement) == 3 - assert Context.module_set_id() == "b6d7e0614440c5ad8a7370fe46c777254d331983" - tid = Context._last_revision("test") - stid = Context._last_revision("subtest") - tbid = Context._last_revision("testb") + assert data_model.module_set_id() == "b6d7e0614440c5ad8a7370fe46c777254d331983" + tid = Context.last_revision("test") + stid = Context.last_revision("subtest") + tbid = Context.last_revision("testb") assert Context.modules[tid].argument == "test" assert Context.translate_pname("t:foo", tbid) == ("foo", "test") assert Context.translate_pname("sd:foo", stid) == ("foo", "defs") diff --git a/yangson/context.py b/yangson/context.py index 42369332bcc96b8ae67c879db9046bc75f917177..ba2e74a70c45cea0f3059189ae63fd5fc113510c 100644 --- a/yangson/context.py +++ b/yangson/context.py @@ -1,7 +1,5 @@ -import hashlib -import re from typing import Dict, List, MutableSet -from .constants import pname_re, YangsonException +from .constants import YangsonException from .parser import Parser, ParserException from .statement import ModuleParser, Statement from .typealiases import * @@ -11,11 +9,6 @@ from .typealiases import * class Context: """Global repository of data model structures and utility methods.""" - # Regular expressions - not_re = re.compile(r"not\s+") - and_re = re.compile(r"\s+and\s+") - or_re = re.compile(r"\s+or\s+") - @classmethod def initialize(cls) -> None: """Initialize the context variables.""" @@ -60,7 +53,7 @@ class Context: if "feature" in item: cls.features.update( [ (f,name) for f in item["feature"] ]) - rev = item["revision"] if item["revision"] else None + rev = item["revision"] mid = (name, rev) ct = item["conformance-type"] if ct == "implement": cls.implement.append(name) @@ -72,7 +65,7 @@ class Context: for s in item["submodules"]["submodule"]: sname = s["name"] cls.ns_map[sname] = name - rev = s["revision"] if s["revision"] else None + rev = s["revision"] smid = (sname, rev) if ct == "implement": cls.implement.append(sname) cls.revisions.setdefault(sname, []).append(rev) @@ -95,13 +88,6 @@ class Context: cls.schema._post_process() cls.schema._make_schema_patterns() - @classmethod - def module_set_id(cls): - """Return numeric id of the current set of modules.""" - fnames = ["@".join(m) for m in cls.modules.keys()] - fnames.sort() - return hashlib.sha1("".join(fnames).encode("ascii")).hexdigest() - @classmethod def _load_module(cls, name: YangIdentifier, rev: RevisionDate) -> Statement: @@ -120,7 +106,8 @@ class Context: raise ModuleNotFound(name, rev) @classmethod - def _last_revision(cls, mname: YangIdentifier) -> ModuleId: + def last_revision(cls, mname: YangIdentifier) -> ModuleId: + """Return last revision of a module that's part of the data model.""" return (mname, cls.revisions[mname][-1]) @classmethod @@ -137,7 +124,7 @@ class Context: if rev in cls.revisions[impn]: imid = (impn, rev) elif rev is None: # use last revision - imid = cls._last_revision(impn) + imid = cls.last_revision(impn) else: raise ModuleNotFound(impn, rev) cls.prefix_map[mid][prefix] = imid diff --git a/yangson/datamodel.py b/yangson/datamodel.py index e6f6f29bc9a713e9af0a9858ad0bf061857489cf..546b85cfb0799e93f2b15ccba734d08ea12af8cb 100644 --- a/yangson/datamodel.py +++ b/yangson/datamodel.py @@ -1,3 +1,4 @@ +import hashlib import json from typing import Dict, List, Optional from .constants import YangsonException @@ -26,7 +27,14 @@ class DataModel: raise BadYangLibraryData() from None Context.from_yang_library(yl, mod_path) - def from_raw(self, robj: RawObject) -> RootNode: + @staticmethod + def module_set_id(): + """Return numeric id of the current set of modules.""" + fnames = sorted(["@".join(m) for m in Context.modules.keys()]) + return hashlib.sha1("".join(fnames).encode("ascii")).hexdigest() + + @staticmethod + def from_raw(robj: RawObject) -> RootNode: """Return an instance created from a raw data tree. :param robj: a dictionary representing raw data tree @@ -34,7 +42,8 @@ class DataModel: cooked = Context.schema.from_raw(robj) return RootNode(cooked, Context.schema, cooked.timestamp) - def get_schema_node(self, path: SchemaPath) -> Optional[SchemaNode]: + @staticmethod + def get_schema_node(path: SchemaPath) -> Optional[SchemaNode]: """Return the schema node corresponding to `path`. :param path: schema path @@ -42,7 +51,8 @@ class DataModel: """ return Context.schema.get_schema_descendant(Context.path2route(path)) - def get_data_node(self, path: SchemaPath) -> Optional[DataNode]: + @staticmethod + def get_data_node(path: SchemaPath) -> Optional[DataNode]: """Return the data node corresponding to `path`. :param path: data path @@ -55,7 +65,8 @@ class DataModel: if node is None: return None return node - def parse_instance_id(self, iid: str) -> InstancePath: + @staticmethod + def parse_instance_id(iid: str) -> InstancePath: """Parse instance identifier. :param iid: instance identifier string @@ -65,7 +76,8 @@ class DataModel: """ return InstanceIdParser(iid).parse() - def parse_resource_id(self, rid: str) -> InstancePath: + @staticmethod + def parse_resource_id(rid: str) -> InstancePath: """Parse RESTCONF data resource identifier. :param rid: data resource identifier @@ -77,7 +89,8 @@ class DataModel: """ return ResourceIdParser(rid).parse() - def ascii_tree(self) -> str: + @staticmethod + def ascii_tree() -> str: """Return ascii-art representation of the main data tree.""" return Context.schema._ascii_tree("")