From 7ab405c8cf32a49ead868b9d5aa8b05b8c5942e5 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 20 Apr 2013 12:37:49 +0100 Subject: Add script to convert abook databases to clab --- abook-to-clab | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 abook-to-clab diff --git a/abook-to-clab b/abook-to-clab new file mode 100755 index 0000000..f182972 --- /dev/null +++ b/abook-to-clab @@ -0,0 +1,47 @@ +#!/usr/bin/python + + +import ConfigParser +import os +import yaml +import sys + + +records = {} + +for filename in sys.argv[1:]: + cp = ConfigParser.ConfigParser() + cp.read([filename]) + for section in cp.sections(): + if section == 'format': + continue + obj = {} + for option in cp.options(section): + assert option not in obj + v = cp.get(section, option, raw=True) + if ',' in v: + vs = v.split(',') + obj[option] = dict((str(i), x) for i, x in enumerate(vs)) + else: + obj[option] = v + + assert type(obj['name']) is str + uid = ''.join(obj['name'].split()).lower() + if uid in records: + r = records[uid] + for key in obj: + if key in r: + v = r[key] + if type(v) is dict: + v[str(len(v) + 1)] = obj[key] + else: + r[key] = { '0': v, '1': obj[key] } + else: + r[key] = obj[key] + else: + records[uid] = obj + +for uid, r in records.iteritems(): + with open(os.path.join('db', uid + '.yaml'), 'w') as f: + yaml.safe_dump(r, stream=f, default_flow_style=False) + -- cgit v1.2.1