Passed
Pull Request — master (#318)
by Jaisen
01:43
created

elodie.plugins.plugins.Plugins.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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_plugin_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
        plugin_list = load_plugin_config()
27
        for plugin in plugin_list:
28
            plugin_lower = plugin.lower()
29
            try:
30
                # We attempt to do the following.
31
                #  1. Load the module of the plugin.
32
                #  2. Instantiate an object of the plugin's class.
33
                #  3. Add the plugin to the list of plugins.
34
                #  
35
                #  #3 should only happen if #2 doesn't throw an error
36
                this_module = import_module('elodie.plugins.{}.{}'.format(plugin_lower, plugin_lower))
37
                self.classes[plugin] = getattr(this_module, plugin)()
38
                # We only append to self.plugins if we're able to load the class
39
                self.plugins.append(plugin)
40
            except:
41
                log.warn('Some error occurred initiating plugin {} - {}'.format(plugin, exc_info()[0]))
42
                continue
43
44
45
    def run_all_before(self, file_path, destination_path, media):
46
        """Process `before` methods of each plugin that was loaded.
47
        """
48
        for cls in self.classes:
49
            this_method = getattr(self.classes[cls], 'before')
50
            this_method(file_path, destination_path, media)
51