From dfdd9ce10bf02df8dd2959b419fefeff816de899 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 3 Jul 2016 22:58:41 +0200 Subject: [PATCH] factorize module loading --- patacrep/files.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/patacrep/files.py b/patacrep/files.py index 7a9b29ef..967844d4 100644 --- a/patacrep/files.py +++ b/patacrep/files.py @@ -8,7 +8,7 @@ import pkgutil import posixpath import re import sys -from zipimport import ZipImportError +from zipimport import zipimporter from patacrep import utils from patacrep import __DATADIR__ @@ -80,6 +80,13 @@ def chdir(*path): else: 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): """Iterate over modules located in list of `path`. @@ -90,14 +97,7 @@ def iter_modules(path, prefix): yield sys.modules[name] else: try: - yield module_finder.find_spec(name).loader.load_module() - 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 + yield load_module(module_finder, name) except ImportError as error: LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) continue