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 importlib import import_module |
10
|
|
|
from sys import exc_info |
11
|
|
|
from traceback import format_exc |
12
|
|
|
|
13
|
|
|
from elodie.config import load_plugin_config |
14
|
|
|
from elodie import log |
15
|
|
|
|
16
|
|
|
class ElodiePluginError(Exception): |
17
|
|
|
pass |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class PluginBase(object): |
21
|
|
|
|
22
|
|
|
__name__ = 'PluginBase' |
23
|
|
|
|
24
|
|
|
def log(self, msg): |
25
|
|
|
log.info(msg) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class Plugins(object): |
30
|
|
|
"""A class to execute plugin actions.""" |
31
|
|
|
|
32
|
|
|
def __init__(self): |
33
|
|
|
self.plugins = [] |
34
|
|
|
self.classes = {} |
35
|
|
|
self.loaded = False |
36
|
|
|
|
37
|
|
|
def load(self): |
38
|
|
|
"""Load plugins from config file. |
39
|
|
|
""" |
40
|
|
|
# If plugins have been loaded then return |
41
|
|
|
if self.loaded == True: |
42
|
|
|
return |
43
|
|
|
|
44
|
|
|
plugin_list = load_plugin_config() |
45
|
|
|
for plugin in plugin_list: |
46
|
|
|
plugin_lower = plugin.lower() |
47
|
|
|
try: |
48
|
|
|
# We attempt to do the following. |
49
|
|
|
# 1. Load the module of the plugin. |
50
|
|
|
# 2. Instantiate an object of the plugin's class. |
51
|
|
|
# 3. Add the plugin to the list of plugins. |
52
|
|
|
# |
53
|
|
|
# #3 should only happen if #2 doesn't throw an error |
54
|
|
|
this_module = import_module('elodie.plugins.{}.{}'.format(plugin_lower, plugin_lower)) |
55
|
|
|
self.classes[plugin] = getattr(this_module, plugin)() |
56
|
|
|
# We only append to self.plugins if we're able to load the class |
57
|
|
|
self.plugins.append(plugin) |
58
|
|
|
except: |
59
|
|
|
log.error('An error occurred initiating plugin {}'.format(plugin)) |
60
|
|
|
log.error(format_exc()) |
61
|
|
|
|
62
|
|
|
self.loaded = True |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def run_all_before(self, file_path, destination_path, media): |
66
|
|
|
self.load() |
67
|
|
|
"""Process `before` methods of each plugin that was loaded. |
68
|
|
|
""" |
69
|
|
|
pass_status = True |
70
|
|
|
for cls in self.classes: |
71
|
|
|
this_method = getattr(self.classes[cls], 'before') |
72
|
|
|
# We try to call the plugin's `before()` method. |
73
|
|
|
# If the method explicitly raises an ElodiePluginError we'll fail the import |
74
|
|
|
# by setting pass_status to False. |
75
|
|
|
# If any other error occurs we log the message and proceed as usual. |
76
|
|
|
# By default, plugins don't change behavior. |
77
|
|
|
try: |
78
|
|
|
this_method(file_path, destination_path, media) |
79
|
|
|
except ElodiePluginError as err: |
80
|
|
|
log.warn('Plugin {} raised an exception: {}'.format(cls, err)) |
81
|
|
|
log.error(format_exc()) |
82
|
|
|
pass_status = False |
83
|
|
|
except: |
84
|
|
|
log.error(format_exc()) |
85
|
|
|
return pass_status |
86
|
|
|
|