plugins.py 2.91 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
import inspect
import os.path
6
import pickle
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
        super(SenpyPlugin, self).__init__(info)
20
        self.id = '{}_{}'.format(self.name, self.version)
21
        self._info = info
22
        self.is_activated = False
23

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

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

31
32
33
34
35
    def activate(self):
        pass

    def deactivate(self):
        pass
36

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
38
39
    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
40

41
class SentimentPlugin(SenpyPlugin):
42

43
44
45
46
    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
47
        self["@type"] = "marl:SentimentAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
48

49

50
class EmotionPlugin(SenpyPlugin):
51

52
53
54
55
    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
56
        self["@type"] = "onyx:EmotionAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
57
58
59
60
61
62


class ShelfMixin(object):

    @property
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
        if not hasattr(self, '_sh') or self._sh is None:
64
65
66
            self.__dict__['_sh'] = {}
            if os.path.isfile(self.shelf_file):
                self.__dict__['_sh'] = pickle.load(open(self.shelf_file, 'rb'))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
67
68
69
70
        return self._sh

    @sh.deleter
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
71
72
        if os.path.isfile(self.shelf_file):
            os.remove(self.shelf_file)
73
74
            del self.__dict__['_sh']
        self.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
75
76

    def __del__(self):
77
78
        self.save()        
        super(ShelfMixin, self).__del__()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
79
80
81
82
83

    @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:
84
                self.__dict__['_shelf_file'] = self._info['shelf_file']
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
85
            else:
86
                self._shelf_file = os.path.join(self.get_folder(), self.name + '.p')
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
87
88
        return self._shelf_file 

89
90
91
92
93
    def save(self):
        logger.debug('closing pickle')
        if hasattr(self, '_sh') and self._sh is not None:
            with open(self.shelf_file, 'wb') as f:
                pickle.dump(self._sh, f)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94
            del(self.__dict__['_sh'])