plugins.py 3.94 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__)

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
PARAMS = {
    "input": {
        "@id": "input",
        "aliases": ["i", "input"],
        "required": True,
        "help": "Input text"
    },
    "informat": {
        "@id": "informat",
        "aliases": ["f", "informat"],
        "required": False,
        "default": "text",
        "options": ["turtle", "text"],
    },
    "intype": {
        "@id": "intype",
        "aliases": ["intype", "t"],
        "required": False,
        "default": "direct",
        "options": ["direct", "url", "file"],
    },
    "outformat": {
        "@id": "outformat",
        "aliases": ["outformat", "o"],
        "default": "json-ld",
        "required": False,
        "options": ["json-ld"],
    },
    "language": {
        "@id": "language",
        "aliases": ["language", "l"],
        "required": False,
    },
    "prefix": {
        "@id": "prefix",
        "aliases": ["prefix", "p"],
        "required": True,
        "default": "",
    },
    "urischeme": {
        "@id": "urischeme",
        "aliases": ["urischeme", "u"],
        "required": False,
        "default": "RFC5147String",
        "options": "RFC5147String"
    },
}
59

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
60

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

63
64
    def __init__(self, info=None):
        if not info:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
65
66
            raise Error(message=("You need to provide configuration"
                                 "information for the plugin."))
67
68
69
70
        logger.debug("Initialising {}".format(info))
        self.name = info["name"]
        self.version = info["version"]
        self.params = info.get("params", PARAMS.copy())
71
72
        if "@id" not in self.params:
            self.params["@id"] = "params_%s" % self.id
73
        self.is_activated = False
74
        self._info = info
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
75
        super(SenpyPlugin, self).__init__()
76

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
77
78
79
    def get_folder(self):
        return os.path.dirname(inspect.getfile(self.__class__))

80
    def analyse(self, *args, **kwargs):
81
        logger.debug("Analysing with: {} {}".format(self.name, self.version))
82
83
        pass

84
85
86
87
88
    def activate(self):
        pass

    def deactivate(self):
        pass
89

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
90
91
92
93
    @property
    def id(self):
        return "{}_{}".format(self.name, self.version)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94
95
96
    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
97

98
class SentimentPlugin(SenpyPlugin):
99

100
101
102
103
    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
104
        self["@type"] = "marl:SentimentAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
105

106

107
class EmotionPlugin(SenpyPlugin):
108

109
110
111
112
    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
113
        self["@type"] = "onyx:EmotionAnalysis"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
114
115
116
117
118
119


class ShelfMixin(object):

    @property
    def sh(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
120
        if not hasattr(self, '_sh') or self._sh is None:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
121
122
123
124
125
            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
126
127
        if os.path.isfile(self.shelf_file):
            os.remove(self.shelf_file)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
128
129
130
131
132
        self.close()

    def __del__(self):
        self.close()        
        self.deactivate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
133
134
135
136
137
138
139
140
141
142
143

    @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):
144
        self.sh.close()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
145
        del(self._sh)