Skip to content

Commit

Permalink
Support empy3 and empy4 (#821)
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com>
  • Loading branch information
ahcorde authored Aug 22, 2024
1 parent 481c943 commit e25750d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
43 changes: 33 additions & 10 deletions rosidl_adapter/rosidl_adapter/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

import em

try:
from em import Configuration
em_has_configuration = True
except ImportError:
em_has_configuration = False


def expand_template(template_name, data, output_file, encoding='utf-8'):
content = evaluate_template(template_name, data)
Expand Down Expand Up @@ -45,18 +51,31 @@ def evaluate_template(template_name, data):

output = StringIO()
try:
_interpreter = em.Interpreter(
output=output,
options={
em.BUFFERED_OPT: True,
em.RAW_OPT: True,
})

if em_has_configuration:
config = Configuration(
defaultRoot=template_path,
defaultStdout=output,
deleteOnError=True,
rawErrors=True,
useProxy=True)
_interpreter = em.Interpreter(
config=config,
dispatcher=False)
else:
_interpreter = em.Interpreter(
output=output,
options={
em.BUFFERED_OPT: True,
em.RAW_OPT: True,
})
with open(template_path, 'r') as h:
content = h.read()
_interpreter.invoke(
'beforeFile', name=template_name, file=h, locals=data)
_interpreter.string(content, template_path, locals=data)
if em_has_configuration:
_interpreter.string(content, locals=data)
else:
_interpreter.string(content, template_path, locals=data)
_interpreter.invoke('afterFile')

return output.getvalue()
Expand All @@ -66,7 +85,8 @@ def evaluate_template(template_name, data):
file=sys.stderr)
raise
finally:
_interpreter.shutdown()
if _interpreter is not None:
_interpreter.shutdown()
_interpreter = None


Expand All @@ -78,7 +98,10 @@ def _evaluate_template(template_name, **kwargs):
'beforeInclude', name=template_path, file=h, locals=kwargs)
content = h.read()
try:
_interpreter.string(content, template_path, kwargs)
if em_has_configuration:
_interpreter.string(content, locals=kwargs)
else:
_interpreter.string(content, template_path, kwargs)
except Exception as e: # noqa: F841
print(
f"{e.__class__.__name__} processing template '{template_name}': "
Expand Down
48 changes: 36 additions & 12 deletions rosidl_pycommon/rosidl_pycommon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
import sys

import em

try:
from em import Configuration
em_has_configuration = True
except ImportError:
em_has_configuration = False

from rosidl_parser.definition import IdlLocator
from rosidl_parser.parser import parse_idl_file

Expand Down Expand Up @@ -146,20 +153,31 @@ def expand_template(
template_basepath = template_name.parent
template_name = template_name.name

global interpreter
output = StringIO()
interpreter = em.Interpreter(
output=output,
options={
em.BUFFERED_OPT: True,
em.RAW_OPT: True,
},
)

global template_prefix_path
template_prefix_path.append(template_basepath)
template_path = get_template_path(template_name)

global interpreter
output = StringIO()
if em_has_configuration:
config = Configuration(
defaultRoot=template_path,
defaultStdout=output,
deleteOnError=True,
rawErrors=True,
useProxy=True)
interpreter = em.Interpreter(
config=config,
dispatcher=False)
else:
interpreter = em.Interpreter(
output=output,
options={
em.BUFFERED_OPT: True,
em.RAW_OPT: True,
},
)

# create copy before manipulating
data = dict(data)
_add_helper_functions(data)
Expand All @@ -169,7 +187,10 @@ def expand_template(
template_content = h.read()
interpreter.invoke(
'beforeFile', name=template_name, file=h, locals=data)
interpreter.string(template_content, template_path, locals=data)
if em_has_configuration:
interpreter.string(template_content, locals=data)
else:
interpreter.string(template_content, template_path, locals=data)
interpreter.invoke('afterFile')
except Exception as e: # noqa: F841
if os.path.exists(output_file):
Expand Down Expand Up @@ -218,7 +239,10 @@ def _expand_template(template_name, **kwargs):
'beforeInclude', name=str(template_path), file=h, locals=kwargs)
content = h.read()
try:
interpreter.string(content, str(template_path), kwargs)
if em_has_configuration:
interpreter.string(content, locals=kwargs)
else:
interpreter.string(content, template_path, locals=kwargs)
except Exception as e: # noqa: F841
print(f"{e.__class__.__name__} in template '{template_path}': {e}",
file=sys.stderr)
Expand Down

0 comments on commit e25750d

Please sign in to comment.