1
|
|
|
""" |
2
|
|
|
Plugin object. |
3
|
|
|
|
4
|
|
|
.. moduleauthor:: Jaisen Mathai <[email protected]> |
5
|
|
|
""" |
6
|
|
|
from __future__ import print_function |
7
|
|
|
from builtins import object |
8
|
|
|
|
9
|
|
|
from sys import exc_info |
10
|
|
|
from importlib import import_module |
11
|
|
|
|
12
|
|
|
from elodie.config import load_config |
13
|
|
|
from elodie import log |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Plugins(object): |
17
|
|
|
"""A class to execute plugin actions.""" |
18
|
|
|
|
19
|
|
|
def __init__(self): |
20
|
|
|
self.plugins = [] |
21
|
|
|
self.classes = {} |
22
|
|
|
|
23
|
|
|
def load(self): |
24
|
|
|
"""Load plugins from config file. |
25
|
|
|
""" |
26
|
|
|
config = load_config() |
27
|
|
|
if 'Plugins' in config and 'plugins' in config['Plugins']: |
28
|
|
|
config_plugins = config['Plugins']['plugins'].split(',') |
29
|
|
|
for plugin in config_plugins: |
30
|
|
|
plugin_lower = plugin.lower() |
31
|
|
|
try: |
32
|
|
|
# We attempt to do the following. |
33
|
|
|
# 1. Load the module of the plugin. |
34
|
|
|
# 2. Instantiate an object of the plugin's class. |
35
|
|
|
# 3. Add the plugin to the list of plugins. |
36
|
|
|
# |
37
|
|
|
# #3 should only happen if #2 doesn't throw an error |
38
|
|
|
this_module = import_module('elodie.plugins.{}.{}'.format(plugin_lower, plugin_lower)) |
39
|
|
|
self.classes[plugin] = getattr(this_module, plugin)() |
40
|
|
|
# We only append to self.plugins if we're able to load the class |
41
|
|
|
self.plugins.append(plugin) |
42
|
|
|
except: |
43
|
|
|
log.warn('Some error occurred initiating plugin {} - {}'.format(plugin, exc_info()[0])) |
44
|
|
|
continue |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def run_all_before(self, file_path, destination_path, media): |
48
|
|
|
"""Process `before` methods of each plugin that was loaded. |
49
|
|
|
""" |
50
|
|
|
for cls in self.classes: |
51
|
|
|
this_method = getattr(self.classes[cls], 'before') |
52
|
|
|
this_method(file_path, destination_path, media) |
53
|
|
|
|