Skip to content

Commit

Permalink
Migrate from legacy importlib-resources (#143)
Browse files Browse the repository at this point in the history
* Migrate from legacy importlib-resources

The path() function is deprecated.
https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy
For Python>=3.9, use files() instead.

* Use path() when files() is not available

Signed-off-by: Isabel Paredes <isabel.paredes@quantstack.net>
Co-authored-by: Chris Lalancette <clalancette@gmail.com>
  • Loading branch information
IsabelParedes and clalancette authored Dec 31, 2023
1 parent 4e575b1 commit 64c97af
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions ament_package/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
IS_WINDOWS = os.name == 'nt'


def _get_path(template, name):
if hasattr(importlib_resources, 'files'):
return importlib_resources.files(template).joinpath(name)
else:
with importlib_resources.path(template, name) as path:
return str(path)


def get_environment_hook_template_path(name):
with importlib_resources.path('ament_package.template.environment_hook', name) as path:
return str(path)
return _get_path('ament_package.template.environment_hook', name)


def get_package_level_template_names(all_platforms=False):
Expand All @@ -41,8 +48,7 @@ def get_package_level_template_names(all_platforms=False):


def get_package_level_template_path(name):
with importlib_resources.path('ament_package.template.package_level', name) as path:
return str(path)
return _get_path('ament_package.template.package_level', name)


def get_prefix_level_template_names(*, all_platforms=False):
Expand All @@ -61,8 +67,7 @@ def get_prefix_level_template_names(*, all_platforms=False):


def get_prefix_level_template_path(name):
with importlib_resources.path('ament_package.template.prefix_level', name) as path:
return str(path)
return _get_path('ament_package.template.prefix_level', name)


def configure_file(template_file, environment):
Expand Down

0 comments on commit 64c97af

Please # to comment.