Skip to content

Commit

Permalink
Added feedback
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 committed Aug 1, 2024
1 parent 0accfa0 commit 6eed931
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
21 changes: 14 additions & 7 deletions rosidl_adapter/rosidl_adapter/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

from io import StringIO
import os
from packaging import version
import sys

import em

if version.parse(em.__version__) >= version.parse('4.0.0'):
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 @@ -48,10 +50,12 @@ def evaluate_template(template_name, data):

output = StringIO()
try:
if version.parse(em.__version__) >= version.parse('4.0.0'):
if em_has_configuration:
config = Configuration(
defaultRoot=template_path,
defaultStdout=output,
deleteOnError=True,
rawErrors=True,
useProxy=False)
_interpreter = em.Interpreter(
config=config,
Expand All @@ -67,7 +71,7 @@ def evaluate_template(template_name, data):
content = h.read()
_interpreter.invoke(
'beforeFile', name=template_name, file=h, locals=data)
if version.parse(em.__version__) >= version.parse('4.0.0'):
if em_has_configuration:
_interpreter.string(content, locals=data)
else:
_interpreter.string(content, template_path, locals=data)
Expand All @@ -76,7 +80,7 @@ def evaluate_template(template_name, data):
return output.getvalue()
except Exception as e: # noqa: F841
print(
f"{e.__class__.__name__} processing template '{template_name}'",
f"{e.__class__.__name__} processing2 template '{template_name}'",
file=sys.stderr)
raise
finally:
Expand All @@ -93,10 +97,13 @@ 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}': "
f"{e.__class__.__name__} processing1 template '{template_name}': "
f'{e}', file=sys.stderr)
sys.exit(1)
_interpreter.invoke('afterInclude')
19 changes: 15 additions & 4 deletions rosidl_pycommon/rosidl_pycommon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@

import em

if version.parse(em.__version__) >= version.parse('4.0.0'):
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 @@ -157,10 +160,12 @@ def expand_template(

global interpreter
output = StringIO()
if version.parse(em.__version__) >= version.parse('4.0.0'):
if em_has_configuration:
config = Configuration(
defaultRoot=template_path,
defaultStdout=output,
deleteOnError=True,
rawErrors=True,
useProxy=False)
interpreter = em.Interpreter(
config=config,
Expand All @@ -183,7 +188,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 @@ -232,7 +240,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, str(template_path), 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 6eed931

Please sign in to comment.