From 4c88c48fd921d6799c1def4beaa0b47dc1cfbed8 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Fri, 6 Sep 2024 13:50:40 -0500 Subject: [PATCH] Support EmPy 4.x configuration API 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 `` instead of the file name. --- colcon_core/shell/template/__init__.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/colcon_core/shell/template/__init__.py b/colcon_core/shell/template/__init__.py index 7f2c69bc..ef405822 100644 --- a/colcon_core/shell/template/__init__.py +++ b/colcon_core/shell/template/__init__.py @@ -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 @@ -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()