Skip to content

Commit

Permalink
context.UpdateContext supports human-readable representation and equa…
Browse files Browse the repository at this point in the history
…lity testing.
  • Loading branch information
ynikitenko committed Sep 24, 2023
1 parent 0c873e3 commit 1107169
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
50 changes: 46 additions & 4 deletions lena/context/update_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(self, subcontext, update, value=False, default=_sentinel,
raise lena.core.LenaValueError(
"subcontext must be non-empty"
)
self._init_subcontext = subcontext
self._subcontext = lena.context.str_to_list(subcontext)

self._has_default = default is not _sentinel
Expand Down Expand Up @@ -150,9 +151,12 @@ def __init__(self, subcontext, update, value=False, default=_sentinel,
)
else:
# ChainableUndefined appeared in jinja2 2.11.0
self._update = jinja2.Template(
update, undefined=jinja2.ChainableUndefined
)
if '{' in update:
self._update = jinja2.Template(
update, undefined=jinja2.ChainableUndefined
)
else:
self._update = update
except jinja2.exceptions.TemplateSyntaxError as err:
raise lena.core.LenaValueError(
"template syntax error, {}".format(err.message)
Expand Down Expand Up @@ -197,7 +201,10 @@ def __call__(self, value):
else:
# context format update
try:
update = self._update.render(context)
if isinstance(self._update, str):
update = self._update
else:
update = self._update.render(context)
except jinja2.exceptions.UndefinedError as err:
if self._raise_on_missing:
raise lena.core.LenaKeyError(
Expand All @@ -223,3 +230,38 @@ def __call__(self, value):
else:
subdict[keys[-1]] = update
return (data, context)

def __eq__(self, other):
if not isinstance(other, UpdateContext):
return NotImplemented
if self._has_default != other._has_default:
return False
if self._has_default:
# we don't compare sentinels,
# which hypothetically could be different
if self._default != other._default:
return False
return (
self._init_subcontext == other._init_subcontext and
self._update == other._update and
self._value == other._value and
self._skip_on_missing == other._skip_on_missing and
self._raise_on_missing == other._raise_on_missing and
self._recursively == other._recursively
)

def __repr__(self):
args = ['UpdateContext("{}"'.format(self._init_subcontext)]
if isinstance(self._update, str):
args.append('"{}"'.format(self._update))
else:
args.append(str(self._update))
if self._value:
args += "value={}".format(self._value)
if self._skip_on_missing:
args += "skip_on_missing={}".format(self._skip_on_missing)
if self._raise_on_missing:
args += "raise_on_missing={}".format(self._raise_on_missing)
if not self._recursively:
args += "recursively={}".format(self._recursively)
return ", ".join(args) + ")"
7 changes: 7 additions & 0 deletions tests/context/test_update_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def test_update_context_update_not_str():
# update can be not a dict or a string
uc13 = UpdateContext("data", 0)

# representation works
assert repr(uc13) == 'UpdateContext("data", 0)'

# equality testing works
assert uc13 == UpdateContext("data", 0)
assert uc13 != UpdateContext("data", 1)

# for simple value default, skip_on_missing and raise_on_missing
# can't be set
with pytest.raises(LenaValueError):
Expand Down

0 comments on commit 1107169

Please sign in to comment.