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

feat(parser): Support relative paths in references #12

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions reclass/datatypes/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ def _interpolate_inner(self, path, inventory):

def _interpolate_render_value(self, path, value, inventory):
try:
value.path = path # pass the path to the resolver to support relative paths
new = value.render(self._base, inventory)
except ResolveError as e:
e.context = path
Expand Down
31 changes: 30 additions & 1 deletion reclass/utils/dictpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ class DictPath(object):
level down the nested dictionary.
'''

def __init__(self, delim, contents=None):
def __init__(self, delim, contents=None, path=''):
self._delim = delim
self._refpath = path # path where the reference lays

if contents is None:
self._parts = []
Expand Down Expand Up @@ -122,6 +123,34 @@ def new_subpath(self, key):
return DictPath(self._delim, self._parts + [key])

def get_value(self, base):
"""
get the value from a given key (in self._parts) and a dictionary (base)
relative references are allowed, e.g. ${:start:from:here}
"""
# only apply relative paths if keyword is specified
if self._parts and self._parts[0] in ("", "~", ".self_name"):
parts = str(self._refpath).split(":")
for part in self._parts:
if not part:
# e.g. ${::key} --> go one level back
parts = parts[:-1]
elif part == "~":
# e.g. ${~:key} --> go to root (parameters)
parts = []
elif (part == ".self_name"):
# e.g. ${:.self_name} returns parent key name
try:
return parts[-1]
except IndexError:
# You can't access values outside of '.parameters'. Using 'parameters'.
# We could throw a more precise error ...
return "parameters"
else:
# you can mix normal key stepping with new features
parts.append(part)
self._parts = parts

# get the value from the dictionary
return self._get_innermost_container(base)[self._get_key()]

def set_value(self, base, value):
Expand Down
10 changes: 5 additions & 5 deletions reclass/values/refitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ def _flatten_contents(self, context, inventory=None):
result = [str(i.render(context, inventory)) for i in self.contents]
return "".join(result)

def _resolve(self, ref, context):
path = DictPath(self._settings.delimiter, ref)
def _resolve(self, ref, context, path=''):
refpath = DictPath(self._settings.delimiter, ref, path=path)
try:
return path.get_value(context)
return refpath.get_value(context)
except (KeyError, TypeError) as e:
raise ResolveError(ref)

def render(self, context, inventory):
def render(self, context, inventory, path=''):
#strings = [str(i.render(context, inventory)) for i in self.contents]
#return self._resolve("".join(strings), context)
return self._resolve(self._flatten_contents(context, inventory),
context)
context, path=path)

def __str__(self):
strings = [str(i) for i in self.contents]
Expand Down
6 changes: 5 additions & 1 deletion reclass/values/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .dictitem import DictItem
from .listitem import ListItem
from .scaitem import ScaItem
from .refitem import RefItem
from reclass.errors import InterpolationError

from six import string_types
Expand All @@ -20,9 +21,10 @@ class Value(object):

_parser = Parser()

def __init__(self, value, settings, uri, parse_string=True):
def __init__(self, value, settings, uri, parse_string=True, path=''):
self._settings = settings
self.uri = uri
self.path = path
self.overwrite = False
self.constant = False
if isinstance(value, string_types):
Expand Down Expand Up @@ -87,6 +89,8 @@ def assembleRefs(self, context):

def render(self, context, inventory):
try:
if isinstance(self._item, RefItem):
return self._item.render(context, inventory, path=self.path)
return self._item.render(context, inventory)
except InterpolationError as e:
e.uri = self.uri
Expand Down