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

Replace imp with importlib #365

Merged
merged 2 commits into from
Sep 27, 2023
Merged
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
22 changes: 14 additions & 8 deletions configurations/importer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import imp
import importlib.util
from importlib.machinery import PathFinder
import logging
import os
import sys
Expand Down Expand Up @@ -126,25 +127,30 @@ def stylize(text):
self.name))
self.logger.debug(stylize(message))

def find_module(self, fullname, path=None):
def find_spec(self, fullname, path=None, target=None):
if fullname is not None and fullname == self.module:
module = fullname.rsplit('.', 1)[-1]
return ConfigurationLoader(self.name,
imp.find_module(module, path))
spec = PathFinder.find_spec(fullname, path)
if spec is not None:
return importlib.machinery.ModuleSpec(spec.name,
ConfigurationLoader(self.name, spec),
origin=spec.origin)
return None


class ConfigurationLoader:

def __init__(self, name, location):
def __init__(self, name, spec):
self.name = name
self.location = location
self.spec = spec

def load_module(self, fullname):
if fullname in sys.modules:
mod = sys.modules[fullname] # pragma: no cover
else:
mod = imp.load_module(fullname, *self.location)
mod = importlib.util.module_from_spec(self.spec)
sys.modules[fullname] = mod
self.spec.loader.exec_module(mod)

cls_path = '{0}.{1}'.format(mod.__name__, self.name)

try:
Expand Down