|
1 # -*- coding: utf-8 -*- |
|
2 ''' |
|
3 Created on Jun 11, 2013 |
|
4 |
|
5 @author: ymh |
|
6 ''' |
|
7 |
|
8 from rdflib import plugin, ConjunctiveGraph, URIRef, Literal |
|
9 from rdflib.store import Store |
|
10 from django.db import connections |
|
11 |
|
12 class TermGraph(ConjunctiveGraph): |
|
13 |
|
14 def __init__(self, do_open=False, create=False): |
|
15 identifier = "jocondelab" |
|
16 store = plugin.get("SQLAlchemy", Store)(identifier=identifier) |
|
17 ConjunctiveGraph.__init__(self, store=store, identifier=identifier) |
|
18 if do_open: |
|
19 self.open(create) |
|
20 |
|
21 def open(self, create=False): |
|
22 db_settings = connections['default'].settings_dict |
|
23 sa_db_settings = { |
|
24 'engine': 'postgresql+psycopg2' if db_settings['ENGINE'] == "django.db.backends.postgresql_psycopg2" else db_settings['ENGINE'], |
|
25 'user': db_settings['USER'], |
|
26 'password': db_settings['PASSWORD'], |
|
27 'port': db_settings['PORT'] if db_settings['PORT'] else "5432", |
|
28 'host': db_settings['HOST'] if db_settings['HOST'] else "localhost", |
|
29 'name': db_settings['NAME'] |
|
30 } |
|
31 connect_config = "%(engine)s://%(user)s:%(password)s@%(host)s:%(port)s/%(name)s"%sa_db_settings |
|
32 |
|
33 return ConjunctiveGraph.open(self, connect_config, create=create) |
|
34 |
|
35 def get_uri_for_term(self, term, context): |
|
36 c = URIRef(context) |
|
37 tl = Literal(term) |
|
38 |
|
39 for s,p,_ in self.triples((None, None, tl), context=c): |
|
40 if p in [URIRef("http://www.w3.org/2004/02/skos/core#prefLabel"), URIRef("http://www.w3.org/2004/02/skos/core#alternateLabel")]: |
|
41 return unicode(s) |
|
42 return None |
|
43 |
|
44 graph = TermGraph(do_open=True, create=False) |