Skip to content

Commit

Permalink
Port away from imp module, use importlib instead
Browse files Browse the repository at this point in the history
Based on:
https://python-future.org/_modules/imp.html
https://docs.python.org/3/whatsnew/3.12.html

The imp module is deprecated in favour of importlib and slated for removal in Python 3.12.

Tested with Python 3.9.9 (.pyc files) and Python 3.12 (.py files)
  • Loading branch information
Hains committed Nov 9, 2023
1 parent 7e93d79 commit 6f80d60
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
28 changes: 24 additions & 4 deletions plugin/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@

from __future__ import print_function
import os
import imp
import json
import six
from importlib.machinery import SourcelessFileLoader, SourceFileLoader
from importlib import util
from importlib._bootstrap import _load

from twisted.web import server, http, resource
from twisted.web.resource import EncodingResourceWrapper
Expand Down Expand Up @@ -117,14 +119,32 @@ def error404(self, request):
request.write(b"<html><head><title>OpenWebif</title></head><body><h1>Error 404: Not found</h1><br>The requested page doesn't exist.</body></html>")
request.finish()

def load_compiled(self, name, pathname):
loader = SourcelessFileLoader(name, pathname)
spec = util.spec_from_file_location(name, pathname, loader=loader)
module = _load(spec)
module.__loader__ = SourcelessFileLoader(name, pathname)
module.__spec__.loader = module.__loader__
return module

def load_source(self, modname, filename):
loader = SourceFileLoader(modname, filename)
spec = util.spec_from_file_location(modname, filename, loader=loader)
module = util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module

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"))
template = self.load_compiled(module, getViewsPath(path + ".pyo"))
elif fileExists(getViewsPath(path + ".pyc")):
template = imp.load_compiled(module, getViewsPath(path + ".pyc"))
template = self.load_compiled(module, getViewsPath(path + ".pyc"))
else:
template = imp.load_source(module, getViewsPath(path + ".py"))
template = self.load_source(module, getViewsPath(path + ".py"))
mod = getattr(template, module, None)
if callable(mod):
return str(mod(searchList=args))
Expand Down
4 changes: 2 additions & 2 deletions plugin/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
from twisted.internet.error import CannotListenError

from Plugins.Extensions.OpenWebif.controllers.root import RootController
from Plugins.Extensions.OpenWebif.controllers.base import BaseController
from Plugins.Extensions.OpenWebif.sslcertificate import SSLCertificateGenerator, KEY_FILE, CERT_FILE, CA_FILE, CHAIN_FILE
from socket import has_ipv6
from OpenSSL import SSL
from OpenSSL import crypto
from Components.Network import iNetwork

import os
import imp
import ipaddress
import six

Expand Down Expand Up @@ -159,7 +159,7 @@ def buildRootTree(session):

loaded.append(modulename)
try:
imp.load_source(modulename, origwebifpath + "/WebChilds/External/" + modulename + ".py")
BaseController.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)
Expand Down

0 comments on commit 6f80d60

Please sign in to comment.