comparison piecrust/plugins/base.py @ 910:371731b555ec

plugins: Fix a crash bug when the plugins directory doesn't exist.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 23 Jul 2017 18:01:58 -0700
parents 4850f8c21b6e
children 45ad976712ec
comparison
equal deleted inserted replaced
909:eed19a80c00e 910:371731b555ec
115 115
116 to_install = self.app.config.get('site/plugins') 116 to_install = self.app.config.get('site/plugins')
117 if to_install: 117 if to_install:
118 for name in to_install: 118 for name in to_install:
119 plugin = self._loadPlugin(name) 119 plugin = self._loadPlugin(name)
120 self._plugins.append(plugin) 120 if plugin is not None:
121 self._plugins.append(plugin)
121 122
122 for plugin in self._plugins: 123 for plugin in self._plugins:
123 plugin.initialize(self.app) 124 plugin.initialize(self.app)
124 125
125 def _loadPlugin(self, plugin_name): 126 def _loadPlugin(self, plugin_name):
128 # Import from the current environment. 129 # Import from the current environment.
129 mod = importlib.import_module(mod_name) 130 mod = importlib.import_module(mod_name)
130 except ImportError as ex: 131 except ImportError as ex:
131 mod = None 132 mod = None
132 133
133 if mod is None: 134 if mod is None and self.app.plugins_dir:
134 # Import as a loose Python file from the plugins dir. 135 # Import as a loose Python file from the plugins dir.
135 pfile = os.path.join(self.app.plugins_dir, plugin_name + '.py') 136 pfile = os.path.join(self.app.plugins_dir, plugin_name + '.py')
136 if os.path.isfile(pfile): 137 if os.path.isfile(pfile):
137 spec = importlib.util.spec_from_file_location(plugin_name, 138 spec = importlib.util.spec_from_file_location(plugin_name,
138 pfile) 139 pfile)