Skip to content

Commit

Permalink
fix: remove pkg_resources for compatibility with python 3.12
Browse files Browse the repository at this point in the history
pkg_resources is a package that is unavailable in python 3.12, unless
setuptools is explicitely installed. Turns out, there are replacement
functions coming from importlib_resources, which can be obtained from
the importlib-resources pypi package. This package will be installed
with tutor starting from 17.0.2.
  • Loading branch information
regisb committed Feb 12, 2024
1 parent 4aa69ed commit d3d0294
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
1 change: 1 addition & 0 deletions changelog.d/20240212_115536_regis_pkg_resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Bugfix] Make plugin compatible with Python 3.12 by removing dependency on `pkg_resources`. (by @regisb)
42 changes: 23 additions & 19 deletions tutorcredentials/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing as t
from glob import glob

import pkg_resources
import importlib_resources
from tutor import hooks as tutor_hooks
from tutor.__about__ import __version_suffix__
from tutormfe.hooks import MFE_APPS, MFE_ATTRS_TYPE
Expand Down Expand Up @@ -64,8 +64,11 @@
# MFEs
########################################


@MFE_APPS.add()
def _add_learner_record_mfe(apps: dict[str, MFE_ATTRS_TYPE]) -> dict[str, MFE_ATTRS_TYPE]:
def _add_learner_record_mfe(
apps: dict[str, MFE_ATTRS_TYPE]
) -> dict[str, MFE_ATTRS_TYPE]:
apps.update(
{
"learner-record": {
Expand All @@ -76,20 +79,28 @@ def _add_learner_record_mfe(apps: dict[str, MFE_ATTRS_TYPE]) -> dict[str, MFE_AT
)
return apps


########################################
# INITIALIZATION TASKS
########################################

MY_INIT_TASKS = [
("mysql", ("templates", "credentials", "tasks", "mysql", "init")),
("lms", ("templates", "credentials", "tasks", "lms", "init")),
("credentials", ("templates", "credentials", "tasks", "credentials", "init")),
("mysql", ("templates", "credentials", "tasks", "mysql", "sync_users")),
("mysql", "init"),
("lms", "init"),
("credentials", "init"),
("mysql", "sync_users"),
]

HERE = os.path.abspath(os.path.dirname(__file__))
for service, template_path in MY_INIT_TASKS:
full_path: str = os.path.join(HERE, *template_path)
for service, template_name in MY_INIT_TASKS:
full_path: str = str(
importlib_resources.files("tutorcredentials")
/ "templates"
/ "credentials"
/ "tasks"
/ service
/ template_name
)

with open(full_path, encoding="utf-8") as init_task_file:
init_task: str = init_task_file.read()
Expand Down Expand Up @@ -161,11 +172,9 @@ def _mount_credentials_on_build(
# TEMPLATE RENDERING
########################################

tutor_hooks.Filters.ENV_TEMPLATE_ROOTS.add_items(
# Root paths for template files, relative to the project root.
[
pkg_resources.resource_filename("tutorcredentials", "templates"),
]
tutor_hooks.Filters.ENV_TEMPLATE_ROOTS.add_item(
# Root path for template files, relative to the project root.
str(importlib_resources.files("tutorcredentials") / "templates")
)

tutor_hooks.Filters.ENV_TEMPLATE_TARGETS.add_items(
Expand All @@ -180,12 +189,7 @@ def _mount_credentials_on_build(
# PATCH LOADING
########################################

for path in glob(
os.path.join(
pkg_resources.resource_filename("tutorcredentials", "patches"),
"*",
)
):
for path in glob(str(importlib_resources.files("tutorcredentials") / "patches" / "*")):
with open(path, encoding="utf-8") as patch_file:
tutor_hooks.Filters.ENV_PATCHES.add_item(
(os.path.basename(path), patch_file.read())
Expand Down

0 comments on commit d3d0294

Please sign in to comment.