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