1 """ calitems.py -- convert RDF calendar to danger JSON
5 $ export PYTHONPATH=~/src/swap
6 $ python calitems.py mycal.rdf > mycal.json
7 $ python calitems.py --test
9 See also doctests for `eachEvent`.
15 from swap import uripath # http://www.w3.org/2000/10/swap/
16 from swap.myStore import Namespace, load
18 import hipsrv # hmm... move asHiptopDate here?
20 def eachEvent(kb, float_tz='America/Chicago'):
21 """iterate over events in RDF kb, returning danger-style JSON
23 @param float_tz: local timezone of the device at the
24 time of insert/post... at least... I think... it
27 supports timed events only
28 summary, dtstart required
29 loc, description optional
31 >>> kb = _s2f(TestLocal, "http://example/cal1")
32 >>> e = eachEvent(kb).next()
34 ['end_date', 'event_type', 'location', 'notes', 'repeat_type', 'start_date', 'timezone', 'title']
36 u'Aug. 25 (Saturday) 318 08:00 1215 Yellow Dragons v 1216 BVSC Dragons Community Park West'
40 >>> kb = _s2f(TestUTC, "http://example/cal2")
41 >>> e = eachEvent(kb).next()
43 ['end_date', 'event_type', 'notes', 'repeat_type', 'start_date', 'timezone', 'title']
45 u'2007-09-15 lv PHL 20:40 ar GLA 08:35 SUNDAY US AIRWAYS #768'
53 # @# these should be module constants, but myStore global
54 # wierdness seems to interfere.
55 RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
56 ICAL = Namespace('http://www.w3.org/2002/12/cal/icaltzd#')
58 for ev in kb.each(pred = RDF.type, obj = ICAL.Vevent):
59 titl = unicode(kb.the(subj = ev, pred = ICAL.summary) or '').strip()
62 dtstart = unicode(kb.the(subj = ev, pred = ICAL.dtstart))
63 dtend = unicode(kb.the(subj = ev, pred = ICAL.dtend) or '')
65 loc = unicode(kb.any(subj = ev, pred = ICAL.location) or '')
67 desc = unicode(kb.any(subj = ev, pred = ICAL.description) or '')
68 desc = ' '.join(desc.split()) # a la xslt normalize-space
70 ety = 1 # timed (not all day), end time (not dur)
72 if not "T" in dtstart: # all-day event
75 dtstart = "%sT000000Z" % (dtstart.replace("-", ""))
77 dtend = "%sT000000Z" % (dtend.replace("-", ""))
78 elif dtstart.endswith("Z"):
79 dtstart=dtstart.replace("-", "").replace(":", "")
81 dtend=dtend.replace("-", "").replace(":", "")
83 dtend=dtstart # hmm... plus 1 hour?
85 dtstart = hipsrv.asHiptopDate(dtstart, tz)
87 dtend = hipsrv.asHiptopDate(dtend, tz)
89 dtend=dtstart # hmm... plus 1 hour?
93 'start_date': dtstart,
99 if tz: rec['timezone'] = tz
100 if loc: rec['location'] = loc
106 addr = uripath.join("file:" + os.getcwd() + "/", fn)
111 print "@@", kb.size()
114 for e in eachEvent(kb):
119 print simplejson.dumps(e)
125 """make a formula from a string.
127 Cribbed from llyn.BI_parsedAsN3
128 should be part of the myStore API, no? yes. TODO
130 >>> _s2f("<#sky> <#color> <#blue>.", "http://example/socrates")
133 ^ that test output depends on the way formulas print themselves.
135 # """ emacs python mode needs help
137 from swap.notation3 import SinkParser
138 from swap.myStore import formula
141 p = SinkParser(graph.store, openFormula=graph, baseURI=base,
142 thisDoc="data:@@some-formula-string")
151 @prefix : <http://www.w3.org/2002/12/cal/icaltzd#> .
156 :description "Aug. 25 (Saturday) 318 08:00 1215 Yellow Dragons v 1216 BVSC Dragons Community Park West";
157 :dtstart "2007-08-25T08:00:00"^^:dateTime;
158 :location "Community Park West";
159 :summary "Soccer Game 1215 Yellow Dragons v 1216 BVSC Dragons";
166 @prefix : <http://www.w3.org/2006/vcard/ns#> .
167 @prefix SUMO: <http://www.ontologyportal.org/translations/SUMO.owl.txt#> .
168 @prefix XML: <http://www.w3.org/2001/XMLSchema#> .
169 @prefix c: <http://www.w3.org/2002/12/cal/icaltzd#> .
170 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
171 @prefix sou: <http://www.w3.org//2007/07dc-lhr/sou19#> .
173 sou:ee139866156 a c:Vevent;
174 SUMO:destination <,2sou19.rdf#GLA>;
175 SUMO:origin <,2sou19.rdf#PHL>;
176 c:description "2007-09-15 lv PHL 20:40 ar GLA 08:35 SUNDAY US AIRWAYS #768";
177 c:dtend "2007-09-16T07:35:00Z"^^XML:dateTime;
178 c:dtstart "2007-09-16T00:40:00Z"^^XML:dateTime;
179 c:summary "US AIRWAYS #768";
180 c:url <https://bwi.flightview.com/fvSabreVT/fvCPL.exe?vthost=1W&acid=76
181 8&qtype=htm&AL=US&Find1=Track+Flight> .
189 if __name__ == "__main__":
192 if '--test' in sys.argv: