From 0accfa07c81d549bf5a6a630d851f795a249b443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Hern=C3=A1ndez=20Cordero?= Date: Thu, 1 Aug 2024 17:11:57 +0200 Subject: [PATCH] Support empy3 and empy4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alejandro Hernández Cordero --- .../rosidl_adapter/resource/__init__.py | 33 +++++++++++++----- rosidl_pycommon/rosidl_pycommon/__init__.py | 34 +++++++++++++------ 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/rosidl_adapter/rosidl_adapter/resource/__init__.py b/rosidl_adapter/rosidl_adapter/resource/__init__.py index ce3335853..076ca6c7e 100644 --- a/rosidl_adapter/rosidl_adapter/resource/__init__.py +++ b/rosidl_adapter/rosidl_adapter/resource/__init__.py @@ -14,10 +14,13 @@ from io import StringIO import os +from packaging import version import sys import em +if version.parse(em.__version__) >= version.parse('4.0.0'): + from em import Configuration def expand_template(template_name, data, output_file, encoding='utf-8'): content = evaluate_template(template_name, data) @@ -45,18 +48,29 @@ def evaluate_template(template_name, data): output = StringIO() try: - _interpreter = em.Interpreter( - output=output, - options={ - em.BUFFERED_OPT: True, - em.RAW_OPT: True, - }) - + if version.parse(em.__version__) >= version.parse('4.0.0'): + config = Configuration( + defaultRoot=template_path, + defaultStdout=output, + useProxy=False) + _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 version.parse(em.__version__) >= version.parse('4.0.0'): + _interpreter.string(content, locals=data) + else: + _interpreter.string(content, template_path, locals=data) _interpreter.invoke('afterFile') return output.getvalue() @@ -66,7 +80,8 @@ def evaluate_template(template_name, data): file=sys.stderr) raise finally: - _interpreter.shutdown() + if _interpreter is not None: + _interpreter.shutdown() _interpreter = None diff --git a/rosidl_pycommon/rosidl_pycommon/__init__.py b/rosidl_pycommon/rosidl_pycommon/__init__.py index 3fecd8c72..6bfa7fa4e 100644 --- a/rosidl_pycommon/rosidl_pycommon/__init__.py +++ b/rosidl_pycommon/rosidl_pycommon/__init__.py @@ -15,11 +15,16 @@ from io import StringIO import json import os +from packaging import version import pathlib import re import sys import em + +if version.parse(em.__version__) >= version.parse('4.0.0'): + from em import Configuration + from rosidl_parser.definition import IdlLocator from rosidl_parser.parser import parse_idl_file @@ -146,20 +151,29 @@ 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 version.parse(em.__version__) >= version.parse('4.0.0'): + config = Configuration( + defaultRoot=template_path, + defaultStdout=output, + useProxy=False) + 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)