From 9cb0a8448747dac1145799c1dfb76fe2381f0ca8 Mon Sep 17 00:00:00 2001 From: Emad Rad Date: Sat, 27 Jul 2024 11:03:50 +0330 Subject: [PATCH] fix: support for Python < 3.9 --- tutorwordpress/plugin.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tutorwordpress/plugin.py b/tutorwordpress/plugin.py index 7a5da94..03c3d12 100644 --- a/tutorwordpress/plugin.py +++ b/tutorwordpress/plugin.py @@ -1,16 +1,17 @@ from __future__ import annotations import os +import sys import click from tutor import fmt, hooks, config as tutor_config from .__about__ import __version__ -try: - import importlib.resources as importlib_resources -except ImportError: - import importlib_resources +if sys.version_info >= (3, 9): + from importlib.resources import files as importlib_resources_files +else: + import pkg_resources ############### # CONFIGURATION @@ -45,11 +46,20 @@ ("lms", ("wordpress", "tasks", "lms", "init.sh")), ] + +def get_template_full_path(package_name: str, *template_path: str) -> str: + if sys.version_info >= (3, 9): + return str( + importlib_resources_files(package_name) + / os.path.join("templates", *template_path) + ) + else: + resource_path = pkg_resources.resource_filename(package_name, "") + return os.path.join(resource_path, "templates", *template_path) + + for service, template_path in MY_INIT_TASKS: - full_path: str = str( - importlib_resources.files("tutorwordpress") - / os.path.join("templates", *template_path) - ) + full_path: str = get_template_full_path("tutorwordpress", *template_path) with open(full_path, encoding="utf-8") as init_task_file: init_task: str = init_task_file.read() hooks.Filters.CLI_DO_INIT_TASKS.add_item((service, init_task))