当前位置: 首页>>代码示例>>Python>>正文


Python TripleStore.connect方法代码示例

本文整理汇总了Python中ferenda.TripleStore.connect方法的典型用法代码示例。如果您正苦于以下问题:Python TripleStore.connect方法的具体用法?Python TripleStore.connect怎么用?Python TripleStore.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ferenda.TripleStore的用法示例。


在下文中一共展示了TripleStore.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: tearDown

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
 def tearDown(self):
     self.setupclass = False # make sure super.tearDown deletes all files
     super(AdvancedAPI, self).tearDown()
     FulltextIndex.connect(self.indextype, self.indexlocation,
                           [DocumentRepository()]).destroy()
     TripleStore.connect(self.storetype, self.storelocation,
                         self.storerepository).clear()
开发者ID:staffanm,项目名称:ferenda,代码行数:9,代码来源:integrationAPI.py

示例2: test_sqlite_add_serialized

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def test_sqlite_add_serialized(self, mock_graph):
        store = TripleStore.connect("SQLITE", "", "")
        store.add_serialized("tripledata", "nt")
        self.assertTrue(mock_graph.return_value.parse.called)
        self.assertTrue(mock_graph.return_value.commit.called)
        mock_graph.reset_mock()
        
        store.add_serialized("tripledata", "nt", "namedgraph")
        self.assertTrue(mock_graph.return_value.get_context.called)
        self.assertTrue(mock_graph.return_value.get_context.return_value.parse.called)

        store = TripleStore.connect("SQLITE", "", "", inmemory=True)
        with self.assertRaises(errors.TriplestoreError):
            store.add_serialized("tripledata", "nt")
开发者ID:staffanm,项目名称:ferenda,代码行数:16,代码来源:testTripleStore.py

示例3: test_fuseki_get_serialized

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
 def test_fuseki_get_serialized(self, mock_get):
     store = TripleStore.connect("FUSEKI", "", "", curl=False)
     # test 1: a namedgraph (cases with no context are already run by
     # test_fuseki_get_serialized_file)
     want = util.readfile("test/files/triplestore/namedgraph.nt", "rb")
     got = store.get_serialized(context="namedgraph") # results in single get
     self.assertEqual(want, got)
开发者ID:staffanm,项目名称:ferenda,代码行数:9,代码来源:testTripleStore.py

示例4: test_sqlite_add_serialized_file

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
 def test_sqlite_add_serialized_file(self, mock_graph):
     store = TripleStore.connect("SQLITE", "", "")
     fd, tmpname = mkstemp()
     fp = os.fdopen(fd, "w")
     fp.write("tripledata")
     fp.close()
     store.add_serialized_file(tmpname, "nt")
     os.unlink(tmpname)
开发者ID:staffanm,项目名称:ferenda,代码行数:10,代码来源:testTripleStore.py

示例5: test_sqlite_clear

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
 def test_sqlite_clear(self, mock_graph):
     store = TripleStore.connect("SQLITE", "", "")
     g = Graph()
     g.add((URIRef("http://example.org/doc1"), RDFS.comment, Literal("Hey")))
     g.add((URIRef("http://example.org/doc2"), RDFS.comment, Literal("Ho")))
     mock_graph.return_value.get_context.return_value = g
     store.clear("namedgraph")
     self.assertEqual(2, mock_graph.return_value.remove.call_count)
     self.assertEqual(1, mock_graph.return_value.commit.call_count)
开发者ID:staffanm,项目名称:ferenda,代码行数:11,代码来源:testTripleStore.py

示例6: test_sqlite_init

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def test_sqlite_init(self, mock_graph):
        # create a new db that doesnt exist
        mock_graph.open.return_value = 42
        store = TripleStore.connect("SQLITE", "", "")
        self.assertTrue(mock_graph.return_value.open.called)
        self.assertTrue(mock_graph.return_value.open.call_args[1]['create'])

        # reopen an existing db
        fd, tmpname = mkstemp()
        fp = os.fdopen(fd)
        fp.close()
        store = TripleStore.connect("SQLITE", tmpname, "")
        os.unlink(tmpname)
        self.assertFalse(mock_graph.return_value.open.call_args[1]['create'])

        # make an inmemory db
        store = TripleStore.connect("SQLITE", "", "", inmemory=True)
        self.assertTrue(mock_graph.return_value.quads.called)
        self.assertTrue(mock_graph.return_value.addN.called)
