Skip to content

Commit

Permalink
Add evaluation context processing for ContainerStack and SettingFunction
Browse files Browse the repository at this point in the history
CURA-4358

- Make getProperty() and getRawProperty() in ContainerStack to be
  evaluation-context-aware.
- Allow getRawProperty() to skip some containers if this info is
  provided by the context.
- SettingFunction can now override operators with those provided in the
  evaluation context.
  • Loading branch information
LipuFei committed Oct 9, 2017
1 parent fe9a72d commit f0e8d26
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
20 changes: 15 additions & 5 deletions UM/Settings/ContainerStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def setDirty(self, dirty: bool) -> None:
# result of evaluating that property with the current stack. If you need the
# actual function, use getRawProperty()
def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None):
value = self.getRawProperty(key, property_name)
value = self.getRawProperty(key, property_name, context = context)
if isinstance(value, SettingFunction):
if context is not None:
context.pushContainer(self)
Expand All @@ -222,18 +222,28 @@ def getProperty(self, key: str, property_name: str, context: Optional[PropertyEv
# \return The raw property value of the property, or None if not found. Note that
# the value might be a SettingFunction instance.
#
def getRawProperty(self, key, property_name, *, use_next = True, skip_until_container = None):
for container in self._containers:
def getRawProperty(self, key, property_name, *, context: Optional[PropertyEvaluationContext] = None,
use_next = True, skip_until_container = None):

This comment has been minimized.

Copy link
@jackha

jackha Oct 10, 2017

Contributor

The changes seem to have the same functionality as skip_until_container, but I'm a bit confused about the PropertyEvaluationContext context. Here self._containers is used in combination with some keys being set in the context, but the PropertyEvaluationContext can contain its own stacks. So, is it the correct usage of PropertyEvaluationContext? The usage in getProperty is a bit different.

This comment has been minimized.

Copy link
@diegopradogesto

diegopradogesto Oct 10, 2017

Contributor

As far as I know, both basically do the same, so maybe it's interesting to remove the skip_until_container.

This comment has been minimized.

Copy link
@jackha

jackha Oct 10, 2017

Contributor

Yes I think we should, it makes things unnecessarily complex.

containers = self._containers
if context is not None:
# if context is provided, check if there is any container that needs to be skipped.
start_index = context.context.get("evaluate_from_container_index", 0)
if start_index >= len(self._containers):
return None
containers = self._containers[start_index:]

for container in containers:
if skip_until_container and container.getId() != skip_until_container:
continue #Skip.
skip_until_container = None #When we find the container, stop skipping.

value = container.getProperty(key, property_name)
value = container.getProperty(key, property_name, context)
if value is not None:
return value

if self._next_stack and use_next:
return self._next_stack.getRawProperty(key, property_name, use_next = use_next, skip_until_container = skip_until_container)
return self._next_stack.getRawProperty(key, property_name, context = context,
use_next = use_next, skip_until_container = skip_until_container)
else:
return None

Expand Down
3 changes: 3 additions & 0 deletions UM/Settings/SettingFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def __call__(self, value_provider: ContainerInterface, context: Optional[Propert
g = {} # type: Dict[str, Any]
g.update(globals())
g.update(self.__operators)
# override operators if there is any in the context
if context is not None:
g.update(context.context.get("override_operators", {}))

try:
return eval(self._compiled, g, locals)
Expand Down

0 comments on commit f0e8d26

Please sign in to comment.