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