]> piware.de Git - bin.git/blob - vcard2gnokii
vcard2gnokii: field type postal does not currently work, use note
[bin.git] / vcard2gnokii
1 #!/usr/bin/python
2
3 import sys
4
5 def output_info(info):
6     if info.has_key('X-EVOLUTION-FILE-AS'):
7         result = info['X-EVOLUTION-FILE-AS']
8     elif info.has_key('N'):
9         name_comp = info['N'].split(';')
10         result = '%s\, %' % (name_comp[0], ' '.join([c for c in name_comp[1:] if c]))
11     else:
12         print >> sys.stderr, 'No suitable name for', info
13         return
14
15     result += ';;ME;;5'
16
17     id = 0
18
19     for k, v in info.iteritems():
20         kf = k.split(';')
21         type = 0
22         if len(kf) > 1:
23             if kf[1].startswith('TYPE=HOME'):
24                 type = 2
25             elif kf[1].startswith('TYPE=CELL'):
26                 type = 3
27             elif kf[1].startswith('TYPE=WORK'):
28                 type = 6
29
30         if kf[0] == 'ADR':
31             # current gnokii does not swallow type "9" (postal address)
32             result += ';10;0;%i;%s' % (id, '\\n'.join([f for f in v.split(';') if f]))
33         elif kf[0] == 'EMAIL':
34             result += ';8;%i;%i;%s' % (type, id, v)
35         elif kf[0] == 'TEL':
36             result += ';11;%i;%i;%s' % (type, id, v)
37         elif kf[0] == 'BDAY':
38             result += (';10;0;%i;BDAY: ' %id) + v
39         else:
40             continue
41
42         id += 1
43
44     print result
45
46 info = {}
47 for line in open(sys.argv[1]):
48     if not line or line[0] == ' ':
49         continue
50     line = line.strip()
51     if not line:
52         continue
53     try:
54         (key, value) = line.split(':', 1)
55     except ValueError:
56         print >> sys.stderr, 'ignoring invalid line:', line
57         continue
58     value = value.strip()
59     if value == 'VCARD':
60         if key == 'BEGIN':
61             info = {}
62         elif key == 'END':
63             output_info(info)
64     if value:
65         info[key] = value.strip()