Skip to content

Commit

Permalink
WIP: Compatibility with EmPy 4
Browse files Browse the repository at this point in the history
  • Loading branch information
cottsay authored and clalancette committed Aug 5, 2024
1 parent f9072e8 commit 070896d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
30 changes: 14 additions & 16 deletions colcon_core/shell/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from colcon_core.logging import colcon_logger
try:
from em import Configuration
from em import Interpreter
from em import OVERRIDE_OPT
except ImportError as e:
try:
import em # noqa: F401
Expand All @@ -33,13 +33,16 @@ def expand_template(template_path, destination_path, data):
:raises: Any exception which `em.Interpreter.string` might raise
"""
output = StringIO()
interpreter = None
try:
# disable OVERRIDE_OPT to avoid saving / restoring stdout
interpreter = CachingInterpreter(
output=output, options={OVERRIDE_OPT: False})
config = Configuration(
defaultRoot=str(template_path),
defaultStdout=output,
useProxy=False)
interpreter = CachingInterpreter(config=config, dispatcher=False)
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()
except Exception as e: # noqa: F841
logger.error(
Expand All @@ -54,21 +57,14 @@ def expand_template(template_path, destination_path, data):
with destination_path.open('w') as h:
h.write(output)
finally:
interpreter.shutdown()


class BypassStdoutInterpreter(Interpreter):
"""Interpreter for EmPy which keeps `stdout` unchanged."""

def installProxy(self): # noqa: D102 N802
# avoid replacing stdout with ProxyFile
pass
if interpreter is not None:
interpreter.shutdown()


cached_tokens = {}


class CachingInterpreter(BypassStdoutInterpreter):
class CachingInterpreter(Interpreter):
"""Interpreter for EmPy which which caches parsed tokens."""

def parse(self, scanner, locals=None): # noqa: A002 D102
Expand All @@ -90,4 +86,6 @@ def parse(self, scanner, locals=None): # noqa: A002 D102
self.invoke('atParse', scanner=scanner, locals=locals)
for token in tokens:
self.invoke('atToken', token=token)
token.run(self, locals)
self.run(token, locals)
scanner.accumulate()
return True
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ python_requires = >=3.6
install_requires =
coloredlogs; sys_platform == 'win32'
distlib
EmPy<4
EmPy>=4
importlib-metadata; python_version < "3.8"
packaging
# the pytest dependency and its extensions are provided for convenience
Expand Down

0 comments on commit 070896d

Please sign in to comment.