changeset 330:1ff1fbf6b183

move stategraph from magma Ignore-this: 608e916953280812145c6f9ffc5313db
author drewp@bigasterisk.com
date Sat, 17 Feb 2018 23:44:51 -0800
parents 1d562167868c
children a94f2a522d41
files lib/stategraph.py
diffstat 1 files changed, 49 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/stategraph.py	Sat Feb 17 23:44:51 2018 -0800
@@ -0,0 +1,49 @@
+import datetime, os, inspect
+from dateutil.tz import tzlocal
+from rdflib import Graph, Namespace, Literal
+DCTERMS = Namespace("http://purl.org/dc/terms/")
+
+class StateGraph(object):
+    """
+    helper to create a graph with some of the current state of the world
+    """
+    def __init__(self, ctx):
+        """
+        note that we put the time of the __init__ call into the graph
+        as its dcterms:modified time.
+        """
+        self.g = Graph()
+        self.ctx = ctx
+
+        try:
+            requestingFile = inspect.stack()[1][1]
+            self.g.add((ctx, DCTERMS['creator'], 
+                        Literal(os.path.abspath(requestingFile))))
+        except IndexError:
+            pass
+        self.g.add((ctx, DCTERMS['modified'],
+               Literal(datetime.datetime.now(tzlocal()))))
+
+    def add(self, *args, **kw):
+        self.g.add(*args, **kw)
+
+    def ntLines(self):
+        nt = self.g.serialize(format='nt')
+        # this canonical order is just for debugging, so the lines are
+        # stable when you refresh the file repeatedly
+        return sorted(filter(None, nt.splitlines()))
+        
+    def asTrig(self):
+        return "%s {\n%s\n}\n" % (self.ctx.n3(), '\n'.join(self.ntLines()))
+
+    def asJsonLd(self):
+        return self.g.serialize(format='json-ld')
+
+    def asAccepted(self, acceptHeader):
+        if acceptHeader == 'application/nquads':
+            return 'application/nquads', '\n'.join(
+                line.strip().rstrip('.') + '%s .' % self.ctx.n3()
+                for line in self.ntLines())
+        else:
+            return 'application/x-trig', self.asTrig()
+