Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port away from imp module, use importlib instead #1632

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions plugin/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

from __future__ import print_function
import os
import imp
import json
import six
if six.PY3:
from importlib.util import spec_from_file_location, module_from_spec
else:
import imp

from twisted.web import server, http, resource
from twisted.web.resource import EncodingResourceWrapper
Expand Down Expand Up @@ -117,18 +120,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")) if six.PY3 else imp.load_compiled(module, getViewsPath(path + ".pyo"))
elif fileExists(getViewsPath(path + ".pyc")):
template = self.load_source(module, getViewsPath(path + ".pyc")) if six.PY3 else imp.load_compiled(module, getViewsPath(path + ".pyc"))
elif fileExists(getViewsPath(path + ".py")):
template = self.load_source(module, getViewsPath(path + ".py")) if six.PY3 else imp.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
Loading