开发者ID:staffanm,项目名称:ferenda,代码行数:21,代码来源:testTripleStore.py

示例7: download

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def download(self, basefile=None):
        # Get all "term sets" (used dcterms:subject Objects, wiki pages
        # describing legal concepts, swedish wikipedia pages...)
        terms = defaultdict(dict)

        # 1) Query the triplestore for all dcterms:subject triples (is this
        # semantically sensible for a "download" action -- the content
        # isn't really external?) -- term set "subjects" (these come
        # from both court cases and legal definitions in law text)
        sq = """
        PREFIX dcterms:<http://purl.org/dc/terms/>
        PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>

        SELECT ?uri ?subject ?label
        WHERE { {?uri dcterms:subject ?subject . }
                OPTIONAL {?subject rdfs:label ?label . } }
        """
        store = TripleStore.connect(self.config.storetype,
                                    self.config.storelocation,
                                    self.config.storerepository)
        results = store.select(sq, "python")
        for row in results:
            if 'label' in row:
                label = row['label']
            else:
                label = self.basefile_from_uri(row['subject'])
                if label is None:
                    self.log.warning("could not determine keyword from %s" % row['subject'])
                    continue
            
            sanitized = self.sanitize_term(label)
            if sanitized:
                if sanitized not in terms:
                    terms[sanitized]['subjects'] = []
                terms[sanitized]['subjects'].append(row['uri'])

        self.log.debug("Retrieved %s subject terms from triplestore" % len(terms))

        for termset_func in self.termset_funcs:
            termset_func(terms)

        for term in terms:
            term = self.sanitize_term(term)
            if not term:
                continue
            oldterms = ""
            termpath = self.store.downloaded_path(term)
            if os.path.exists(termpath):
                oldterms = yaml.load(util.readfile(termpath))
            if terms[term] != oldterms:
                util.ensure_dir(termpath)
                util.writefile(termpath, yaml.dump(terms[term], default_flow_style=False))
                self.log.info("%s: in %s termsets" % (term, len(terms[term])))
            else:
                self.log.debug("%s: skipped" % term)
开发者ID:staffanm,项目名称:ferenda,代码行数:57,代码来源:keyword.py

示例8: test_sesame_add_serialized

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def test_sesame_add_serialized(self, mock_post):
        store = TripleStore.connect("SESAME", "", "")
        rf = util.readfile
        store.add_serialized(rf("test/files/triplestore/defaultgraph.ttl"),
                             format="turtle")
        self.assertEqual(mock_post.call_count, 1)

        store.add_serialized(rf("test/files/triplestore/namedgraph.nt"),
                             format="nt",
                             context="namedgraph")
        self.assertEqual(mock_post.call_count, 2)
开发者ID:staffanm,项目名称:ferenda,代码行数:13,代码来源:testTripleStore.py

示例9: test_sesame_get_serialized

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def test_sesame_get_serialized(self, mock_get):
        store = TripleStore.connect("SESAME", "", "")
        want = util.readfile("test/files/triplestore/combinedgraph.nt", "rb")
        got = store.get_serialized() 
        self.assertEqual(want, got)
        self.assertEqual(mock_get.call_count, 1)

        want = util.readfile("test/files/triplestore/namedgraph.nt", "rb")
        got = store.get_serialized(context="namedgraph") # results in single get
        self.assertEqual(want, got)
        self.assertEqual(mock_get.call_count, 2)
开发者ID:staffanm,项目名称:ferenda,代码行数:13,代码来源:testTripleStore.py

