plugins.py 2.75 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
from future import standard_library
standard_library.install_aliases()
3

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4
5
6
import inspect
import os.path
import shelve
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
import logging
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
8
from .models import Response, PluginModel, Error
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9
10
11

logger = logging.getLogger(__name__)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
12
class SenpyPlugin(PluginModel):
13

14
15
    def __init__(self, info=None):
        if not info:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
17
            raise Error(message=("You need to provide configuration"
                                 "information for the plugin."))
18
        logger.debug("Initialising {}".format(info))
19
20
21
        super(SenpyPlugin, self).__init__(info)
        self.params = info.get("extra_params", {})
        self._info = info
22
23
        if "@id" not in self.params:
            self.params["@id"] = "params_%s" % self.id
24
        self.is_activated = False
25

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
26
27
28
    def get_folder(self):
        return os.path.dirname(inspect.getfile(self.__class__))

29
    def analyse(self, *args, **kwargs):
30
        logger.debug("Analysing with: {} {}".format(self.name, self.version))
31
32
        pass

33
34
35
36
37
    def activate(self):
        pass

    def deactivate(self):
        pass
38

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
39
40
41
42
    @property
    def id(self):
        return "{}_{}".format(self.name, self.version)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
43
44
45
    def __del__(self):
        ''' Destructor, to make sure all the resources are freed '''
        self.deactivate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46

47
class SentimentPlugin(SenpyPlugin):
48

49
50
51
52
    def __init__(self, info, *args, **kwargs):
        super(SentimentPlugin, self).__init__(info, *args, **kwargs)
        self.minPolarityValue = float(info.get("minPolarityValue", 0))
        self.maxPolarityValue = float(info.get("maxPolarityValue", 1))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
53
        self["@type"] = "marl:SentimentAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
54

55

56
class EmotionPlugin(SenpyPlugin):
57

58
59
60
61
    def __init__(self, info, *args, **kwargs):
        resp = super(EmotionPlugin, self).__init__(info, *args, **kwargs)
        self.minEmotionValue = float(info.get("minEmotionValue", 0))
        self.maxEmotionValue = float(info.get("maxEmotionValue", 0))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
62
        self["@type"] = "onyx:EmotionAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
64
65
66
67
68


class ShelfMixin(object):

    @property
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
69
        if not hasattr(self, '_sh') or self._sh is None:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
70
71
72
73
74
            self._sh = shelve.open(self.shelf_file, writeback=True)
        return self._sh

    @sh.deleter
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
75
76
        if os.path.isfile(self.shelf_file):
            os.remove(self.shelf_file)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
77
78
79
80
81
        self.close()

    def __del__(self):
        self.close()        
        self.deactivate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
82
83
84
85
86
87
88
89
90
91
92

    @property
    def shelf_file(self):
        if not hasattr(self, '_shelf_file') or not self._shelf_file:
            if hasattr(self, '_info') and 'shelf_file' in self._info:
                self._shelf_file = self._info['shelf_file']
            else:
                self._shelf_file = os.path.join(self.get_folder(), self.name + '.db')
        return self._shelf_file 

    def close(self):
93
        self.sh.close()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94
        del(self._sh)