Skip to content

Commit 9cb0a84

Browse files
committed
fix: support for Python < 3.9
1 parent 6738e81 commit 9cb0a84

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

tutorwordpress/plugin.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from __future__ import annotations
22

33
import os
4+
import sys
45

56
import click
67
from tutor import fmt, hooks, config as tutor_config
78

89
from .__about__ import __version__
910

10-
try:
11-
import importlib.resources as importlib_resources
12-
except ImportError:
13-
import importlib_resources
11+
if sys.version_info >= (3, 9):
12+
from importlib.resources import files as importlib_resources_files
13+
else:
14+
import pkg_resources
1415

1516
###############
1617
# CONFIGURATION
@@ -45,11 +46,20 @@
4546
("lms", ("wordpress", "tasks", "lms", "init.sh")),
4647
]
4748

49+
50+
def get_template_full_path(package_name: str, *template_path: str) -> str:
51+
if sys.version_info >= (3, 9):
52+
return str(
53+
importlib_resources_files(package_name)
54+
/ os.path.join("templates", *template_path)
55+
)
56+
else:
57+
resource_path = pkg_resources.resource_filename(package_name, "")
58+
return os.path.join(resource_path, "templates", *template_path)
59+
60+
4861
for service, template_path in MY_INIT_TASKS:
49-
full_path: str = str(
50-
importlib_resources.files("tutorwordpress")
51-
/ os.path.join("templates", *template_path)
52-
)
62+
full_path: str = get_template_full_path("tutorwordpress", *template_path)
5363
with open(full_path, encoding="utf-8") as init_task_file:
5464
init_task: str = init_task_file.read()
5565
hooks.Filters.CLI_DO_INIT_TASKS.add_item((service, init_task))

0 commit comments

Comments
 (0)