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:
oe-alliance/OpenWebif@b10e510
oe-alliance/OpenWebif@4612bbf

Remove redundant code.

Thank @jbleyel.

Tested with Python 3.9.9 (.pyc files) and Python 3.12 (.py files).
  • Loading branch information
Hains committed Nov 10, 2023
1 parent 7e93d79 commit 48bf9bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
25 changes: 16 additions & 9 deletions plugin/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -117,18 +117,25 @@ 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_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
Expand Down
6 changes: 0 additions & 6 deletions plugin/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from Components.Network import iNetwork

import os
import imp
import ipaddress
import six

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 48bf9bd

Please sign in to comment.