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 #1631

Closed
wants to merge 1 commit 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: 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
Loading