plugins.py 2.93 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
8
import tempfile
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9
from .models import Response, PluginModel, Error
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10
11
12

logger = logging.getLogger(__name__)

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

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

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

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

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

    def deactivate(self):
        pass
37

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

42
class SentimentPlugin(SenpyPlugin):
43

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

50

51
class EmotionPlugin(SenpyPlugin):
52

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


class ShelfMixin(object):

    @property
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
64
        if not hasattr(self, '_sh') or self._sh is None:
65
66
67
            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
68
69
70
71
        return self._sh

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

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

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

90
91
92
93
94
    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
95
            del(self.__dict__['_sh'])