コード例 #1
0
ファイル: thingview.py プロジェクト: libris/lxlviewer
class Things(object):
    def __init__(self, config):
        self.lang = config['LANG']
        self.vocab_uri = config['VOCAB_IRI']
        self.ui_defs = ui_defs

        self._storage = Storage('lddb',
                config['DBNAME'], config.get('DBHOST', '127.0.0.1'),
                config.get('DBUSER'), config.get('DBPASSWORD'))

        elastic = Elasticsearch(config['ESHOST'],
                sniff_on_start=config.get('ES_SNIFF_ON_START', True),
                sniff_on_connection_fail=True, sniff_timeout=60,
                sniffer_timeout=300, timeout=10)

        #ns_mgr = Graph().parse('sys/context/base.jsonld',
        #        format='json-ld').namespace_manager
        #ns_mgr.bind("", vocab_uri)
        #graphcache = GraphCache(config['GRAPH_CACHE'])
        #graphcache.graph.namespace_manager = ns_mgr

        vocab = VocabView(self.load_vocab_graph(), self.vocab_uri, lang=self.lang)

        self.ldview = DataView(vocab, self._storage, elastic, config['ES_INDEX'])

    def get_site(self, site_id):
        # TODO: ldview.get_record_data(g.current_base)
        return sites.get(site_id)

    def load_vocab_graph(self):
        try:
            context_uri = self.vocab_uri + 'context'
            context = self._storage.get_record(context_uri)
            if context is None:
                raise Exception('Failed to get context from storage ', context_uri, context)

            self.jsonld_context_data = context.data

            #vocabgraph = graphcache.load(config['VOCAB_SOURCE'])
            vocab_items = sum((record.data[GRAPH] for record in
                        self._storage.find_by_quotation(self.vocab_uri, limit=4096)),
                        self._storage.get_record(self.vocab_uri).data[GRAPH])
            vocabdata = json.dumps(vocab_items, indent=2)
            vocabgraph = Graph().parse(
                    data=vocabdata,
                    context=self.jsonld_context_data,
                    format='json-ld')
            #vocabgraph.namespace_manager = ns_mgr
            vocabgraph.namespace_manager.bind("", self.vocab_uri)
        finally:
            self._storage.disconnect()

        # TODO: load base vocabularies for labels, inheritance here,
        # or in vocab build step?
        #for url in vocabgraph.objects(None, OWL.imports):
        #    graphcache.load(vocab_source_map.get(str(url), url))

        return vocabgraph

    def get_vocab_util(self):
        return VocabUtil(self.load_vocab_graph(), self.lang)