]> piware.de Git - bin.git/blob - vcard2gnokii
workitems: add tracking of assignee (DB schema change)
[bin.git] / vcard2gnokii
1 #!/usr/bin/python
2
3 import sys
4
5 def output_info(info, pos):
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;%i;5' % pos
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 'TYPE=HOME' in k:
24                 type = 2
25             elif 'TYPE=CELL' in k:
26                 type = 3
27             elif 'TYPE=WORK' in k:
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 pos = 1
47 info = {}
48 for line in open(sys.argv[1]):
49     if not line or line[0] == ' ':
50         continue
51     line = line.strip()
52     if not line:
53         continue
54     try:
55         (key, value) = line.split(':', 1)
56     except ValueError:
57         print >> sys.stderr, 'ignoring invalid line:', line
58         continue
59     value = value.strip()
60     if value == 'VCARD':
61         if key == 'BEGIN':
62             info = {}
63         elif key == 'END':
64             output_info(info, pos)
65             pos += 1
66     if value:
67         info[key] = value.strip()