Switch to module imports for importlib in utils

The present day guidance is to lean towards module imports for the
stdlib modules. Switch importlib imports to this instead of
plucking out the functions that we need. This makes it more
immediately apparent as to where the functions are coming from
in the application logic.
This commit is contained in:
Greg Taylor 2019-09-15 18:24:48 -07:00
parent da48fa2e52
commit 901277ea64

View file

@ -16,12 +16,12 @@ import textwrap
import random import random
import inspect import inspect
import traceback import traceback
import importlib
import importlib.util
import importlib.machinery import importlib.machinery
from twisted.internet.task import deferLater from twisted.internet.task import deferLater
from twisted.internet.defer import returnValue # noqa - used as import target from twisted.internet.defer import returnValue # noqa - used as import target
from os.path import join as osjoin from os.path import join as osjoin
from importlib import import_module
from importlib.util import find_spec
from inspect import ismodule, trace, getmembers, getmodule, getmro from inspect import ismodule, trace, getmembers, getmodule, getmro
from collections import defaultdict, OrderedDict from collections import defaultdict, OrderedDict
from twisted.internet import threads, reactor from twisted.internet import threads, reactor
@ -1216,7 +1216,7 @@ def mod_import(module):
return mod_import_from_path(module) return mod_import_from_path(module)
try: try:
return import_module(module) return importlib.import_module(module)
except ImportError: except ImportError:
return None return None
@ -1378,7 +1378,7 @@ def fuzzy_import_from_module(path, variable, default=None, defaultpaths=None):
paths = [path] + make_iter(defaultpaths) paths = [path] + make_iter(defaultpaths)
for modpath in paths: for modpath in paths:
try: try:
mod = import_module(modpath) mod = importlib.import_module(modpath)
except ImportError as ex: except ImportError as ex:
if not str(ex).startswith("No module named %s" % modpath): if not str(ex).startswith("No module named %s" % modpath):
# this means the module was found but it # this means the module was found but it
@ -1420,13 +1420,13 @@ def class_from_module(path, defaultpaths=None):
raise ImportError("the path '%s' is not on the form modulepath.Classname." % path) raise ImportError("the path '%s' is not on the form modulepath.Classname." % path)
try: try:
if not find_spec(testpath, package='evennia'): if not importlib.util.find_spec(testpath, package='evennia'):
continue continue
except ModuleNotFoundError: except ModuleNotFoundError:
continue continue
try: try:
mod = import_module(testpath, package='evennia') mod = importlib.import_module(testpath, package='evennia')
except ModuleNotFoundError: except ModuleNotFoundError:
err = traceback.format_exc(30) err = traceback.format_exc(30)
break break