-
Notifications
You must be signed in to change notification settings - Fork 0
/
localrdf.py
94 lines (79 loc) · 3.08 KB
/
localrdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os, md5
from rdflib.Graph import ConjunctiveGraph, Graph
from rdflib import BNode, URIRef, Literal
from rdflib import RDF, RDFS
from namespaces import TT, TRAIN, DC
def get_context(self, identifier, quoted=False):
assert isinstance(identifier, URIRef) or \
isinstance(identifier, BNode), type(identifier)
return Graph(store=self.store, identifier=identifier,
namespace_manager=self)
ConjunctiveGraph.get_context = get_context
def getGraph():
"""
get the main graph, including data from trains.n3 on disk
"""
graph = ConjunctiveGraph()
graph.parse("trains.n3", format="n3", publicID=TT['disk#context'])
return graph
def addStop(readGraph, writeGraph, train, date, station):
"""URI for a train stop. date is xs:date"""
stop = URIRef("%s/%s/%s" % (train, date,
readGraph.value(station, TT['stationCode'])))
writeGraph.add((stop, RDF.type, TT['Stop']))
writeGraph.add((train, TT['stop'], stop))
return stop
def addTrain(graph, trainNum):
train = TRAIN["n" + trainNum]
graph.add((train, RDF.type, TT['Train']))
graph.add((train, RDFS.label, Literal(str(trainNum))))
return train
SUBGRAPH_FORMAT = "xml"
def filenameFromURI(uri):
return md5.md5(uri).hexdigest()
def writeSubgraph(subgraph):
created = subgraph.value(subgraph.identifier, TT['contextDate'])
assert created
filename = "graph/%s/%s" % (created, filenameFromURI(subgraph.identifier))
try:
os.makedirs(os.path.dirname(filename))
except OSError:
pass
subgraph.serialize(open(filename, "w"), format=SUBGRAPH_FORMAT)
open(filename + ".uri", "w").write(str(subgraph.identifier) + "\0" +
str(subgraph.label(subgraph.identifier)))
def subgraphs(date):
"""generator of (uri, ntfilename)
pass a date literal like '2008-02-03' to get all the data for that date
"""
dateDir = "graph/%s" % date
if not os.path.isdir(dateDir):
return
for name in os.listdir(dateDir):
filename = "graph/%s/%s" % (date, name)
if filename.endswith('.uri'):
continue
uri, label = open(filename + ".uri").read().split("\0")
yield uri, label, filename
def readSubgraphXML(filename):
if SUBGRAPH_FORMAT == 'xml':
return open(filename).read()
g = ConjunctiveGraph()
g.parse(filename, format=SUBGRAPH_FORMAT)
return g
def allGraphs(date):
"""a graph with all the data we have for a given date, like '2008-02-03'"""
g = getGraph()
for uri, label, filename in subgraphs(date):
if not label:
label = "(no label provided)"
g.parse(filename, format=SUBGRAPH_FORMAT)
return g
def readSubgraph(graph, ctxURI):
"""read stmts from ctxURI into graph, if we have any on disk"""
filename = filenameFromURI(ctxURI)
for dateDir in os.listdir("graph"):
f = "graph/%s/%s" % (dateDir, filename)
if os.path.exists(f):
print "readSubgraph adds", f
graph.parse(f, format=SUBGRAPH_FORMAT, publicID=ctxURI)