Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix statement follow_path to allow easy external paths #856

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions pyang/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2703,7 +2703,7 @@ def validate_leafref_path(ctx, stmt, path_spec, path,
else:
in_typedef = False

def find_identifier(identifier):
def find_identifier(identifier, current_module):
if util.is_prefixed(identifier):
(prefix, name) = identifier
if (path.i_module.keyword == 'submodule' and
Expand All @@ -2719,6 +2719,8 @@ def find_identifier(identifier):
return (pmodule, name)
elif in_typedef and stmt.i_module.i_version != '1':
raise Abort
elif current_module:
return (current_module, identifier)
else: # local identifier
return (local_module, identifier)

Expand All @@ -2734,11 +2736,12 @@ def is_predicate(x):
return True
return False

def follow_path(ptr, up, dn):
def follow_path(ptr, up, dn, target_module):
path_list = []
last_skipped = None
if up == -1: # absolute path
(pmodule, name) = find_identifier(dn[0])
(pmodule, name) = find_identifier(dn[0], target_module)
target_module = pmodule
ptr = search_child(pmodule.i_children, pmodule.i_modulename, name)
if not is_submodule_included(path, ptr):
ptr = None
Expand Down Expand Up @@ -2798,7 +2801,8 @@ def follow_path(ptr, up, dn):
keys = []
while i < len(dn):
if is_identifier(dn[i]) is True:
(pmodule, name) = find_identifier(dn[i])
(pmodule, name) = find_identifier(dn[i], target_module)
target_module = pmodule
module_name = pmodule.i_modulename
elif ptr.keyword == 'list': # predicate on a list, good
key_list = ptr
Expand All @@ -2807,7 +2811,7 @@ def follow_path(ptr, up, dn):
while i < len(dn) and is_predicate(dn[i]) is True:
# unpack the predicate
(_tag, keyleaf, pup, pdn) = dn[i]
(pmodule, pname) = find_identifier(keyleaf)
(pmodule, pname) = find_identifier(keyleaf, target_module)
# make sure the keyleaf is really a key in the list
pleaf = search_child(ptr.i_key, pmodule.i_modulename, pname)
if pleaf is None:
Expand All @@ -2826,7 +2830,7 @@ def follow_path(ptr, up, dn):
# check what this predicate refers to; make sure it's
# another leaf; either of type leafref to keyleaf, OR same
# type as the keyleaf
(xkey_list, x_key, xleaf, _x) = follow_path(stmt, pup, pdn)
(xkey_list, x_key, xleaf, _x) = follow_path(stmt, pup, pdn, local_module)
stmt.i_derefed_leaf = xleaf
if xleaf.keyword != 'leaf':
err_add(ctx.errors, pathpos,
Expand Down Expand Up @@ -2863,7 +2867,7 @@ def follow_path(ptr, up, dn):
(up, dn, derefup, derefdn) = path_spec
if derefup > 0:
# first follow the deref
(key_list, keys, ptr, _x) = follow_path(stmt, derefup, derefdn)
(key_list, keys, ptr, _x) = follow_path(stmt, derefup, derefdn, local_module)
if ptr.keyword != 'leaf':
err_add(ctx.errors, pathpos, 'LEAFREF_DEREF_NOT_LEAFREF',
(ptr.arg, ptr.pos))
Expand Down Expand Up @@ -2902,9 +2906,9 @@ def follow_path(ptr, up, dn):
d2 = m.group(2)
expanded_path = "%s[%s = current()/%s]/%s" % \
(s1, s2, d1, d2)
(key_list, keys, ptr, path_list) = follow_path(derefed_stmt, up, dn)
(key_list, keys, ptr, path_list) = follow_path(derefed_stmt, up, dn, derefed_stmt.i_module)
else:
(key_list, keys, ptr, path_list) = follow_path(stmt, up, dn)
(key_list, keys, ptr, path_list) = follow_path(stmt, up, dn, local_module)
expanded_path = path.arg
# ptr is now the node that the leafref path points to
# check that it is a leaf
Expand Down
8 changes: 4 additions & 4 deletions pyang/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ def chk_xpath_path(ctx, mod, pos, initial, node, path):
prefix = nodetest[1]
name = nodetest[2]
if prefix is None:
if initial is None:
if node is None:
pmodule = None
elif initial.keyword == 'module':
pmodule = initial
elif node.keyword == 'module':
pmodule = node
else:
pmodule = initial.i_module
pmodule = node.i_module
else:
pmodule = prefix_to_module(mod, prefix, pos, ctx.errors)
# if node and initial are None, it means we're checking an XPath
Expand Down