Skip to content

Commit

Permalink
Support EmPy 4.x configuration API (#665)
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 authored Sep 20, 2024
1 parent 927af5e commit fbbb8af
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(
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 fbbb8af

Please sign in to comment.