comparison piecrust/plugins/base.py @ 1005:2e5c5d33d62c

Merge changes.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 21 Nov 2017 22:07:12 -0800
parents e7ca3c577305 46025a1b5434
children 8af2ea1f5c34
comparison
equal deleted inserted replaced
1004:4f2e0136123d 1005:2e5c5d33d62c
128 # Import from the current environment. 128 # Import from the current environment.
129 mod = importlib.import_module(mod_name) 129 mod = importlib.import_module(mod_name)
130 except ImportError as ex: 130 except ImportError as ex:
131 mod = None 131 mod = None
132 132
133 if mod is None and self.app.plugins_dir: 133 if mod is None:
134 # Import as a loose Python file from the plugins dir. 134 # Import as a loose Python file from the plugins dir.
135 pfile = os.path.join(self.app.plugins_dir, plugin_name + '.py') 135 for plugins_dir in self.app.plugins_dirs:
136 if os.path.isfile(pfile): 136 pfile = os.path.join(plugins_dir, plugin_name + '.py')
137 if sys.version_info[1] >= 5: 137 if os.path.isfile(pfile):
138 # Python 3.5+ 138 if sys.version_info[1] >= 5:
139 from importlib.util import (spec_from_file_location, 139 # Python 3.5+
140 module_from_spec) 140 from importlib.util import (spec_from_file_location,
141 spec = spec_from_file_location(plugin_name, pfile) 141 module_from_spec)
142 mod = module_from_spec(spec) 142 spec = spec_from_file_location(plugin_name, pfile)
143 spec.loader.exec_module(mod) 143 mod = module_from_spec(spec)
144 sys.modules[mod_name] = mod 144 spec.loader.exec_module(mod)
145 else: 145 sys.modules[mod_name] = mod
146 # Python 3.4, 3.3. 146 else:
147 from importlib.machinery import SourceFileLoader 147 # Python 3.4, 3.3.
148 mod = SourceFileLoader( 148 from importlib.machinery import SourceFileLoader
149 plugin_name, pfile).load_module() 149 mod = SourceFileLoader(
150 sys.modules[mod_name] = mod 150 plugin_name, pfile).load_module()
151 sys.modules[mod_name] = mod
151 152
152 if mod is None: 153 if mod is None:
153 logger.error("Failed to load plugin '%s'." % plugin_name) 154 logger.error("Failed to load plugin '%s'." % plugin_name)
154 return 155 return
155 156