Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def element(self, uri, idx=None):
- _path = uri.split('/')
- if not _path:
- return None
- def get_indexes(_idx):
- indexes = []
- if _idx is None:
- indexes = [0]
- elif isinstance(_idx, int):
- indexes = [_idx]
- elif isinstance(_idx, tuple):
- indexes = list(_idx)
- elif isinstance(_idx, list):
- indexes = _idx
- cnt = len(_path)
- if cnt > len(indexes):
- for i in range(0, cnt-1):
- indexes.insert(0, 0)
- return indexes
- def get_element(parent, elm_type, elm_id):
- def walk(_parent):
- for node in _parent.nodes.values():
- if node.type == elm_type and node.id == elm_id:
- return node
- if node.has_children:
- node = walk(node)
- if node:
- return node
- return None
- return walk(parent)
- idxs = get_indexes(idx)
- _type = _path.pop(0)
- _id = f'{_type.lower()}{idxs.pop(0)}'
- elm = get_element(self, _type, _id)
- if elm.uri == uri:
- return elm
- while idxs:
- _type = _path.pop(0)
- _id = f'{_type.lower()}{idxs.pop(0)}'
- elm = get_element(elm, _type, _id)
- return elm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement