879fd66fc8ef05a903dfdcead8405501f8348d0a
[treecutter.git] / xinclude / contact.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 from vobject import readComponents
5 import sys
6
7 for arg in sys.argv[1:]:
8   al = arg.split("=")
9   if al[0] == "lang":
10     lang = al[1]
11   if al[0] == "xptr":
12     argument = al[1]
13
14 (cards,query) = argument.split('?')
15 (key, name) = query.split(':')
16 with open(cards, 'r') as f:
17   card_data = f.read()
18 f.closed
19
20 # Scan for the correct card
21 found = None
22 out = u''
23 for card in readComponents(card_data):
24 # card.prettyPrint()
25   if key in card.contents.keys():
26     if name.decode('utf-8') == card.contents[key][0].value[0]:
27       found = card
28   if key == 'firstname':
29     if name.decode('utf-8') == card.n.value.given:
30       found = card
31   if key == 'surname':
32     if name.decode('utf-8') == card.n.value.family:
33       found = card
34
35 if not found:
36   print query+' failed in '+cards
37   exit(2)
38 # when card is found parse it to docbook.
39 pn = ''
40
41 if 'n' in found.contents.keys():
42   n = found.n.value
43   empty = n.prefix == '' and n.given == '' and \
44       n.additional =='' and n.family ==''  and n.suffix == ''
45   if not empty:
46     pn += '<personname>'
47     if n.prefix != '':
48       pn += '<honorific>'+n.prefix+'</honorific>'
49     if n.given != '':
50       pn += '<firstname>'+n.given+'</firstname>'
51     if n.additional != '':
52       pn += '<othername>'+n.additional+'</othername>'
53     if n.family != '':
54       pn += '<surname>'+n.family+'</surname>'
55     if n.suffix != '':
56       pn += '<lineage>'+n.suffix+'</lineage>'
57     pn += '</personname>'
58
59 ad = ''
60 if 'adr' in found.contents.keys():
61   for a, t in zip(found.contents['adr'],found.contents['tel']):
62     ad += '\n<address type="'+a.type_param+'">'
63     if a.value.street != '':
64       ad += '<street>'+a.value.street+'</street>'
65     if a.value.code != '':
66       ad += '<postcode>'+a.value.code+'</postcode>'
67     if a.value.city != '':
68       ad += '<city>'+a.value.city+'</city>'
69     if a.value.country != '':
70       ad += '<country>'+a.value.country+'</country>'
71     if t.value != '':
72       ad += '<phone>'+t.value+'</phone>'
73     ad += '</address>'
74
75 o = ''
76 if 'org' in found.contents.keys():
77   o = '''
78       <org>
79         <orgname>'''+found.org.value[0]+'''</orgname>
80         '''+ad+'''
81         </org>'''
82
83
84 url = ''
85 if 'url' in found.contents.keys():
86   url += '<uri type="website"><link xlink:href="'+found.url.value+'"/></uri>'
87 geo = ''
88 if 'geo' in found.contents.keys():
89   (lat,lon) = found.geo.value.split(';')
90   geo += '<uri type="location"><link xlink:href="http://www.openstreetmap.org/'+ \
91       '?mlat='+lat+'&amp;mlon='+lon+'&amp;zoom=18&amp;layers=M">geo:'+lat+','+lon+'</link></uri>'
92
93 # Turn off email for now
94 email = ''
95 #if 'email' in found.contents.keys():
96 #  email += '<email>'+found.email.value+'</email>'
97
98 if empty:
99   content = pn+o+url+geo+email
100 else:
101   if o == '':
102     content = '<person>'+pn+ad+url+geo+email+'</person>'
103   else:
104     content = '<person>'+pn+'<affiliation>'+o+'</affiliation>'+url+geo+email+ \
105         '</person>'
106 out = '''
107 <para xmlns="http://docbook.org/ns/docbook"
108       xmlns:xlink="http://www.w3.org/1999/xlink">
109   '''+content+'''
110 </para>
111 '''
112
113 ex ='''
114     <address>
115       <street>Street</street>
116       <postcode>Postcode</postcode> <city>City</city>
117       <country>Country</country>
118       <phone>+1 123 456 789</phone>
119     </address>
120     <affiliation>
121       <jobtitle>Occupation</jobtitle>
122       <org>
123         <orgname>Organization</orgname>
124         <address>
125           <street>Street</street>
126           <postcode>Postcode</postcode> <city>City</city>
127           <country>Country</country>
128           <phone>+1 123 456 789</phone>
129         </address>
130       </org>
131     </affiliation>
132   </person>
133 </para>
134 '''
135
136 sys.stdout.write(out.encode('utf-8'))