diff options
author | Mark Watts <watts.mark2015@gmail.com> | 2020-05-31 17:57:05 -0500 |
---|---|---|
committer | Mark Watts <watts.mark2015@gmail.com> | 2020-05-31 17:58:29 -0500 |
commit | 04d65c94eb1d0fd17a4aa981a9ac297b97608aab (patch) | |
tree | 7affa258820eae38cc7371100d0aa9030116546b | |
parent | 2c7cca3cc9721e7bff2be6256e8b061993a06ae5 (diff) | |
download | rdflib-04d65c94eb1d0fd17a4aa981a9ac297b97608aab.tar.gz |
Supporting bnode_context for each call to parse (#980)
- Needed so you can access through Graph.parse which does not support
passing args to Parser __init__
-rw-r--r-- | rdflib/plugins/parsers/ntriples.py | 16 | ||||
-rw-r--r-- | test/test_nt_misc.py | 38 |
2 files changed, 43 insertions, 11 deletions
diff --git a/rdflib/plugins/parsers/ntriples.py b/rdflib/plugins/parsers/ntriples.py index e2ee5ff3..f817cede 100644 --- a/rdflib/plugins/parsers/ntriples.py +++ b/rdflib/plugins/parsers/ntriples.py @@ -128,21 +128,23 @@ class NTriplesParser(object): else: self._bnode_ids = {} + self._parse_bnode_ids = None + if sink is not None: self.sink = sink else: self.sink = Sink() - def parse(self, f): + def parse(self, f, bnode_context=None): """Parse f as an N-Triples file.""" if not hasattr(f, "read"): raise ParseError("Item to parse must be a file-like object.") - # since N-Triples 1.1 files can and should be utf-8 encoded f = codecs.getreader("utf-8")(f) self.file = f self.buffer = "" + self._parse_bnode_ids = bnode_context while True: self.line = self.readline() if self.line is None: @@ -153,14 +155,14 @@ class NTriplesParser(object): raise ParseError("Invalid line: %r" % self.line) return self.sink - def parsestring(self, s): + def parsestring(self, s, **kwargs): """Parse s as an N-Triples string.""" if not isinstance(s, str): raise ParseError("Item to parse must be a string instance.") f = BytesIO() f.write(cast_bytes(s)) f.seek(0) - self.parse(f) + self.parse(f, **kwargs) def readline(self): """Read an N-Triples line from buffered input.""" @@ -246,8 +248,12 @@ class NTriplesParser(object): def nodeid(self): if self.peek("_"): # Fix for https://github.com/RDFLib/rdflib/issues/204 + if self._parse_bnode_ids is not None: + bnode_ids = self._parse_bnode_ids + else: + bnode_ids = self._bnode_ids bnode_id = self.eat(r_nodeid).group(1) - new_id = self._bnode_ids.get(bnode_id, None) + new_id = bnode_ids.get(bnode_id, None) if new_id is not None: # Re-map to id specfic to this doc return bNode(new_id) diff --git a/test/test_nt_misc.py b/test/test_nt_misc.py index 0c738c35..4a21fed9 100644 --- a/test/test_nt_misc.py +++ b/test/test_nt_misc.py @@ -131,21 +131,34 @@ class NTTestCase(unittest.TestCase): class BNodeContextTestCase(unittest.TestCase): - def test_bnode_shared_across_parse(self): + def test_bnode_shared_across_instances(self): my_sink = FakeSink() bnode_context = dict() p = ntriples.NTriplesParser(my_sink, bnode_context=bnode_context) - p.parsestring(''' _:0 <http://purl.obolibrary.org/obo/RO_0002350> <http://www.gbif.org/species/0000001> . ''') - p = ntriples.NTriplesParser(my_sink, bnode_context=bnode_context) + q = ntriples.NTriplesParser(my_sink, bnode_context=bnode_context) + q.parsestring(''' + _:0 <http://purl.obolibrary.org/obo/RO_0002350> <http://www.gbif.org/species/0000002> . + ''') + + self.assertEqual(len(my_sink.subs), 1) + + def test_bnode_distinct_across_instances(self): + my_sink = FakeSink() + p = ntriples.NTriplesParser(my_sink) p.parsestring(''' + _:0 <http://purl.obolibrary.org/obo/RO_0002350> <http://www.gbif.org/species/0000001> . + ''') + + q = ntriples.NTriplesParser(my_sink) + q.parsestring(''' _:0 <http://purl.obolibrary.org/obo/RO_0002350> <http://www.gbif.org/species/0000002> . ''') - assert len(my_sink.subs) == 1 + self.assertEqual(len(my_sink.subs), 2) def test_bnode_distinct_across_parse(self): my_sink = FakeSink() @@ -153,14 +166,27 @@ class BNodeContextTestCase(unittest.TestCase): p.parsestring(''' _:0 <http://purl.obolibrary.org/obo/RO_0002350> <http://www.gbif.org/species/0000001> . - ''') + ''', bnode_context=dict()) + + p.parsestring(''' + _:0 <http://purl.obolibrary.org/obo/RO_0002350> <http://www.gbif.org/species/0000002> . + ''', bnode_context=dict()) + + self.assertEqual(len(my_sink.subs), 2) + def test_bnode_shared_across_parse(self): + my_sink = FakeSink() p = ntriples.NTriplesParser(my_sink) + + p.parsestring(''' + _:0 <http://purl.obolibrary.org/obo/RO_0002350> <http://www.gbif.org/species/0000001> . + ''') + p.parsestring(''' _:0 <http://purl.obolibrary.org/obo/RO_0002350> <http://www.gbif.org/species/0000002> . ''') - assert len(my_sink.subs) == 2 + self.assertEqual(len(my_sink.subs), 1) class FakeSink(object): |