示例10: test_curl

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def test_curl(self, runcmd_mock):
        # needs to test add_serialized, add_serialized_file, get_serialized
        # and get_serialized_file. We'll patch util.runcmd and make sure that
        # the command line is correct. We should also have util.runcmd return
        # a non-zero return code once.
        # our util.runcmd replacement should, for the get_serialized file,
        # create a suitable temp file

        store = TripleStore.connect("FUSEKI", "", "", curl=True)
        # 1. add_serialized
        runcmd_mock.return_value = (0, "", "")
        store.add_serialized("tripledata", "nt")
        cmdline = runcmd_mock.call_args[0][0] # first ordered argument
        # replace the temporary file name
        cmdline = re.sub('"@[^"]+"', '"@tempfile.nt"', cmdline)
        self.assertEqual('curl -X POST --data-binary "@tempfile.nt" --header "Content-Type:application/n-triples;charset=UTF-8" "//data?default"', cmdline)
        runcmd_mock.mock_reset()

        # 2. add_serialized_file
        runcmd_mock.return_value = (0, "", "")
        store.add_serialized_file("tempfile.nt", "nt")
        cmdline = runcmd_mock.call_args[0][0] # first ordered argument
        self.assertEqual('curl -X POST --data-binary "@tempfile.nt" --header "Content-Type:application/n-triples;charset=UTF-8" "//data?default"', cmdline)
        runcmd_mock.mock_reset()

        # 3. get_serialized
        def create_tempfile(*args, **kwargs):
            filename = re.search('-o "([^"]+)"', args[0]).group(1)
            with open(filename, "wb") as fp:
                fp.write("tripledata\n".encode())
            return (0, "", "")
        runcmd_mock.side_effect = create_tempfile
        res = store.get_serialized("nt")
        self.assertEqual(b"tripledata\ntripledata\n", res)
        cmdline = runcmd_mock.call_args[0][0] # first ordered argument
        # replace the temporary file name
        cmdline = re.sub('-o "[^"]+"', '-o "tempfile.nt"', cmdline)
        # FIXME is this really right?
        self.assertEqual('curl -o "tempfile.nt" --header "Accept:application/n-triples" "//data?graph=urn:x-arq:UnionGraph"', cmdline)
        runcmd_mock.side_effect = None
        runcmd_mock.mock_reset()

        # 4. get_serialized_file
        store.get_serialized_file("triples.xml", "xml")
        cmdline = runcmd_mock.call_args[0][0] # first ordered argument
        self.assertEqual('curl -o "triples.xml" --header "Accept:application/rdf+xml" "//data?default"', cmdline)
        runcmd_mock.mock_reset()

        # 5. handle errors
        with self.assertRaises(errors.TriplestoreError):
            runcmd_mock.return_value = (1, "", "Internal error")
            store.get_serialized_file("triples.nt", "nt")
开发者ID:staffanm,项目名称:ferenda,代码行数:54,代码来源:testTripleStore.py

示例11: test_sesame_construct

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def test_sesame_construct(self, mock_get):
        store = TripleStore.connect("SESAME", "", "")
        rf = util.readfile
        want = Graph()
        want.parse(data=rf("test/files/triplestore/construct-results.ttl"),
                   format="turtle")
        got = store.construct("the-query")
        self.assertEqualGraphs(want, got)
        self.assertEqual(mock_get.call_count, 1)

        with self.assertRaises(errors.TriplestoreError):
            mock_get.side_effect = requests.exceptions.HTTPError("Server error")
            got = store.construct("the-query")
开发者ID:staffanm,项目名称:ferenda,代码行数:15,代码来源:testTripleStore.py

示例12: test_sqlite_construct

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
 def test_sqlite_construct(self, mock_graph):
     store = TripleStore.connect("SQLITE", "", "")
     sq = """CONSTRUCT ?s ?p ?o WHERE {?o ?p ?s . }"""
     g = Graph()
     g.add((URIRef("http://example.org/doc1"), RDFS.comment, Literal("Hey")))
     g.add((URIRef("http://example.org/doc2"), RDFS.comment, Literal("Ho")))
     res = Mock
     res.graph = g
     mock_graph.return_value.query.return_value = res
     self.assertEqual(g, store.construct(sq))
 
     mock_graph.return_value.query.side_effect = pyparsing.ParseException("Syntax error")
     with self.assertRaises(errors.SparqlError):
         store.construct(sq)
