plugins.py 3.54 KB
Newer Older
1

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
2
import logging
3
import ConfigParser
4
from .models import Response, Leaf
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
5
6
7

logger = logging.getLogger(__name__)

8
9
10
11
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
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"
    },
}
55

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

57
class SenpyPlugin(Leaf):
58
59
    _context = Leaf.get_context(Response._context)
    _frame = {"@context": _context,
60
              "name": {},
61
62
              "extra_params": {"@container": "@index"},
              "@explicit": True,
63
64
              "version": {},
              "repo": None,
65
66
              "is_activated": {},
              "params": None,
67
              }
68

69
70
    def __init__(self, info=None):
        if not info:
71
72
            raise ValueError(("You need to provide configuration"
                              "information for the plugin."))
73
        logger.debug("Initialising {}".format(info))
74
        super(SenpyPlugin, self).__init__()
75
76
        self.name = info["name"]
        self.version = info["version"]
77
        self.id = "{}_{}".format(self.name, self.version)
78
        self.params = info.get("params", PARAMS.copy())
79
80
        if "@id" not in self.params:
            self.params["@id"] = "params_%s" % self.id
81
        self.extra_params = info.get("extra_params", {})
82
83
84
        self.params.update(self.extra_params.copy())
        if "@id" not in self.extra_params:
            self.extra_params["@id"] = "extra_params_%s" % self.id
85
        self.is_activated = False
86
        self._info = info
87
88

    def analyse(self, *args, **kwargs):
89
        logger.debug("Analysing with: {} {}".format(self.name, self.version))
90
91
        pass

92
93
94
95
96
    def activate(self):
        pass

    def deactivate(self):
        pass
97

98
99
100
101
102
103
    def jsonld(self, parameters=False, *args, **kwargs):
        nframe = kwargs.pop("frame", self._frame)
        if parameters:
            nframe = nframe.copy()
            nframe["params"] = {}
        return super(SenpyPlugin, self).jsonld(frame=nframe, *args, **kwargs)
104

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
105
106
107
108
    @property
    def id(self):
        return "{}_{}".format(self.name, self.version)

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

110
class SentimentPlugin(SenpyPlugin):
111

112
113
114
115
    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
116

117

118
class EmotionPlugin(SenpyPlugin):
119

120
121
122
123
    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))