#!/usr/bin/python import sys def output_info(info, pos): if info.has_key('X-EVOLUTION-FILE-AS'): result = info['X-EVOLUTION-FILE-AS'] elif info.has_key('N'): name_comp = info['N'].split(';') result = '%s\, %' % (name_comp[0], ' '.join([c for c in name_comp[1:] if c])) else: print >> sys.stderr, 'No suitable name for', info return result += ';;ME;%i;5' % pos id = 0 for k, v in info.iteritems(): kf = k.split(';') type = 0 if len(kf) > 1: if 'TYPE=HOME' in k: type = 2 elif 'TYPE=CELL' in k: type = 3 elif 'TYPE=WORK' in k: type = 6 if kf[0] == 'ADR': # current gnokii does not swallow type "9" (postal address) result += ';10;0;%i;%s' % (id, '\\n'.join([f for f in v.split(';') if f])) elif kf[0] == 'EMAIL': result += ';8;%i;%i;%s' % (type, id, v) elif kf[0] == 'TEL': result += ';11;%i;%i;%s' % (type, id, v) elif kf[0] == 'BDAY': result += (';10;0;%i;BDAY: ' %id) + v else: continue id += 1 print result pos = 1 info = {} for line in open(sys.argv[1]): if not line or line[0] == ' ': continue line = line.strip() if not line: continue try: (key, value) = line.split(':', 1) except ValueError: print >> sys.stderr, 'ignoring invalid line:', line continue value = value.strip() if value == 'VCARD': if key == 'BEGIN': info = {} elif key == 'END': output_info(info, pos) pos += 1 if value: info[key] = value.strip()