Skip to content

Commit b26a7c0

Browse files
authored
Replace usages of pkg_resources with importlib (#1428)
1 parent e00f0a6 commit b26a7c0

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

cms/locale/locale.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import babel.numbers
4242
import babel.support
4343
import babel.units
44-
import pkg_resources
44+
import importlib.resources
4545

4646
from cms import config
4747
from cmscommon.datetime import utc
@@ -282,13 +282,25 @@ def get_translations() -> dict[str, Translation]:
282282
"""
283283
result = {DEFAULT_TRANSLATION.identifier: DEFAULT_TRANSLATION}
284284

285-
for lang_code in sorted(pkg_resources.resource_listdir("cms.locale", "")):
286-
mofile_path = os.path.join(lang_code, "LC_MESSAGES", "cms.mo")
287-
if pkg_resources.resource_exists("cms.locale", mofile_path):
288-
with pkg_resources.resource_stream("cms.locale", mofile_path) as f:
289-
t = Translation(lang_code, f)
290-
logger.info("Found translation %s", t.identifier)
291-
result[t.identifier] = t
285+
try:
286+
locale_pkg = importlib.resources.files("cms.locale")
287+
for lang_dir in locale_pkg.iterdir():
288+
if lang_dir.is_dir():
289+
lang_code = lang_dir.name
290+
try:
291+
mofile_path = lang_dir / "LC_MESSAGES" / "cms.mo"
292+
with mofile_path.open("rb") as f:
293+
t = Translation(lang_code, f)
294+
logger.info("Found translation %s", t.identifier)
295+
result[t.identifier] = t
296+
except Exception:
297+
logger.warning(
298+
"Failed to load translation for %s",
299+
lang_code,
300+
exc_info=True,
301+
)
302+
except Exception:
303+
logger.warning("Failed to scan locale directory", exc_info=True)
292304

293305
return result
294306

cms/plugin.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import logging
2323

24-
import pkg_resources
24+
from importlib.metadata import entry_points
2525

2626

2727
logger = logging.getLogger(__name__)
@@ -49,14 +49,17 @@ def plugin_list(entry_point_group: str) -> list[type]:
4949
5050
"""
5151
classes = []
52-
for entry_point in pkg_resources.iter_entry_points(entry_point_group):
52+
for entry_point in entry_points(group=entry_point_group):
5353
try:
5454
classes.append(entry_point.load())
55-
except (pkg_resources.UnknownExtra, ImportError):
55+
except Exception:
5656
logger.warning(
57-
"Failed to load entry point %s for group %s from %s:%s, "
58-
"provided by distribution %s, requiring extras %s.",
59-
entry_point.name, entry_point_group, entry_point.module_name,
60-
".".join(entry_point.attrs), entry_point.dist,
61-
", ".join(entry_point.extras), exc_info=True)
57+
"Failed to load entry point %s for group %s from %s, "
58+
"provided by distribution %s.",
59+
entry_point.name,
60+
entry_point_group,
61+
entry_point.value,
62+
entry_point.dist.name if entry_point.dist else "unknown",
63+
exc_info=True,
64+
)
6265
return classes

cmsranking/Config.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
# You should have received a copy of the GNU Affero General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import atexit
1920
import errno
2021
import logging
2122
import os
2223
import sys
2324
import tomllib
2425
import typing
2526

26-
import pkg_resources
27+
import importlib.resources
2728

2829
from cmsranking.Logger import add_file_handler
2930

@@ -59,7 +60,11 @@ def __init__(self):
5960
# Buffers
6061
self.buffer_size = 100 # Needs to be strictly positive.
6162

62-
self.web_dir = pkg_resources.resource_filename("cmsranking", "static")
63+
# Keep the static files context manager alive for the application lifetime
64+
self._static_files_context = importlib.resources.path("cmsranking", "static")
65+
self.web_dir = str(self._static_files_context.__enter__())
66+
# Register cleanup handler to properly exit the context manager
67+
atexit.register(self._static_files_context.__exit__, None, None, None)
6368

6469
# Try to find CMS installation root from the venv in which we run
6570
self.base_dir = sys.prefix

0 commit comments

Comments
 (0)