开发者ID:staffanm,项目名称:ferenda,代码行数:16,代码来源:testTripleStore.py

示例13: prep_annotation_file

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def prep_annotation_file(self, basefile):
        uri = self.canonical_uri(basefile)
        keyword = basefile
        store = TripleStore.connect(self.config.storetype,
                                    self.config.storelocation,
                                    self.config.storerepository)

        # Use SPARQL queries to create a rdf graph (to be used by the
        # xslt transform) containing the wiki authored
        # dcterms:description for this term. FIXME: This isn't a real
        # RDF graph yet.
        wikidesc = self.time_store_select(store,
                                          "sparql/keyword_subjects.rq",
                                          basefile,
                                          None,
                                          "descriptions")

        # compatibility hack to enable lxml to process qnames for namespaces
        def ns(string):
            if ":" in string:
                prefix, tag = string.split(":", 1)
                return "{%s}%s" % (str(self.ns[prefix]), tag)

        # FIXME: xhv MUST be part of nsmap
        if 'xhtml' not in self.ns:
            self.ns['xhtml'] = "http://www.w3.org/1999/xhtml"

        root_node = etree.Element(ns("rdf:RDF"), nsmap=self.ns)

        main_node = etree.SubElement(root_node, ns("rdf:Description"))
        main_node.set(ns("rdf:about"), uri)

        for d in wikidesc:
            desc_node = etree.SubElement(main_node, ns("dcterms:description"))
            xhtmlstr = "<div xmlns='http://www.w3.org/1999/xhtml'>%s</div>" % (d['desc'])
            # xhtmlstr = xhtmlstr.replace(
            #    ' xmlns="http://www.w3.org/1999/xhtml"', '')
            desc_node.append(etree.fromstring(xhtmlstr.encode('utf-8')))

        # subclasses override this to add extra annotations from other
        # sources
        self.prep_annotation_file_termsets(basefile, main_node)

        treestring = etree.tostring(root_node,
                                    encoding="utf-8",
                                    pretty_print=True)
        with self.store.open_annotation(basefile, mode="wb") as fp:
            fp.write(treestring)
        return self.store.annotation_path(basefile)
开发者ID:staffanm,项目名称:ferenda,代码行数:51,代码来源:keyword.py

示例14: test_fuseki_clear

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def test_fuseki_clear(self, mock_post, mock_delete):
        store = TripleStore.connect("FUSEKI", "", "")
        store.clear()
        self.assertEqual(mock_delete.call_count, 0)
        self.assertEqual(mock_post.call_count, 1)
        with self.assertRaises(errors.TriplestoreError):
            mock_post.side_effect = requests.exceptions.ConnectionError("Server error")
            got = store.clear()

        with self.assertRaises(errors.TriplestoreError):
            mock_post.side_effect = requests.exceptions.HTTPError("Server error")
            got = store.clear()

        mock_post.side_effect = requests.exceptions.HTTPError("No such graph")
        got = store.clear("namedgraph")
开发者ID:staffanm,项目名称:ferenda,代码行数:17,代码来源:testTripleStore.py

示例15: select

# 需要导入模块: from ferenda import TripleStore [as 别名]
# 或者: from ferenda.TripleStore import connect [as 别名]
    def select(self, template, uri, format="json"):
        sq = util.readfile(template) % {'uri': uri}
        ts = TripleStore.connect(self.config.storetype,
                                 self.config.storelocation,
                                 self.config.storerepository)

        print("# Constructing the following from %s, repository %s, type %s" %
              (self.config.storelocation,
               self.config.storerepository,
               self.config.storetype))
        print("".join(["# %s\n" % x for x in sq.split("\n")]))
        p = {}
        with util.logtime(print,
                          "# Selected in %(elapsed).3fs",
                          p):
            res = ts.select(sq, format=format)
            print(res.decode('utf-8'))
开发者ID:h4ck3rm1k3,项目名称:ferenda,代码行数:19,代码来源:devel.py


注:本文中的ferenda.TripleStore.connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。