Browse Source

factorize module loading

pull/235/head
Oliverpool 9 years ago
parent
commit
dfdd9ce10b
  1. 18
      patacrep/files.py

18
patacrep/files.py

@ -8,7 +8,7 @@ import pkgutil
import posixpath import posixpath
import re import re
import sys import sys
from zipimport import ZipImportError from zipimport import zipimporter
from patacrep import utils from patacrep import utils
from patacrep import __DATADIR__ from patacrep import __DATADIR__
@ -80,6 +80,13 @@ def chdir(*path):
else: else:
yield yield
def load_module(module_finder, name):
"""Load a custom module, be it a zipimporter or a FileFinder"""
if isinstance(module_finder, zipimporter):
return module_finder.load_module(name)
else:
return module_finder.find_spec(name).loader.load_module()
def iter_modules(path, prefix): def iter_modules(path, prefix):
"""Iterate over modules located in list of `path`. """Iterate over modules located in list of `path`.
@ -90,14 +97,7 @@ def iter_modules(path, prefix):
yield sys.modules[name] yield sys.modules[name]
else: else:
try: try:
yield module_finder.find_spec(name).loader.load_module() yield load_module(module_finder, name)
except AttributeError:
# It is a zipimport.zipimporter object
try:
yield module_finder.load_module(name)
except ZipImportError as error:
LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error)))
continue
except ImportError as error: except ImportError as error:
LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error)))
continue continue

Loading…
Cancel
Save