Passed
Pull Request — master (#318)
by Jaisen
02:04
created

elodie.plugins.plugins   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Plugins.__init__() 0 3 1
A Plugins.run_all_before() 0 6 2
A Plugins.load() 0 22 5
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