Mercurial > code > home > repos > homeauto
diff service/mqtt_to_rdf/rdf_debug.py @ 1650:2061df259224
move graphDump (on its way out, since reprs are getting better)
author | drewp@bigasterisk.com |
---|---|
date | Sat, 18 Sep 2021 23:53:59 -0700 |
parents | |
children | 20474ad4968e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/service/mqtt_to_rdf/rdf_debug.py Sat Sep 18 23:53:59 2021 -0700 @@ -0,0 +1,31 @@ +import logging +from typing import List, Union, cast + +from rdflib.graph import Graph +from rdflib.namespace import Namespace + +from inference_types import Triple + +log = logging.getLogger('infer') + +ROOM = Namespace("http://projects.bigasterisk.com/room/") + + +def graphDump(g: Union[Graph, List[Triple]], oneLine=True): + # this is very slow- debug only! + if not log.isEnabledFor(logging.DEBUG): + return "(skipped dump)" + try: + if not isinstance(g, Graph): + g2 = Graph() + g2 += g + g = g2 + g.bind('', ROOM) + g.bind('ex', Namespace('http://example.com/')) + lines = cast(bytes, g.serialize(format='n3')).decode('utf8').splitlines() + lines = [line for line in lines if not line.startswith('@prefix')] + if oneLine: + lines = [line.strip() for line in lines] + return ' '.join(lines) + except TypeError: + return repr(g) \ No newline at end of file