comparison piecrust/plugins/base.py @ 984:e7ca3c577305

fix: Compatibility for Python 3.4.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 30 Oct 2017 19:23:10 -0700
parents 45ad976712ec
children 2e5c5d33d62c
comparison
equal deleted inserted replaced
983:7fe1151595bf 984:e7ca3c577305
1 import os.path 1 import os.path
2 import sys 2 import sys
3 import logging 3 import logging
4 import importlib 4 import importlib
5 import importlib.util
6 5
7 6
8 logger = logging.getLogger(__name__) 7 logger = logging.getLogger(__name__)
9 8
10 9
133 132
134 if mod is None and self.app.plugins_dir: 133 if mod is None and self.app.plugins_dir:
135 # Import as a loose Python file from the plugins dir. 134 # Import as a loose Python file from the plugins dir.
136 pfile = os.path.join(self.app.plugins_dir, plugin_name + '.py') 135 pfile = os.path.join(self.app.plugins_dir, plugin_name + '.py')
137 if os.path.isfile(pfile): 136 if os.path.isfile(pfile):
138 spec = importlib.util.spec_from_file_location(plugin_name, 137 if sys.version_info[1] >= 5:
139 pfile) 138 # Python 3.5+
140 mod = importlib.util.module_from_spec(spec) 139 from importlib.util import (spec_from_file_location,
141 spec.loader.exec_module(mod) 140 module_from_spec)
142 sys.modules[mod_name] = mod 141 spec = spec_from_file_location(plugin_name, pfile)
142 mod = module_from_spec(spec)
143 spec.loader.exec_module(mod)
144 sys.modules[mod_name] = mod
145 else:
146 # Python 3.4, 3.3.
147 from importlib.machinery import SourceFileLoader
148 mod = SourceFileLoader(
149 plugin_name, pfile).load_module()
150 sys.modules[mod_name] = mod
143 151
144 if mod is None: 152 if mod is None:
145 logger.error("Failed to load plugin '%s'." % plugin_name) 153 logger.error("Failed to load plugin '%s'." % plugin_name)
146 return 154 return
147 155