]> piware.de Git - bin.git/blob - vcard2gnokii
initial checkin
[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     for k, v in info.iteritems():
18         kf = k.split(';')
19         type = 0
20         if len(kf) > 1:
21             if kf[1] == 'TYPE=HOME':
22                 type = 2
23             elif kf[1] == 'TYPE=CELL':
24                 type = 3
25             elif kf[1] == 'TYPE=WORK':
26                 type = 6
27
28         if kf[0] == 'ADR':
29             result += ';9;%i;9;%s' % (type, '\\n'.join([f for f in v.split(';') if f]))
30         if kf[0] == 'EMAIL':
31             result += ';8;%i;8;%s' % (type, v)
32         if kf[0] == 'TEL':
33             result += ';11;%i;11;%s' % (type, v)
34         if kf[0] == 'BDAY':
35             result += ';10;0;10;BDAY: ' + v
36
37     print result
38
39 info = {}
40 for line in open(sys.argv[1]):
41     if not line or line[0] == ' ':
42         continue
43     line = line.strip()
44     if not line:
45         continue
46     try:
47         (key, value) = line.split(':', 1)
48     except ValueError:
49         print >> sys.stderr, 'ignoring invalid line:', line
50         continue
51     value = value.strip()
52     if value == 'VCARD':
53         if key == 'BEGIN':
54             info = {}
55         elif key == 'END':
56             output_info(info)
57     if value:
58         info[key] = value.strip()