Skip to content
Newer
Older
100755 31 lines (24 sloc) 854 Bytes
291faf2 @rubys Import ConfigParser and cosmetic fixes
authored
1 from ConfigParser import ConfigParser
468bbcc @rubys CSV reading lists
authored
2 import csv
3
4 # input = csv, output = ConfigParser
5 def csv2config(input, config=None):
6
7 if not hasattr(input, 'read'):
8 input = csv.StringIO(input)
9
10 if not config:
11 config = ConfigParser()
12
13 reader = csv.DictReader(input)
291faf2 @rubys Import ConfigParser and cosmetic fixes
authored
14 for row in reader:
15 section = row[reader.fieldnames[0]]
9de2109 @amit Use config.has_section() instead of a separate dict
amit authored
16 if not config.has_section(section):
d7c771b @amit Fix for duplicate rows in csv subscription list
amit authored
17 config.add_section(section)
291faf2 @rubys Import ConfigParser and cosmetic fixes
authored
18 for name, value in row.items():
468bbcc @rubys CSV reading lists
authored
19 if value and name != reader.fieldnames[0]:
20 config.set(section, name, value)
21
22 return config
23
24 if __name__ == "__main__":
291faf2 @rubys Import ConfigParser and cosmetic fixes
authored
25 # small main program which converts CSV into config.ini format
468bbcc @rubys CSV reading lists
authored
26 import sys, urllib
27 config = ConfigParser()
28 for input in sys.argv[1:]:
29 csv2config(urllib.urlopen(input), config)
30 config.write(sys.stdout)
Something went wrong with that request. Please try again.