Skip to content

Commit

Permalink
Support EmPy 4.x configuration API
Browse files Browse the repository at this point in the history
There isn't a clean way to configure EmPy the way we need to which is
compatible with both EmPy 3.x and 4.x. Unfortunately, this means that
we'll need to have separate code paths.

Additionally, EmPy 4.x doesn't take the `name` argument to
Interpreter.string(). From what I can tell, it wasn't used for anything
interesting anyway. In particular, exception traces still contain
`<string>` instead of the file name.
  • Loading branch information
cottsay committed Sep 9, 2024
1 parent 7cb836e commit 4c88c48
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions colcon_core/shell/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from colcon_core.logging import colcon_logger
try:
from em import Interpreter
from em import OVERRIDE_OPT
except ImportError as e:
try:
import em # noqa: F401
Expand All @@ -35,13 +34,24 @@ def expand_template(template_path, destination_path, data):
"""
output = StringIO()
try:
# disable OVERRIDE_OPT to avoid saving / restoring stdout
interpreter = CachingInterpreter(
output=output, options={OVERRIDE_OPT: False})
try:
from em import Configuration
except ImportError:
from em import OVERRIDE_OPT
# disable OVERRIDE_OPT to avoid saving / restoring stdout
interpreter = CachingInterpreter(
output=output, options={OVERRIDE_OPT: False})
else:
interpreter = CachingInterpreter(

Check warning on line 45 in colcon_core/shell/template/__init__.py

View check run for this annotation

Codecov / codecov/patch

colcon_core/shell/template/__init__.py#L45

Added line #L45 was not covered by tests
config=Configuration(
defaultRoot=str(template_path),
defaultStdout=output,
useProxy=False),
dispatcher=False)
try:
with template_path.open('r') as h:
content = h.read()
interpreter.string(content, str(template_path), locals=data)
interpreter.string(content, locals=data)
output = output.getvalue()
finally:
interpreter.shutdown()
Expand Down

0 comments on commit 4c88c48

Please sign in to comment.