From f40d2d0b2bca79a5d2ec83073f879c2b10b75f28 Mon Sep 17 00:00:00 2001 From: Rob Shakir Date: Wed, 16 Mar 2016 12:06:05 -0700 Subject: [PATCH] Cut version 0.3.2 for release to PyPI. * (M) pyangbind/lib/* - update files to fix PEP8 compliance issues. * (M) setup.py - update version. * (M) tests/* - update files for PEP8 compliance. --- .gitignore | 1 + CHANGELOG.md | 3 + pyangbind/lib/serialise.py | 57 +++++--- pyangbind/lib/xpathhelper.py | 6 +- pyangbind/lib/yangtypes.py | 13 +- pyangbind/plugin/pybind.py | 19 ++- setup.py | 2 +- tests/extmethods/run.py | 16 ++- tests/identityref/run.py | 5 +- tests/include-import/run.py | 2 +- tests/list/run.py | 7 +- tests/pep8_chk.sh | 13 ++ tests/rpc/run.py | 2 +- tests/serialise/ietf-json-deserialise/run.py | 7 +- tests/serialise/ietf-json-serialise/run.py | 9 +- tests/serialise/json-deserialise/run.py | 41 +++--- tests/serialise/json-serialise/run.py | 5 +- tests/serialise/juniper-json-examples/run.py | 141 ++++++++++--------- tests/string/run.py | 2 +- tests/xpath/01-list_leaflist/run.py | 1 - 20 files changed, 211 insertions(+), 141 deletions(-) create mode 100644 tests/pep8_chk.sh diff --git a/.gitignore b/.gitignore index fe2c62fc..ca6937ba 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ dist/* tests/pyvirtualenv dev/* tmp/* +*.PEP8-ERRORS \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d7d1dbd..753aef3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,3 +40,6 @@ WARNING). * Adds support for carrying the namespace and defined module in a class. * Adds a metadata element to carry JSON metadata that was received. + +16-03-2016 - 0.3.2 + * Fixes an issue relating to to identity value inheritance. \ No newline at end of file diff --git a/pyangbind/lib/serialise.py b/pyangbind/lib/serialise.py index 92d48e20..47cbb239 100644 --- a/pyangbind/lib/serialise.py +++ b/pyangbind/lib/serialise.py @@ -32,6 +32,7 @@ import sys + class pybindJSONIOError(Exception): pass @@ -39,6 +40,7 @@ class pybindJSONIOError(Exception): class pybindJSONUpdateError(Exception): pass + class pybindJSONDecodeError(Exception): pass @@ -86,7 +88,7 @@ def default(self, obj, mode="default"): if isinstance(obj, dict): ndict = {} - for k,v in obj.iteritems(): + for k, v in obj.iteritems(): ndict[k] = self.default(v, mode=mode) return ndict @@ -190,9 +192,9 @@ def default(self, obj, mode="default"): return [self.default(i) for i in obj] - raise AttributeError("Unmapped type: %s" % original_yang_type) + class pybindJSONDecoder(object): @staticmethod def load_json(d, parent, yang_base, obj=None, path_helper=None, @@ -231,21 +233,23 @@ def load_json(d, parent, yang_base, obj=None, path_helper=None, for elem in chobj._pyangbind_elements: unsetchildelem = getattr(chobj, "_unset_%s" % elem) unsetchildelem() - pybindJSONDecoder.load_json(d[key], chobj, yang_base, obj=chobj, path_helper=path_helper) + pybindJSONDecoder.load_json(d[key], chobj, yang_base, obj=chobj, + path_helper=path_helper) set_via_stdmethod = False elif pybind_attr in ["YANGListType", "list"]: # we need to add each key to the list and then skip a level in the # JSON hierarchy list_obj = getattr(obj, safe_name(key), None) if list_obj is None: - raise pybindJSONDecodeError("Could not load list object with name %s" % key) + raise pybindJSONDecodeError("Could not load list object " + + "with name %s" % key) ordered_list = getattr(list_obj, "_ordered", None) if ordered_list: # Put keys in order: okeys = [] kdict = {} - for k,v in d[key].iteritems(): - if not "__yang_order" in v: + for k, v in d[key].iteritems(): + if "__yang_order" not in v: # Element is not specified in terms of order, so # push to a list that keeps this order okeys.append(k) @@ -264,7 +268,8 @@ def load_json(d, parent, yang_base, obj=None, path_helper=None, if child_key not in chobj: chobj.add(child_key) parent = chobj[child_key] - pybindJSONDecoder.load_json(d[key][child_key], parent, yang_base, obj=parent, path_helper=path_helper) + pybindJSONDecoder.load_json(d[key][child_key], parent, yang_base, + obj=parent, path_helper=path_helper) set_via_stdmethod = False if overwrite: for child_key in chobj: @@ -305,8 +310,8 @@ def load_json(d, parent, yang_base, obj=None, path_helper=None, def check_metadata_add(key, data, obj): keys = [unicode(k) for k in data] if ("@" + key) in keys: - for k,v in data["@" + key].iteritems(): - obj._add_metadata(k,v) + for k, v in data["@" + key].iteritems(): + obj._add_metadata(k, v) @staticmethod def load_ietf_json(d, parent, yang_base, obj=None, path_helper=None, @@ -340,8 +345,8 @@ def load_ietf_json(d, parent, yang_base, obj=None, path_helper=None, if key == "@": # Handle whole container metadata object - for k,v in d[key].iteritems(): - obj._add_metadata(k,v) + for k, v in d[key].iteritems(): + obj._add_metadata(k, v) continue elif "@" in key: # Don't handle metadata elements, each element @@ -356,7 +361,9 @@ def load_ietf_json(d, parent, yang_base, obj=None, path_helper=None, if attr_get is None: raise AttributeError("Invalid attribute specified (%s)" % ykey) pybindJSONDecoder.check_metadata_add(key, d, attr_get()) - pybindJSONDecoder.load_ietf_json(d[key], None, None, obj=attr_get(), path_helper=path_helper, extmethods=extmethods, overwrite=overwrite) + pybindJSONDecoder.load_ietf_json(d[key], None, None, obj=attr_get(), + path_helper=path_helper, extmethods=extmethods, + overwrite=overwrite) elif isinstance(d[key], list): for elem in d[key]: # if this is a list, then this is a YANG list @@ -372,13 +379,15 @@ def load_ietf_json(d, parent, yang_base, obj=None, path_helper=None, nobj = this_attr[k] elif " " in this_attr._keyval: kwargs = {} - for pkv,ykv in zip(this_attr._keyval.split(" "), this_attr._yang_keys.split(" ")): + for pkv, ykv in zip(this_attr._keyval.split(" "), + this_attr._yang_keys.split(" ")): kwargs[pkv] = elem[ykv] nobj = this_attr.add(**kwargs) - #pybindJSONDecoder.load_ietf_json(elem, None, None, obj=nobj, path_helper=path_helper, extmethods=extmethods, overwrite=overwrite) else: nobj = this_attr.add(elem[this_attr._yang_keys]) - pybindJSONDecoder.load_ietf_json(elem, None, None, obj=nobj, path_helper=path_helper, extmethods=extmethods, overwrite=overwrite) + pybindJSONDecoder.load_ietf_json(elem, None, None, obj=nobj, + path_helper=path_helper, extmethods=extmethods, + overwrite=overwrite) pybindJSONDecoder.check_metadata_add(key, d, nobj) else: std_method_set = True @@ -396,11 +405,13 @@ def load_ietf_json(d, parent, yang_base, obj=None, path_helper=None, else: set_method = getattr(obj, "_set_%s" % safe_name(ykey), None) if set_method is None: - raise AttributeError("Invalid attribute specified in JSON - %s" % (ykey)) + raise AttributeError("Invalid attribute specified in JSON - %s" + % (ykey)) set_method(d[key]) pybindJSONDecoder.check_metadata_add(key, d, get_method()) return obj + class pybindIETFJSONEncoder(pybindJSONEncoder): @staticmethod def generate_element(obj, parent_namespace=None, flt=False): @@ -415,7 +426,7 @@ def generate_element(obj, parent_namespace=None, flt=False): for element_name in obj._pyangbind_elements: element = getattr(obj, element_name, None) yang_name = getattr(element, "yang_name", None) - yname = yang_name() if not yang_name is None else element_name + yname = yang_name() if yang_name is not None else element_name if not element._namespace == parent_namespace: # if the namespace is different, then precede with the module @@ -423,10 +434,13 @@ def generate_element(obj, parent_namespace=None, flt=False): yname = "%s:%s" % (element._defining_module, yname) generated_by = getattr(element, "_pybind_generated_by", None) - if generated_by == "container": - d[yname] = pybindIETFJSONEncoder.generate_element(element, parent_namespace=element._namespace, flt=flt) + if generated_by == "container": + d[yname] = pybindIETFJSONEncoder.generate_element(element, + parent_namespace=element._namespace, flt=flt) elif generated_by == "YANGListType": - d[yname] = [pybindIETFJSONEncoder.generate_element(i, parent_namespace=element._namespace, flt=flt) for i in element._members.itervalues()] + d[yname] = [pybindIETFJSONEncoder.generate_element(i, + parent_namespace=element._namespace, flt=flt) + for i in element._members.itervalues()] else: if flt and element._changed(): d[yname] = element @@ -435,7 +449,8 @@ def generate_element(obj, parent_namespace=None, flt=False): return d def encode(self, obj): - return json.JSONEncoder.encode(self, self._preprocess_element(obj, mode="ietf")) + return json.JSONEncoder.encode(self, + self._preprocess_element(obj, mode="ietf")) def default(self, obj, mode="ietf"): return pybindJSONEncoder().default(obj, mode="ietf") diff --git a/pyangbind/lib/xpathhelper.py b/pyangbind/lib/xpathhelper.py index 4113b625..329da79a 100644 --- a/pyangbind/lib/xpathhelper.py +++ b/pyangbind/lib/xpathhelper.py @@ -91,8 +91,8 @@ def get(self, path, caller=False): class YANGPathHelper(PybindXpathHelper): _attr_re = re.compile("^(?P[^\[]+)(?P(\[[^\]]+\])+)$") - _arg_re = re.compile("^((and|or) )?[@]?(?P[a-zA-Z0-9\-\_:]+)([ ]+)?=([ ]+)?" + - "[\'\"]?(?P[^ ^\'^\"]+)([\'\"])?([ ]+)?" + + _arg_re = re.compile("^((and|or) )?[@]?(?P[a-zA-Z0-9\-\_:]+)([ ]+)?" + + "=([ ]+)?[\'\"]?(?P[^ ^\'^\"]+)([\'\"])?([ ]+)?" + "(?P.*)") _relative_path_re = re.compile("^(\.|\.\.)") @@ -200,7 +200,7 @@ def _tagname_attributes(self, tag, normalise_namespace=True): attributes[c] = a tmp_arg = r else: - raise XPathError("invalid attribute string specified" + + raise XPathError("invalid attribute string specified" + "for %s" % tagname + "(err part: %s (%s))" % (arg, tmp_arg)) return (tagname, attributes) diff --git a/pyangbind/lib/yangtypes.py b/pyangbind/lib/yangtypes.py index d6df59ce..05575377 100644 --- a/pyangbind/lib/yangtypes.py +++ b/pyangbind/lib/yangtypes.py @@ -82,6 +82,7 @@ def safe_name(arg): # so that we can retrieve it when get() is called. return arg + def RestrictedPrecisionDecimalType(*args, **kwargs): """ Function to return a new type that is based on decimal.Decimal with @@ -854,7 +855,8 @@ def YANGDynClass(*args, **kwargs): rpath = [] chk_path = "/" + "/".join(remove_path_attributes(rpath)) if chk_path in extmethods: - for method in [i for i in dir(extmethods[chk_path]) if not i.startswith("_")]: + for method in [i for i in dir(extmethods[chk_path]) + if not i.startswith("_")]: clsslots.append("_" + method) class YANGBaseClass(base_type): @@ -888,9 +890,11 @@ def __init__(self, *args, **kwargs): self._metadata = {} if self._extmethods: - chk_path = "/" + "/".join(remove_path_attributes(self._register_path())) + chk_path = \ + "/" + "/".join(remove_path_attributes(self._register_path())) if chk_path in self._extmethods: - for method in [i for i in dir(self._extmethods[chk_path]) if not i.startswith("_")]: + for method in [i for i in dir(self._extmethods[chk_path]) if + not i.startswith("_")]: # Don't allow methods to be overwritten if hasattr(self, "_" + method): continue @@ -902,7 +906,7 @@ def __init__(self, *args, **kwargs): if default: self._default = default if len(args): - if not self._default is False: + if self._default is not False: if not args[0] == self._default: self._set() else: @@ -1099,7 +1103,6 @@ def __init__(self, *args, **kwargs): self._type = re.sub("<(type|class) '(?P.*)'>", "\g", str(get_method()._base_type)) self._ptr = True - #self._referenced_object = path_chk[0] elif self._require_instance: if not value: self._referenced_object = None diff --git a/pyangbind/plugin/pybind.py b/pyangbind/plugin/pybind.py index 10661beb..dc1c25fc 100644 --- a/pyangbind/plugin/pybind.py +++ b/pyangbind/plugin/pybind.py @@ -262,7 +262,8 @@ def build_pybind(ctx, modules, fd): ctx.pybind_common_hdr += "from operator import attrgetter\n" if ctx.opts.use_xpathhelper: - ctx.pybind_common_hdr += "import pyangbind.lib.xpathhelper as xpathhelper\n" + ctx.pybind_common_hdr += "import pyangbind.lib.xpathhelper as " + \ + "xpathhelper\n" ctx.pybind_common_hdr += """from pyangbind.lib.yangtypes import """ ctx.pybind_common_hdr += """RestrictedPrecisionDecimalType, """ ctx.pybind_common_hdr += """RestrictedClassType, TypedListType\n""" @@ -358,11 +359,14 @@ def build_pybind(ctx, modules, fd): get_children(ctx, fd, rpcs, module, module, register_paths=False, path="/%s_rpc" % (safe_name(module.arg))) + def build_identities(ctx, defnd): def find_all_identity_values(item, definitions, values=list()): - new_values = [k for k in definitions[item] if not k in ["@module", "@namespace"] and not k in values] + new_values = [k for k in definitions[item] if k not + in ["@module", "@namespace"] and k not in values] for v in new_values: - values.extend([i for i in find_all_identity_values(v, definitions, values=values) if not i in values]) + values.extend([i for i in find_all_identity_values(v, definitions, + values=values) if i not in values]) values.extend(new_values) return values @@ -448,9 +452,10 @@ def find_all_identity_values(item, definitions, values=list()): for identity in orig_identity_d: vals = find_all_identity_values(identity, orig_identity_d, values=[]) for value in vals: - if not value in orig_identity_d[identity]: - identity_d[identity][value] = {k: v for k,v in - orig_identity_d[value].iteritems() if k in ["@module", "@namespace"]} + if value not in orig_identity_d[identity]: + identity_d[identity][value] = {k: v for k, v in + orig_identity_d[value].iteritems() if k in + ["@module", "@namespace"]} # Add entries to the class_map such that this identity can be referenced by # elements that use this identity ref. @@ -1280,7 +1285,7 @@ def get_element(ctx, fd, element, module, parent, path, # Find element's namespace namespace = element.i_orig_module.search_one("namespace").arg \ - if hasattr(element, "i_orig_module") else None + if hasattr(element, "i_orig_module") else None defining_module = element.i_orig_module.arg if \ hasattr(element, "i_orig_module") else None diff --git a/setup.py b/setup.py index 0d5da207..2fc67df2 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ # PyangBind uses the same versioning approach as OpenConfig - see # http://www.openconfig.net/file-cabinet/Semantic_Versioning_for_OpenConfig.pdf?attredirects=0&d=1 - version='0.3.1', + version='0.3.2', description="PyangBind is a plugin for pyang which converts YANG data" + \ "models into a Python class hierarchy, such that Python " + \ diff --git a/tests/extmethods/run.py b/tests/extmethods/run.py index 72a0f601..85e90c8a 100755 --- a/tests/extmethods/run.py +++ b/tests/extmethods/run.py @@ -6,6 +6,7 @@ TESTNAME = "extmethods" + class extmethodcls(object): def commit(self, *args, **kwargs): return "COMMIT_CALLED" @@ -72,17 +73,22 @@ def main(): for chk in results: method = getattr(x.item.one, "_" + chk[0], None) - assert (method is not None) == chk[1], "Method %s retrieved incorrectly, method was: %s" % method + assert (method is not None) == chk[1], \ + "Method %s retrieved incorrectly, method was: %s" % method if method is not None: result = method() - assert result == chk[2], "Incorrect result returned from %s -> %s != %s" % (chk[0], result, chk[2]) + assert result == chk[2], "Incorrect return from %s -> %s != %s" \ + % (chk[0], result, chk[2]) - expected_return = {'args': ('one',), 'kwargs': {'caller': ['item', 'one'], 'two': 2, 'path_helper': False}} - assert x.item.one._echo('one', two=2) == expected_return, "args+kwargs not echoed correctly" + expected_return = {'args': ('one',), 'kwargs': {'caller': ['item', 'one'], + 'two': 2, 'path_helper': False}} + assert x.item.one._echo('one', two=2) == expected_return, \ + "args+kwargs not echoed correctly" try: x.item.two = False - assert False, "incorrectly set an attribute that did not exist in extmethods" + assert False, \ + "incorrectly set an attribute that did not exist in extmethods" except AttributeError: pass diff --git a/tests/identityref/run.py b/tests/identityref/run.py index 559291f5..374c469e 100755 --- a/tests/identityref/run.py +++ b/tests/identityref/run.py @@ -112,8 +112,8 @@ def main(): "id4 leaf was set incorrectly (%s: %s != %s)" % \ (k[0], k[1], passed) - for k in [("daughter", True), ("cousin", True), ("mother", True), ("aunt", True), - ("greatgrandmother", False)]: + for k in [("daughter", True), ("cousin", True), ("mother", True), + ("aunt", True), ("greatgrandmother", False)]: passed = True try: i.test_container.id5 = k[0] @@ -138,6 +138,5 @@ def main(): os.system("/bin/rm %s/bindings.pyc" % this_dir) - if __name__ == '__main__': main() diff --git a/tests/include-import/run.py b/tests/include-import/run.py index ffafa803..251ebfa7 100755 --- a/tests/include-import/run.py +++ b/tests/include-import/run.py @@ -31,7 +31,7 @@ def main(): assert pyangbindpath is not False, "could not resolve pyangbind directory" this_dir = os.path.dirname(os.path.realpath(__file__)) - + cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind -o %s/bindings.py" % this_dir diff --git a/tests/list/run.py b/tests/list/run.py index a15749b8..87ede130 100755 --- a/tests/list/run.py +++ b/tests/list/run.py @@ -71,7 +71,7 @@ def main(): test_instance.list_container.list_element[i] except: passed = False - assert passed == True, "could not look up a list element using the " + \ + assert passed is True, "could not look up a list element using the " + \ " type it was cast to" assert test_instance.list_container.list_element[1].keyval == 1, \ @@ -89,7 +89,7 @@ def main(): "aSecondDefaultValue" assert test_instance.get() == \ - {'list-container': {'list-eight': {}, 'list-seven': {}, + {'list-container': {'list-eight': {}, 'list-seven': {}, 'list-six': {}, 'list-five': {}, 'list-two': {}, 'list-three': {}, 'list-four': {}, 'list-element': {1: {'keyval': 1, 'another-value': 'defaultValue'}, @@ -134,7 +134,7 @@ def main(): test_instance.list_container.list_two.add(keyval=i) except KeyError: assert i in ["broccoli", "carrot"], "invalid item added to " + \ - "list using keyword add (%s)" % i + "list using keyword add (%s)" % i passed = False test_instance.list_container.list_element.add(22) @@ -177,7 +177,6 @@ def main(): "a key-less list did not have the correct value set (%s %d != 10)" % \ (x, test_instance.list_container.list_six[x].val) - y = test_instance.list_container.list_eight.add(val="value one", additional="value two") assert \ diff --git a/tests/pep8_chk.sh b/tests/pep8_chk.sh new file mode 100644 index 00000000..69020e77 --- /dev/null +++ b/tests/pep8_chk.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +TESTDIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +for path in pyangbind tests; do + for f in `find $TESTDIR/../$path -name "*.py" | grep -v "pyvirtualenv"`; do + PEP8ERROR=`pep8 $f |egrep -v "E(111|114|127|128)"` + if [[ ! -z $PEP8ERROR ]]; then + echo "$PEP8ERROR" > $f.PEP8-ERRORS; + fi + done +done + + diff --git a/tests/rpc/run.py b/tests/rpc/run.py index a486208d..1bdebeba 100755 --- a/tests/rpc/run.py +++ b/tests/rpc/run.py @@ -32,7 +32,7 @@ def main(): this_dir = os.path.dirname(os.path.realpath(__file__)) - cmd = "%s "% pythonpath + cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind " cmd += " --split-class-dir=%s/bindings" % this_dir diff --git a/tests/serialise/ietf-json-deserialise/run.py b/tests/serialise/ietf-json-deserialise/run.py index c26cba25..9545f702 100755 --- a/tests/serialise/ietf-json-deserialise/run.py +++ b/tests/serialise/ietf-json-deserialise/run.py @@ -37,7 +37,7 @@ def main(): this_dir = os.path.dirname(os.path.realpath(__file__)) - cmd = "%s "% pythonpath + cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind -o %s/bindings.py" % this_dir cmd += " -p %s" % this_dir @@ -59,7 +59,7 @@ def main(): pth = os.path.join(this_dir, "json", "skeylist.json") nobj = pybindJSONDecoder.load_ietf_json(json.load(open(pth, 'r')), bindings, "ietf_json_deserialise") - expected_get= {'skey': {u'one': {'leaf-one': u'one'}, u'three': + expected_get = {'skey': {u'one': {'leaf-one': u'one'}, u'three': {'leaf-one': u'three'}, u'two': {'leaf-one': u'two'}}} assert nobj.get(filter=True) == expected_get, "Single key list load did " + \ "not return expected JSON" @@ -74,7 +74,6 @@ def main(): assert nobj.get(filter=True) == expected_get, "List with children load " + \ "did not return expected JSON" - pth = os.path.join(this_dir, "json", "complete-obj.json") nobj = pybindJSONDecoder.load_ietf_json(json.load(open(pth, 'r')), bindings, "ietf_json_deserialise") @@ -88,7 +87,7 @@ def main(): "boolean": True, "binary": bitarray("111111"), "union": "16", - "identityref":"idone", + "identityref": "idone", "enumeration": "one", "k1": 1, "uint16": 1, diff --git a/tests/serialise/ietf-json-serialise/run.py b/tests/serialise/ietf-json-serialise/run.py index 770b0007..e6fb7a62 100755 --- a/tests/serialise/ietf-json-serialise/run.py +++ b/tests/serialise/ietf-json-serialise/run.py @@ -35,7 +35,7 @@ def main(): this_dir = os.path.dirname(os.path.realpath(__file__)) - cmd = "%s "% pythonpath + cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind -o %s/bindings.py" % this_dir cmd += " -p %s" % this_dir @@ -82,8 +82,11 @@ def main(): for i in range(1, 10): js.c1.l2.add(i) - pybind_json = json.loads(json.dumps(pybindIETFJSONEncoder.generate_element(js,flt=True), cls=pybindIETFJSONEncoder, indent=4)) - external_json = json.load(open(os.path.join(this_dir, "json", "obj.json"), 'r')) + pybind_json = json.loads(json.dumps( + pybindIETFJSONEncoder.generate_element(js, flt=True), + cls=pybindIETFJSONEncoder, indent=4)) + external_json = json.load( + open(os.path.join(this_dir, "json", "obj.json"), 'r')) assert pybind_json == external_json, "JSON did not match the expected output" diff --git a/tests/serialise/json-deserialise/run.py b/tests/serialise/json-deserialise/run.py index 9a931047..2634d43f 100755 --- a/tests/serialise/json-deserialise/run.py +++ b/tests/serialise/json-deserialise/run.py @@ -37,7 +37,7 @@ def main(): this_dir = os.path.dirname(os.path.realpath(__file__)) - cmd = "%s "% pythonpath + cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind -o %s/bindings.py" % this_dir cmd += " -p %s" % this_dir @@ -56,19 +56,26 @@ def main(): whole_obj = pbJ.load(os.path.join(this_dir, "json", "list.json"), bindings, "json_deserialise", path_helper=y) - expected_whole_obj = {'load-list': {u'1': {'index': 1, 'value': u'one'}, u'3': {'index': 3, 'value': u'three'}, u'2': {'index': 2, 'value': u'two'}}} - assert expected_whole_obj == whole_obj.get(filter=True), "Whole object load did not return the correct list" + expected_whole_obj = {'load-list': {u'1': {'index': 1, 'value': u'one'}, + u'3': {'index': 3, 'value': u'three'}, + u'2': {'index': 2, 'value': u'two'}}} + assert expected_whole_obj == whole_obj.get(filter=True), \ + "Whole object load did not return the correct list" del whole_obj js = bindings.json_deserialise(path_helper=y) # Load into an existing class - pbS.pybindJSONDecoder.load_json(json.load(open(os.path.join(this_dir, "json", "list-items.json"), 'r')), None, None, obj=js) - expected_get = {'load-list': {u'5': {'index': 5, 'value': u'five'}, u'4': {'index': 4, 'value': u'four'}}} - assert expected_get == js.get(filter=True), "Existing object load did not return the correct list" + pbS.pybindJSONDecoder.load_json(json.load( + open(os.path.join(this_dir, "json", "list-items.json"), 'r')), + None, None, obj=js) + expected_get = {'load-list': {u'5': {'index': 5, 'value': u'five'}, + u'4': {'index': 4, 'value': u'four'}}} + assert expected_get == js.get(filter=True), \ + "Existing object load did not return the correct list" del expected_get - all_items = pbJ.load(os.path.join(this_dir, "json", "alltypes.json"), bindings, - "json_deserialise", path_helper=y) + all_items = pbJ.load(os.path.join(this_dir, "json", "alltypes.json"), + bindings, "json_deserialise", path_helper=y) expected_get = { 'c1': { 'l1': { @@ -103,18 +110,20 @@ def main(): } } } - assert all_items.get(filter=True) == expected_get, "Load of object with all items not correct" + assert all_items.get(filter=True) == expected_get, \ + "Load of object with all items not correct" del js - js = pbJ.load(os.path.join(this_dir, "json", "orderedlist-order.json"), bindings, - "json_deserialise", path_helper=y) - assert js.ordered.keys() == ["two", "one"], "Did not correctly load a user ordered list" + js = pbJ.load(os.path.join(this_dir, "json", "orderedlist-order.json"), + bindings, "json_deserialise", path_helper=y) + assert js.ordered.keys() == ["two", "one"], \ + "Did not correctly load a user ordered list" del js - js = pbJ.load(os.path.join(this_dir, "json", "orderedlist-no-order.json"), bindings, - "json_deserialise", path_helper=y) - assert js.ordered.keys() == ["one", "two"], "Did not correctly follow ordering in JSON file" - + js = pbJ.load(os.path.join(this_dir, "json", "orderedlist-no-order.json"), + bindings, "json_deserialise", path_helper=y) + assert js.ordered.keys() == ["one", "two"], \ + "Did not correctly follow ordering in JSON file" if not k: os.system("/bin/rm %s/bindings.py" % this_dir) diff --git a/tests/serialise/json-serialise/run.py b/tests/serialise/json-serialise/run.py index ee738bfa..23b19cc1 100755 --- a/tests/serialise/json-serialise/run.py +++ b/tests/serialise/json-serialise/run.py @@ -35,7 +35,7 @@ def main(): this_dir = os.path.dirname(os.path.realpath(__file__)) - cmd = "%s "% pythonpath + cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind -o %s/bindings.py" % this_dir cmd += " -p %s" % this_dir @@ -78,7 +78,8 @@ def main(): js.c1.l2.add(i) pybind_json = json.loads(dumps(js)) - external_json = json.load(open(os.path.join(this_dir, "json", "expected-output.json"), 'r')) + external_json = json.load(open(os.path.join(this_dir, "json", + "expected-output.json"), 'r')) assert pybind_json == external_json, "JSON did not match the expected output" diff --git a/tests/serialise/juniper-json-examples/run.py b/tests/serialise/juniper-json-examples/run.py index 2714094f..43678764 100755 --- a/tests/serialise/juniper-json-examples/run.py +++ b/tests/serialise/juniper-json-examples/run.py @@ -35,18 +35,22 @@ def main(): assert pyangpath is not False, "could not find path to pyang" assert pyangbindpath is not False, "could not resolve pyangbind directory" + OC = "https://raw.githubusercontent.com/openconfig/" + \ + "public/master/release/models/" + RFC = "https://raw.githubusercontent.com/YangModels/" + \ + "yang/master/standard/ietf/RFC/" FETCH_FILES = [ - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/bgp/openconfig-bgp-multiprotocol.yang", "openconfig"), - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/bgp/openconfig-bgp-operational.yang", "openconfig"), - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/bgp/openconfig-bgp-policy.yang", "openconfig"), - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/bgp/openconfig-bgp-types.yang", "openconfig"), - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/bgp/openconfig-bgp.yang", "openconfig"), - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/policy/openconfig-routing-policy.yang", "openconfig"), - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/policy/openconfig-policy-types.yang", "openconfig"), - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/openconfig-extensions.yang", "include"), - ("https://raw.githubusercontent.com/openconfig/public/master/release/models/openconfig-types.yang", "include"), - ("https://raw.githubusercontent.com/YangModels/yang/master/standard/ietf/RFC/ietf-inet-types.yang", "include"), - ("https://raw.githubusercontent.com/YangModels/yang/master/standard/ietf/RFC/ietf-yang-types.yang", "include"), + (OC + "bgp/openconfig-bgp-multiprotocol.yang", "openconfig"), + (OC + "bgp/openconfig-bgp-operational.yang", "openconfig"), + (OC + "bgp/openconfig-bgp-policy.yang", "openconfig"), + (OC + "bgp/openconfig-bgp-types.yang", "openconfig"), + (OC + "bgp/openconfig-bgp.yang", "openconfig"), + (OC + "policy/openconfig-routing-policy.yang", "openconfig"), + (OC + "policy/openconfig-policy-types.yang", "openconfig"), + (OC + "openconfig-extensions.yang", "include"), + (OC + "openconfig-types.yang", "include"), + (RFC + "ietf-inet-types.yang", "include"), + (RFC + "ietf-yang-types.yang", "include"), ] this_dir = os.path.dirname(os.path.realpath(__file__)) @@ -55,7 +59,7 @@ def main(): wrdir = os.path.join(this_dir, fn[1]) if not os.path.exists(wrdir): os.mkdir(wrdir) - if not wrdir in del_dirs: + if wrdir not in del_dirs: del_dirs.append(wrdir) wrpath = os.path.join(this_dir, fn[1], fn[0].split("/")[-1]) if not os.path.exists(wrpath): @@ -65,15 +69,17 @@ def main(): f.write(response.content) f.close() - files_str = " ".join([os.path.join(this_dir, "openconfig", i) for i in os.listdir(os.path.join(this_dir, "openconfig"))]) + files_str = " ".join([os.path.join(this_dir, "openconfig", i) for i in + os.listdir(os.path.join(this_dir, "openconfig"))]) - cmd = "%s "% pythonpath + cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind --split-class-dir %s/ocbind" % this_dir cmd += " -p %s" % this_dir cmd += " -p %s" % os.path.join(this_dir, "include") cmd += files_str - # NB: use-xpathhelper is NOT specified here, so we don't try and do anything with leafrefs + # NB: use-xpathhelper is NOT specified here, so we don't try and do anything + # with leafrefs os.system(cmd) import ocbind @@ -82,17 +88,19 @@ def main(): json_dir = os.path.join(this_dir, "json") - jbgp_global_ex = json.load(open(os.path.join(json_dir, "bgp-global-ex.json"), 'r')) - ljs = pybindJSONDecoder.load_ietf_json(jbgp_global_ex["configuration"], ocbind, "openconfig_bgp", path_helper=yh) + jbgp_global_ex = json.load( + open(os.path.join(json_dir, "bgp-global-ex.json"), 'r')) + ljs = pybindJSONDecoder.load_ietf_json(jbgp_global_ex["configuration"], + ocbind, "openconfig_bgp", path_helper=yh) expected_ljs = \ { - "bgp" : { - "global" : { - "confederation" : { - "config" : { - "identifier" : 65517, - "enabled" : True, - "member-as" : [ + "bgp": { + "global": { + "confederation": { + "config": { + "identifier": 65517, + "enabled": True, + "member-as": [ 65518, 65519, 65520 @@ -105,23 +113,25 @@ def main(): assert ljs.get(filter=True) == expected_ljs, \ "Invalid JSON loaded for global config" - jbgp_neigh_list = json.load(open(os.path.join(json_dir, "bgp-neighbor-list-ex.json"), 'r')) - ljs = pybindJSONDecoder.load_ietf_json(jbgp_neigh_list["configuration"], ocbind, "openconfig_bgp", path_helper=yh) + jbgp_neigh_list = json.load(open(os.path.join(json_dir, + "bgp-neighbor-list-ex.json"), 'r')) + ljs = pybindJSONDecoder.load_ietf_json(jbgp_neigh_list["configuration"], + ocbind, "openconfig_bgp", path_helper=yh) expected_ljs = \ { - "bgp" : { - "neighbors" : { - "neighbor" : { - "13.13.13.13" : { - "neighbor-address" : "13.13.13.13", - "config" : { - "peer-group" : "g1" + "bgp": { + "neighbors": { + "neighbor": { + "13.13.13.13": { + "neighbor-address": "13.13.13.13", + "config": { + "peer-group": "g1" } }, - "12.12.12.12" : { - "neighbor-address" : "12.12.12.12", - "config" : { - "peer-group" : "g1" + "12.12.12.12": { + "neighbor-address": "12.12.12.12", + "config": { + "peer-group": "g1" } } } @@ -132,45 +142,53 @@ def main(): "Invalid JSON returned when loading neighbor list" jbgp_gr = json.load(open(os.path.join(json_dir, "bgp-gr-ex.json"), 'r')) - ljs = pybindJSONDecoder.load_ietf_json(jbgp_gr["configuration"], ocbind, "openconfig_bgp", path_helper=yh) + ljs = pybindJSONDecoder.load_ietf_json(jbgp_gr["configuration"], ocbind, + "openconfig_bgp", path_helper=yh) expected_ljs = \ { - "bgp" : { - "neighbors" : { - "neighbor" : { - "12.12.12.12" : { - "config" : { - "peer-group" : "g1" + "bgp": { + "neighbors": { + "neighbor": { + "12.12.12.12": { + "config": { + "peer-group": "g1" }, - "neighbor-address" : "12.12.12.12" + "neighbor-address": "12.12.12.12" }, - "13.13.13.13" : { - "neighbor-address" : "13.13.13.13", - "config" : { - "peer-group" : "g2" + "13.13.13.13": { + "neighbor-address": "13.13.13.13", + "config": { + "peer-group": "g2" } } } } } } - assert ljs.get(filter=True) == expected_ljs, "Graceful restart example was not loaded correctly" - assert ljs.bgp.neighbors.neighbor[u"12.12.12.12"]._metadata == {u"inactive": True}, "Metadata for GR example was not loaded correctly" - - jbgp_deactivated = json.load(open(os.path.join(json_dir, "bgp-deactivated-config-ex.json"),'r')) - ljs = pybindJSONDecoder.load_ietf_json(jbgp_deactivated["configuration"], ocbind, "openconfig_bgp", path_helper=yh) + assert ljs.get(filter=True) == expected_ljs, \ + "Graceful restart example was not loaded correctly" + assert ljs.bgp.neighbors.neighbor[u"12.12.12.12"]._metadata == \ + {u"inactive": True}, \ + "Metadata for GR example was not loaded correctly" + + jbgp_deactivated = json.load(open(os.path.join(json_dir, + "bgp-deactivated-config-ex.json"), 'r')) + ljs = pybindJSONDecoder.load_ietf_json(jbgp_deactivated["configuration"], + ocbind, "openconfig_bgp", path_helper=yh) expected_ljs = \ { - "bgp" : { - "global" : { - "config" : { - "router-id" : "10.10.10.10" + "bgp": { + "global": { + "config": { + "router-id": "10.10.10.10" } } } } - assert ljs.get(filter=True) == expected_ljs, "Router ID configuration example not loaded correctly" - assert ljs.bgp.global_.config.router_id._metadata["inactive"] == True, "Metadata for router-id element not set correctly" + assert ljs.get(filter=True) == expected_ljs, \ + "Router ID configuration example not loaded correctly" + assert ljs.bgp.global_.config.router_id._metadata["inactive"] == True, \ + "Metadata for router-id element not set correctly" if not k: del_dirs.append(os.path.join(this_dir, "ocbind")) @@ -182,9 +200,6 @@ def main(): os.rmdir(os.path.join(root, name)) os.rmdir(dirname) - # os.system("/bin/rm -rf %s/ocbind" % this_dir) - # for d in del_dirs: - # os.system("") if __name__ == '__main__': main() diff --git a/tests/string/run.py b/tests/string/run.py index 25dd2ac3..3259d765 100755 --- a/tests/string/run.py +++ b/tests/string/run.py @@ -31,7 +31,7 @@ def main(): assert pyangbindpath is not False, "could not resolve pyangbind directory" this_dir = os.path.dirname(os.path.realpath(__file__)) - + cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind -o %s/bindings.py" % this_dir diff --git a/tests/xpath/01-list_leaflist/run.py b/tests/xpath/01-list_leaflist/run.py index 9a38e2ba..c2a6e063 100755 --- a/tests/xpath/01-list_leaflist/run.py +++ b/tests/xpath/01-list_leaflist/run.py @@ -147,7 +147,6 @@ def t3_leaflist_remove(yobj, tree): "leaf-list (%s -> %s [%s])" % (b[0], path, new_retr[0]) - def t4_list_remove(yobj, tree): for b in ["steam", "liberty", "california-lager", "porter", "ipa",