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

elodie.plugins.plugins   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 6

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 20 3
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