Skip to content

Commit

Permalink
Refactored solution for CiscoDevNet#786 for Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Gorelik committed Jun 12, 2018
1 parent 8acaafa commit 6cfd0c6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
3 changes: 1 addition & 2 deletions sdk/python/core/ydk/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,5 +48,4 @@
"YLeaf",
"YLeafList",
"YType",
"_add_key_token",
]
24 changes: 15 additions & 9 deletions sdk/python/core/ydk/types/py_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
33 changes: 25 additions & 8 deletions ydkgen/printer/python/class_get_entity_path_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,45 @@ 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 + ':'
elif clazz.owner.stmt.i_module.arg != clazz.stmt.i_module.arg:
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)

Expand Down
2 changes: 1 addition & 1 deletion ydkgen/printer/python/module_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6cfd0c6

Please sign in to comment.