comparison martini/main.py @ 0:3c3522ce6e3a

initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
author k0s <k0scist@gmail.com>
date Tue, 08 Dec 2009 15:13:28 -0500
parents
children 5627074cd79b
comparison
equal deleted inserted replaced
-1:000000000000 0:3c3522ce6e3a
1 #!/usr/bin/env python
2
3 usage = "martini file1 [file2] [...] --section1 option1=value1 option2=value2 --section2 option3=value3"
4
5 import config
6 import os
7 import sys
8
9 sep = '--' # section separator
10
11 def parse_args(*args):
12 """
13 convert command line options to files, sections, and options
14 returns a tuple:
15 ( [files], { 'section': { 'option': 'value', 'option2': 'value2' } } )
16 """
17
18 # find the files
19 # XXX note this prohibits files starting with --
20 index = None
21 for index, value in enumerate(args):
22 if value.startswith(sep):
23 break
24 else:
25 return (args, [])
26
27 files = args[:index]
28 args = args[index:]
29
30 # find the sections
31 ini = []
32 for arg in args:
33 if arg.startswith(sep):
34 arg = arg[len(sep):]
35 assert arg
36 section = []
37 ini.append((arg, section))
38 else:
39 section.append(arg)
40
41 return (files, ini)
42
43 def parse_options(*args):
44 files, sections = parse_args(*args)
45 ini = {}
46 for section, options in sections:
47 ini[section] = {}
48 for option in options:
49 key, value = option.split('=', 1)
50 ini[section][key] = value
51
52 return (files, ini)
53
54 def set(args=None):
55
56 # process arguments
57 if args is None:
58 args = sys.argv[1:]
59 files, sections = parse_options(*args)
60
61 # display usage information
62 if not files:
63 print 'Usage:'
64 print usage
65 sys.exit(0)
66
67 # process the files
68 for f in files:
69 if f == '-':
70 fp = sys.stdin
71 else:
72 fp = file(f)
73 munger = config.ConfigMunger(fp, sections)
74
75 if f == '-':
76 fp = sys.stdout
77 else:
78 fp.close()
79 fp = file(f, "w")
80 munger.write(fp=fp)
81
82 def get(args=None):
83
84 # process arguments
85 if args is None:
86 args = sys.argv[1:]
87 files, sections = parse_args(*args)
88
89 # display usage information
90 if not files:
91 print 'Usage:'
92 print usage
93 sys.exit(0)
94
95 # process the files
96 for f in files:
97 if f == '-':
98 fp = sys.stdin
99 else:
100 fp = file(f)
101 munger = config.ConfigMunger(fp)
102 for section, options in sections:
103 if section in munger.sections():
104 if options:
105 for option in options:
106 value = munger.get(section, option)
107 if value is not None:
108 print value
109 else:
110 config.ConfigMunger({section: munger[section]}).write()
111
112 def delete(args=None):
113
114 # process arguments
115 if args is None:
116 args = sys.argv[1:]
117 files, sections = parse_args(*args)
118
119 # display usage information
120 if not files:
121 print 'Usage:'
122 print usage
123 sys.exit(0)
124
125 # process the files
126 for f in files:
127 if f == '-':
128 fp = sys.stdin
129 else:
130 fp = file(f)
131 conf = config.ConfigMunger(fp).dict()
132 for section, options in sections:
133 if section in conf:
134 if options:
135 for option in options:
136 if option in conf[section]:
137 conf[section].pop(option)
138 else:
139 conf.pop(section)
140 if f == '-':
141 fp = sys.stdout
142 else:
143 fp.close()
144 fp = file(f, 'w')
145 config.ConfigMunger(conf).write(fp)
146
147
148 if __name__ == '__main__':
149 set(sys.argv[1:])