Skip to content

Commit

Permalink
grc/core/FlowGraph: refactor internal namespace refresh code
Browse files Browse the repository at this point in the history
can now reason about results when something goes wrong in one of the steps.

mark private functions as _such

Signed-off-by: Marcus Müller <[email protected]>
  • Loading branch information
marcusmueller committed Nov 17, 2023
1 parent 23f4d59 commit 4834551
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions grc/core/FlowGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,13 @@ def rewrite(self):
"""
Flag the namespace to be renewed.
"""
self.renew_namespace()
self._renew_namespace()
Element.rewrite(self)

def renew_namespace(self):
namespace = {}
# Before renewing the namespace, clear it
# to get rid of entries of blocks that
# are no longer valid ( deleted, disabled, ...)
self.namespace.clear()
# Load imports
def _reload_imports(self, namespace: dict) -> dict:
"""
Load imports; be tolerant about import errors
"""
for expr in self.imports():
try:
exec(expr, namespace)
Expand All @@ -247,12 +244,11 @@ def renew_namespace(self):
# this is ok behavior, unfortunately we could be hiding other import bugs
pass
except Exception:
log.exception('Failed to evaluate import expression "{0}"'.format(
expr), exc_info=True)
log.exception(f"Failed to evaluate import expression \"{expr}\"", exc_info=True)
pass
return namespace

self.imported_names = list(namespace.keys())

def _reload_modules(self, namespace: dict) -> dict:
for id, expr in self.get_python_modules():
try:
module = types.ModuleType(id)
Expand All @@ -262,8 +258,12 @@ def renew_namespace(self):
log.exception(
'Failed to evaluate expression in module {0}'.format(id), exc_info=True)
pass
return namespace

# Load parameters
def _reload_parameters(self, namespace: dict) -> dict:
"""
Load parameters. Be tolerant of evaluation failures.
"""
np = {} # params don't know each other
for parameter_block in self.get_parameters():
try:
Expand All @@ -275,11 +275,12 @@ def renew_namespace(self):
parameter_block.name), exc_info=True)
pass
namespace.update(np) # Merge param namespace
return namespace

# We need the updated namespace to evaluate the variable blocks
# otherwise sometimes variable_block rewrite / eval fails
self.namespace.update(namespace)
# Load variables
def _reload_variables(self, namespace: dict) -> dict:
"""
Load variables. Be tolerant of evaluation failures.
"""
for variable_block in self.get_variables():
try:
variable_block.rewrite()
Expand All @@ -294,7 +295,23 @@ def renew_namespace(self):
log.exception('Failed to evaluate variable block {0}'.format(
variable_block.name), exc_info=True)
pass
return namespace

def _renew_namespace(self) -> None:
# Before renewing the namespace, clear it
# to get rid of entries of blocks that
# are no longer valid ( deleted, disabled, ...)
self.namespace.clear()

namespace = self._reload_imports({})
self.imported_names = set(namespace.keys())
namespace = self._reload_modules(namespace)
namespace = self._reload_parameters(namespace)

# We need the updated namespace to evaluate the variable blocks
# otherwise sometimes variable_block rewrite / eval fails
self.namespace.update(namespace)
namespace = self._reload_variables(namespace)
self._eval_cache.clear()

def evaluate(self, expr, namespace=None, local_namespace=None):
Expand Down

0 comments on commit 4834551

Please sign in to comment.