From 6cfd0c63bba529580aaac604569199fe24c64582 Mon Sep 17 00:00:00 2001 From: Yan Gorelik Date: Tue, 12 Jun 2018 14:09:49 -0700 Subject: [PATCH] Refactored solution for #786 for Python API --- sdk/python/core/ydk/types/__init__.py | 3 +- sdk/python/core/ydk/types/py_types.py | 24 +++++++++----- .../python/class_get_entity_path_printer.py | 33 ++++++++++++++----- ydkgen/printer/python/module_printer.py | 2 +- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/sdk/python/core/ydk/types/__init__.py b/sdk/python/core/ydk/types/__init__.py index 2bd496df8..5d37b176f 100644 --- a/sdk/python/core/ydk/types/__init__.py +++ b/sdk/python/core/ydk/types/__init__.py @@ -14,7 +14,7 @@ # limitations under the License. # ------------------------------------------------------------------ -from .py_types import Entity, EntityCollection, Config, Filter, YList, YLeafList, _add_key_token +from .py_types import Entity, EntityCollection, Config, Filter, YList, YLeafList from ydk.ext.types import Bits from ydk.ext.types import ChildrenMap from ydk.ext.types import ModelCachingOption @@ -48,5 +48,4 @@ "YLeaf", "YLeafList", "YType", - "_add_key_token", ] diff --git a/sdk/python/core/ydk/types/py_types.py b/sdk/python/core/ydk/types/py_types.py index 02a581c83..3f109fb07 100644 --- a/sdk/python/core/ydk/types/py_types.py +++ b/sdk/python/core/ydk/types/py_types.py @@ -281,7 +281,21 @@ def get_name_leaf_data(self): return leaf_name_data def get_segment_path(self): - return self._segment_path() + path = self._segment_path() + if ("[" in path) and hasattr(self, 'ylist_key_names') and len(self.ylist_key_names) > 0: + path = path.split('[')[0] + for attr_name in self.ylist_key_names: + leaf = self._leafs[attr_name] + if leaf is not None: + attr_str = format(self.__dict__[attr_name]) + if "'" in attr_str: + path += '[{}="{}"]'.format(leaf.name, attr_str) + else: + path += "[{}='{}']".format(leaf.name, attr_str) + else: + # should never get here + return self._segment_path() + return path def path(self): return self.get_segment_path() @@ -585,11 +599,3 @@ def __getitem__(self, item): def __len__(self): return self._entity_map.__len__() + self._cache_dict.__len__() - -def _add_key_token(attr, attr_name): - attr_str = format(attr) - if "'" in attr_str: - token = '[{}="{}"]'.format(attr_name, attr_str) - else: - token = "[{}='{}']".format(attr_name, attr_str) - return token \ No newline at end of file diff --git a/ydkgen/printer/python/class_get_entity_path_printer.py b/ydkgen/printer/python/class_get_entity_path_printer.py index 823591014..88ec9baad 100644 --- a/ydkgen/printer/python/class_get_entity_path_printer.py +++ b/ydkgen/printer/python/class_get_entity_path_printer.py @@ -45,7 +45,7 @@ def print_output(self, clazz): self._print_get_ydk_segment_path_body(clazz) def _print_get_ydk_segment_path_body(self, clazz): - path="'" + path='"' if clazz.owner is not None: if isinstance(clazz.owner, Package): path+= clazz.owner.stmt.arg + ':' @@ -53,20 +53,37 @@ def _print_get_ydk_segment_path_body(self, clazz): path+=clazz.stmt.i_module.arg + ':' path+= clazz.stmt.arg - path+="'" + path+='"' + predicates = '' insert_token = ' + ' key_props = clazz.get_key_props() for key_prop in key_props: + predicates += insert_token - key_name = '' + predicates += '"[' if key_prop.stmt.i_module.arg != clazz.stmt.i_module.arg: - key_name += key_prop.stmt.i_module.arg - key_name += ':' - key_name += key_prop.stmt.arg + predicates += key_prop.stmt.i_module.arg + predicates += ':' - path += insert_token - path += "_add_key_token(self.%s, '%s')" % (key_prop.name, key_name) + predicates += key_prop.stmt.arg + '=' + + predicates += "'" + + predicates +='"' + + predicates += insert_token + + predicates += ('str(self.%s)') % key_prop.name + insert_token + + predicates += '"' + + predicates += "'" + + predicates += ']"' + + path = '%s%s' % (path, predicates) + self.ctx.writeln("self._segment_path = lambda: %s" % path) diff --git a/ydkgen/printer/python/module_printer.py b/ydkgen/printer/python/module_printer.py index 6c9718f74..e9da83598 100644 --- a/ydkgen/printer/python/module_printer.py +++ b/ydkgen/printer/python/module_printer.py @@ -42,7 +42,7 @@ def print_header(self, package): self.ctx.writeln("from collections import OrderedDict") self.ctx.bline() - self.ctx.writeln("from ydk.types import Entity, EntityPath, Identity, Enum, YType, YLeaf, YLeafList, YList, LeafDataList, Bits, Empty, Decimal64, _add_key_token") + self.ctx.writeln("from ydk.types import Entity, EntityPath, Identity, Enum, YType, YLeaf, YLeafList, YList, LeafDataList, Bits, Empty, Decimal64") self.ctx.writeln("from ydk.filters import YFilter") self.ctx.writeln("from ydk.errors import YError, YModelError") self.ctx.writeln("from ydk.errors.error_handler import handle_type_error as _handle_type_error")