annotate graphconvert.py @ 83:7f50e5bb30f5

ide
author drewp@bigasterisk.com
date Sat, 07 Sep 2024 16:12:15 -0700
parents f2dd88b9964c
children f75b3a109b66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45
e53a1bc87f99 cleanup and some fixes to starred event graph
drewp@bigasterisk.com
parents: 43
diff changeset
1 from dateutil.tz import tzlocal
e53a1bc87f99 cleanup and some fixes to starred event graph
drewp@bigasterisk.com
parents: 43
diff changeset
2 from rdflib import RDF, RDFS, ConjunctiveGraph, Literal, Namespace, URIRef
e53a1bc87f99 cleanup and some fixes to starred event graph
drewp@bigasterisk.com
parents: 43
diff changeset
3
42
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
4 from localtypes import Conf, feedFromCalId
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
5
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
6 EV = Namespace("http://bigasterisk.com/event#")
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
7
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
8
45
e53a1bc87f99 cleanup and some fixes to starred event graph
drewp@bigasterisk.com
parents: 43
diff changeset
9 def asGraph(conf: Conf, cals, events, extraClasses=[], ctxUri: URIRef = EV['gcalendar']):
42
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
10 ctx = ConjunctiveGraph(identifier=ctxUri)
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
11 graph = ConjunctiveGraph()
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
12 graph.namespace_manager.bind('ev', EV)
42
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
13
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
14 for doc in cals:
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
15 feed = URIRef(feedFromCalId(conf, doc['_id']))
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
16 graph.add((feed, RDF.type, EV['CalendarFeed'], ctx))
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
17 if doc['summary']:
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
18 graph.add((feed, RDFS.label, Literal(doc['summary'].strip()), ctx))
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
19 if doc['description']:
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
20 graph.add((feed, EV['description'], Literal(doc['description'].strip()), ctx))
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
21
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
22 for ev in events:
43
b5d3d9a8c83d new graph of just the events happening now
drewp@bigasterisk.com
parents: 42
diff changeset
23 uri = URIRef(ev.get('uri', ev.get('_id')))
b5d3d9a8c83d new graph of just the events happening now
drewp@bigasterisk.com
parents: 42
diff changeset
24 if uri is None:
b5d3d9a8c83d new graph of just the events happening now
drewp@bigasterisk.com
parents: 42
diff changeset
25 raise ValueError(f"{ev=} had no event uri")
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
26
45
e53a1bc87f99 cleanup and some fixes to starred event graph
drewp@bigasterisk.com
parents: 43
diff changeset
27 def add(p: URIRef, o: URIRef | Literal):
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
28 return graph.add((uri, p, o, ctx))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
29
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
30 add(RDF.type, EV['Event'])
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
31 for cls in extraClasses:
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
32 add(RDF.type, cls)
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
33 add(EV['title'], Literal(ev['title']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
34 add(EV['start'], Literal(ev['start']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
35 add(EV['startDate'], Literal(ev['startDate']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
36 add(EV['end'], Literal(ev['end']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
37 add(EV['feed'], URIRef(ev['feed']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
38 # graph.add((feed, RDFS.label, Literal(ev['feedTitle'])))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
39 if 'htmlLink' in ev:
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
40 add(EV['htmlLink'], URIRef(ev['htmlLink']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
41 return graph
45
e53a1bc87f99 cleanup and some fixes to starred event graph
drewp@bigasterisk.com
parents: 43
diff changeset
42