Skip to content

Commit

Permalink
use importlib.resources for config.templates
Browse files Browse the repository at this point in the history
  • Loading branch information
orbeckst committed Jun 17, 2024
1 parent 4574592 commit edbc90d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
==============================

2024-??-?? 0.9.1
orbeckst

* templates in `config.templates` are now stored as
`importlib.resources.abc.Traversable` (PosixPath-like) objects instead of
strings of paths to files in the file system because we switched from
pkg_resources to importlib.resources for managing access to included files
(#282)
* fixed reporting of failures of GromacsCommand and DeprecationWarnings for use
of \s in regular expression strings (#285)

Expand Down
41 changes: 20 additions & 21 deletions gromacs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@
import sys

from configparser import ConfigParser

from pkg_resources import resource_filename, resource_listdir
from importlib import resources

from . import utilities

Expand Down Expand Up @@ -307,37 +306,36 @@


def _generate_template_dict(dirname):
"""Generate a list of included files *and* extract them to a temp space.
"""Generate a list of included files *and* make them available.
Template files are stored inside the package. They are made available as
:class:` importlib.resources.abc.Traversable` object with
:mod:`pathlib.Path`-like methods. They can normally be read with the usual
:func:`open` function but if a real file needs to be present, use
:func:`importlib.resources.as_file`.
All template entries are stored in :data:`config.templates`.
.. SeeAlso:: importlib.resources
Templates have to be extracted from the egg because they are used
by external code. All template filenames are stored in
:data:`config.templates`.
"""
return dict(
(resource_basename(fn), resource_filename(__name__, dirname + "/" + fn))
for fn in resource_listdir(__name__, dirname)
if not fn.endswith("~")
(entry.name, entry)
for entry in resources.files(__name__).joinpath(dirname).iterdir()
if not entry.name.endswith(("~", "__pycache__", "__init__.py"))
)


def resource_basename(resource):
"""Last component of a resource (which always uses '/' as sep)."""
if resource.endswith("/"):
resource = resource[:-1]
parts = resource.split("/")
return parts[-1]


templates = _generate_template_dict("templates")
"""*GromacsWrapper* comes with a number of templates for run input files
and queuing system scripts. They are provided as a convenience and
examples but **WITHOUT ANY GUARANTEE FOR CORRECTNESS OR SUITABILITY FOR
ANY PURPOSE**.
All template filenames are stored in
:data:`gromacs.config.templates`. Templates have to be extracted from
the GromacsWrapper python egg file because they are used by external
code: find the actual file locations from this variable.
All templates are stored in :data:`gromacs.config.templates` as :class:`
importlib.resources.abc.Traversable` objects with :mod:`pathlib.Path`-like
methods. They can normally be read with the usual :func:`open` function but if
a real file needs to be present, use :func:`importlib.resources.as_file`.
**Gromacs mdp templates**
Expand All @@ -357,6 +355,7 @@ def resource_basename(resource):
The queing system scripts are highly specific and you will need to add your
own into :data:`gromacs.config.qscriptdir`.
See :mod:`gromacs.qsub` for the format and how these files are processed.
"""

#: The default template for SGE/PBS run scripts.
Expand Down

0 comments on commit edbc90d

Please sign in to comment.