diff options
author | Ashley Sommer <ashleysommer@gmail.com> | 2020-09-18 00:28:29 +1000 |
---|---|---|
committer | Ashley Sommer <ashleysommer@gmail.com> | 2020-09-18 00:28:29 +1000 |
commit | 48e9137a848e95b00f247b11c83961ea500a1042 (patch) | |
tree | aa2e1b3c5f26f099c5495afe0a89a7630721607e | |
parent | c50ee269c3a518e72931e476dd592ec5336b679a (diff) | |
download | rdflib-48e9137a848e95b00f247b11c83961ea500a1042.tar.gz |
Change assert tests in the N3 parser constructor, to instead be if-raise statements with meaningful error messages
Re-blacked notation3.py
-rwxr-xr-x | rdflib/plugins/parsers/notation3.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/rdflib/plugins/parsers/notation3.py b/rdflib/plugins/parsers/notation3.py index 9915b1aa..089efff8 100755 --- a/rdflib/plugins/parsers/notation3.py +++ b/rdflib/plugins/parsers/notation3.py @@ -39,6 +39,7 @@ from decimal import Decimal from uuid import uuid4 +from exceptions import ParserError from rdflib.term import URIRef, BNode, Literal, Variable, _XSD_PFX, _unique_id from rdflib.graph import QuotedGraph, ConjunctiveGraph, Graph from rdflib.compat import long_type @@ -1731,7 +1732,11 @@ class RDFSink(object): self.graph = graph def newFormula(self): - assert self.graph.store.formula_aware + fa = getattr(self.graph.store, "formula_aware", False) + if not fa: + raise ParserError( + "Cannot create formula parser with non-formula-aware store." + ) f = Formula(self.graph) return f @@ -1785,7 +1790,7 @@ class RDFSink(object): f, p, s, o = quadruple if hasattr(p, "formula"): - raise Exception("Formula used as predicate") + raise ParserError("Formula used as predicate") s = self.normalise(f, s) p = self.normalise(f, p) @@ -1888,8 +1893,8 @@ class TurtleParser(Parser): def parse(self, source, graph, encoding="utf-8", turtle=True): if encoding not in [None, "utf-8"]: - raise Exception( - ("N3/Turtle files are always utf-8 encoded, ", "I was passed: %s") + raise ParserError( + ("N3/Turtle files are always utf-8 encoded, I was passed: %s") % encoding ) @@ -1921,8 +1926,13 @@ class N3Parser(TurtleParser): def parse(self, source, graph, encoding="utf-8"): # we're currently being handed a Graph, not a ConjunctiveGraph - assert graph.store.context_aware # is this implied by formula_aware - assert graph.store.formula_aware + # context-aware is this implied by formula_aware + ca = getattr(graph.store, "context_aware", False) + fa = getattr(graph.store, "formula_aware", False) + if not ca: + raise ParserError("Cannot parse N3 into non-context-aware store.") + elif not fa: + raise ParserError("Cannot parse N3 into non-formula-aware store.") conj_graph = ConjunctiveGraph(store=graph.store) conj_graph.default_context = graph # TODO: CG __init__ should have a |