2 # -*- coding: utf-8 -*-
4 from getpass import getpass
5 from dateutil.tz import *
6 from datetime import datetime
7 from dateutil.parser import parse
10 from urlparse import urlparse
11 from lxml import etree
12 from lxml.builder import ElementMaker
16 from address import Address
17 from contact import Organization
18 from treecutter import constants as const
22 def __init__(self, uri):
26 if uri.scheme == 'file':
27 with open(self.uri.path, 'r') as f:
30 if uri.scheme == 'http':
32 print "http not yet implemented"
33 if uri.scheme == 'https':
35 headers = {"User-Agent": "Mozilla/5.0",
36 "Content-Type": "text/xml",
39 headers['authorization'] = "Basic %s" % (("%s:%s" % (self.uri.username, pw)).encode('base64')[:-1])
40 handle = httplib.HTTPSConnection(self.uri.hostname,self.uri.port)
41 res = handle.request('GET', self.uri.path, "", headers)
42 r = handle.getresponse()
44 print "Failed to connect! Wrong Password ?"
48 headers = ['dtstart','summary','location','description']
49 for cal in vobject.readComponents(self.data):
50 for ev in cal.vevent_list:
54 for p in ev.getChildren():
55 details[p.name.lower()] = p.value
56 self.events.append(Event(details))
58 def filter(self,query):
59 (key, name) = query.split(':')
62 for ev in self.events:
63 if ev.start.year == int(name):
68 for ev in self.events:
71 def sorted(self,order):
73 self.events.sort(key=lambda x: x.start, reverse=True)
77 db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
78 evlist = db.variablelist(db.title(u'Stammtisch träffar'),
80 for ev in self.events:
81 evlist.append(ev.db_xml())
86 def __init__(self,ev):
87 self.start = ev['dtstart']
88 self.end = ev['dtend']
89 self.summary = ev['summary']
90 self.location = ev['location']
92 self.description = ev['description']
95 addrlist = self.location.split(',')
96 addrfmt = "org,street,postcode,city"
97 adict = addrfmt.split(',')
98 argdict = dict(zip(adict,addrlist))
99 self.org = Organization(argdict['org'])
101 addr = Address(**argdict)
102 self.org.add_address(addr)
107 db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
108 # Build paragraphs from the description
109 paras = db.listitem(role="description")
110 for p in re.split('\n\n',unicode(self.description)):
111 paras.append(db.para(p,role="desc"))
113 lst = db.varlistentry(
114 db.term(db.date(self.start.strftime('%Y %b %d'),role='calendar')),
115 db.listitem(db.para(self.summary),db.variablelist(
118 db.listitem(db.para(self.start.strftime('%H:%M')))
122 db.listitem(db.para(self.org.db_xml()))
125 db.term("Beskrivning"),
133 # ln = db.link("Text",**{XLINK+"href": "https://"})
135 if __name__ == "__main__":
136 for arg in sys.argv[1:]:
143 (uristr,query) = argument.split('|')
144 uri = urlparse(uristr)
148 events.sorted('start')
149 exml = events.db_xml()
152 #print(etree.tostring(cxml, pretty_print=True))
153 #sys.stdout.write(out.encode('utf-8'))
154 sys.stdout.write(etree.tostring(exml,encoding='UTF-8',pretty_print=False))