From 48bf9bdf918749437c0d2164e71a639f5b854d00 Mon Sep 17 00:00:00 2001 From: Hains van den Bosch Date: Fri, 10 Nov 2023 08:20:57 +0100 Subject: [PATCH] Port away from imp module, use importlib instead Based on: https://github.com/oe-alliance/OpenWebif/commit/b10e510c7624e76100567620f92e1c9ca1b0f45c https://github.com/oe-alliance/OpenWebif/commit/4612bbf64ef0f1d73a38b295e3dfad24ad9882d4 Remove redundant code. Thank @jbleyel. Tested with Python 3.9.9 (.pyc files) and Python 3.12 (.py files). --- plugin/controllers/base.py | 25 ++++++++++++++++--------- plugin/httpserver.py | 6 ------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/plugin/controllers/base.py b/plugin/controllers/base.py index 1e42b87dc..3ceae2b4a 100644 --- a/plugin/controllers/base.py +++ b/plugin/controllers/base.py @@ -22,9 +22,9 @@ from __future__ import print_function import os -import imp import json import six +from importlib.util import spec_from_file_location, module_from_spec from twisted.web import server, http, resource from twisted.web.resource import EncodingResourceWrapper @@ -117,18 +117,25 @@ def error404(self, request): request.write(b"OpenWebif

Error 404: Not found


The requested page doesn't exist.") request.finish() + def load_source(self, module, pathname): + spec = spec_from_file_location(module, pathname) + template = module_from_spec(spec) + spec.loader.exec_module(template) + return template + def loadTemplate(self, path, module, args): - if fileExists(getViewsPath(path + ".py")) or fileExists(getViewsPath(path + ".pyo")) or fileExists(getViewsPath(path + ".pyc")): - if fileExists(getViewsPath(path + ".pyo")): - template = imp.load_compiled(module, getViewsPath(path + ".pyo")) - elif fileExists(getViewsPath(path + ".pyc")): - template = imp.load_compiled(module, getViewsPath(path + ".pyc")) - else: - template = imp.load_source(module, getViewsPath(path + ".py")) + template = None + if fileExists(getViewsPath(path + ".pyo")): + template = self.load_source(module, getViewsPath(path + ".pyo")) + elif fileExists(getViewsPath(path + ".pyc")): + template = self.load_source(module, getViewsPath(path + ".pyc")) + elif fileExists(getViewsPath(path + ".py")): + template = self.load_source(module, getViewsPath(path + ".py")) + if template: mod = getattr(template, module, None) if callable(mod): return str(mod(searchList=args)) - elif fileExists(getViewsPath(path + ".tmpl")): + if fileExists(getViewsPath(path + ".tmpl")): vp = str(getViewsPath(path + ".tmpl")) return str(Template(file=vp, searchList=[args])) return None diff --git a/plugin/httpserver.py b/plugin/httpserver.py index 4fba811da..c6c503b82 100644 --- a/plugin/httpserver.py +++ b/plugin/httpserver.py @@ -38,7 +38,6 @@ from Components.Network import iNetwork import os -import imp import ipaddress import six @@ -158,11 +157,6 @@ def buildRootTree(session): continue loaded.append(modulename) - try: - imp.load_source(modulename, origwebifpath + "/WebChilds/External/" + modulename + ".py") - except Exception as e: - # maybe there's only the compiled version - imp.load_compiled(modulename, origwebifpath + "/WebChilds/External/" + external) if len(loaded_plugins) > 0: for plugin in loaded_plugins: