From 86f45f814727ff537370e950916a74c29f3abed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Thu, 27 Nov 2014 17:22:25 +0100 Subject: [PATCH] JSON-LD contexts and prefixes --- app.py | 7 ++++--- senpy/models.py | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index 86fe394..3aa175f 100644 --- a/app.py +++ b/app.py @@ -23,12 +23,13 @@ import config from flask import Flask from senpy.extensions import Senpy import logging +import os + logging.basicConfig(level=logging.DEBUG) app = Flask(__name__) - -sp = Senpy() -sp.init_app(app) +mypath = os.path.dirname(os.path.realpath(__file__)) +sp = Senpy(app, os.path.join(mypath, "plugins")) if __name__ == '__main__': app.debug = config.DEBUG diff --git a/senpy/models.py b/senpy/models.py index 6ce4d5d..b53dbb5 100644 --- a/senpy/models.py +++ b/senpy/models.py @@ -4,22 +4,39 @@ from collections import defaultdict class Leaf(defaultdict): - def __init__(self, context=None, ofclass=list): + _prefix = None + + def __init__(self, context=None, prefix=None, ofclass=list): super(Leaf, self).__init__(ofclass) if context: self.context = context + self._prefix = prefix - def __getattr__(self, name): - if name is not "context": - return super(Leaf, self).__getitem__(name) - return self["@context"] + def __getattr__(self, key): + return super(Leaf, self).__getitem__(self._get_key(key)) - def __setattr__(self, name, value): - name = "@context" if name is "context" else name - self[name] = self.get_context(value) + def __setattr__(self, key, value): + try: + object.__getattr__(self, key) + object.__setattr__(self, key, value) + except AttributeError: + key = self._get_key(key) + value = self.get_context(value) if key == "@context" else value + if key[0] == "_": + object.__setattr__(self, key, value) + else: + super(Leaf, self).__setitem__(key, value) - def __delattr__(self, name): - return super(Leaf, self).__delitem__(name) + def __delattr__(self, key): + return super(Leaf, self).__delitem__(self._get_key(key)) + + def _get_key(self, key): + if key is "context": + return "@context" + elif self._prefix: + return "{}:{}".format(self._prefix, key) + else: + return key @staticmethod def get_context(context): @@ -65,6 +82,7 @@ class Opinion(Leaf): } def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs): super(Opinion, self).__init__(context=self.opinionContext, + prefix="marl", *args, **kwargs) if polarityValue is not None: @@ -74,13 +92,12 @@ class Opinion(Leaf): class EmotionSet(Leaf): - emotionContext = { - "@vocab": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#" - } + emotionContext = {} def __init__(self, emotions=None, *args, **kwargs): if not emotions: emotions = [] super(EmotionSet, self).__init__(context=self.emotionContext, + prefix="onyx", *args, **kwargs) self.emotions = emotions or []