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

logger = logging.getLogger(__name__)

13

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
14
class SenpyPlugin(PluginModel):
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

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
43
class SentimentPlugin(SenpyPlugin):
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
    def __init__(self, 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
55
        self["@type"] = "onyx:EmotionAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
56
57
58
59
60


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

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

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

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

88
89
90
91
92
    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
YAPFed    
J. Fernando Sánchez committed
93
            del (self.__dict__['_sh'])