Mercurial > hg > martINI
comparison martini/main.py @ 4:f7f335561ceb
make special usage strings for each program; again, really should abstract this
author | k0s <k0scist@gmail.com> |
---|---|
date | Tue, 02 Mar 2010 20:27:25 -0500 |
parents | d172c078f4a2 |
children | 13d85b455533 |
comparison
equal
deleted
inserted
replaced
3:9e4da7517815 | 4:f7f335561ceb |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | |
3 usage = "%s file1 [file2] [...] --section1 option1=value1 option2=value2 --section2 option3=value3" | |
4 | 2 |
5 import config | 3 import config |
6 import os | 4 import os |
7 import sys | 5 import sys |
8 from optparse import OptionParser | 6 from optparse import OptionParser |
51 ini[section][key] = value | 49 ini[section][key] = value |
52 | 50 |
53 return (files, ini) | 51 return (files, ini) |
54 | 52 |
55 def set(args=None): | 53 def set(args=None): |
54 | |
55 usage = "%s file1 [file2] [...] --section1 option1=value1 option2=value2 --section2 option3=value3" | |
56 | 56 |
57 # process arguments | 57 # process arguments |
58 if args is None: | 58 if args is None: |
59 args = sys.argv[1:] | 59 args = sys.argv[1:] |
60 files, sections = parse_options(*args) | 60 files, sections = parse_options(*args) |
61 | 61 |
62 # display usage information | 62 # display usage information |
63 if not files: | 63 if not files: |
64 print 'Usage:' | 64 print 'Usage:' |
65 print usage % sys.argv[0] | 65 print usage % os.path.basename(sys.argv[0]) |
66 sys.exit(0) | 66 sys.exit(0) |
67 | 67 |
68 # process the files | 68 # process the files |
69 for f in files: | 69 for f in files: |
70 if f == '-': | 70 if f == '-': |
80 fp = file(f, "w") | 80 fp = file(f, "w") |
81 munger.write(fp=fp) | 81 munger.write(fp=fp) |
82 | 82 |
83 def get(args=None): | 83 def get(args=None): |
84 | 84 |
85 usage = "%s file1 [file2] [...] --section1 option1 option2 --section2 option3" | |
86 | |
85 # process arguments | 87 # process arguments |
86 if args is None: | 88 if args is None: |
87 args = sys.argv[1:] | 89 args = sys.argv[1:] |
88 files, sections = parse_args(*args) | 90 files, sections = parse_args(*args) |
89 | 91 |
90 # display usage information | 92 # display usage information |
91 if not files: | 93 if not files: |
92 print 'Usage:' | 94 print 'Usage:' |
93 print usage % sys.argv[0] | 95 print usage % os.path.basename(sys.argv[0]) |
94 sys.exit(0) | 96 sys.exit(0) |
95 | 97 |
96 # process the files | 98 # process the files |
97 for f in files: | 99 for f in files: |
98 if f == '-': | 100 if f == '-': |
110 else: | 112 else: |
111 config.ConfigMunger({section: munger[section]}).write() | 113 config.ConfigMunger({section: munger[section]}).write() |
112 | 114 |
113 def delete(args=None): | 115 def delete(args=None): |
114 | 116 |
117 usage = "%s file1 [file2] [...] --section1 option1 option2 --section2 option3" | |
118 | |
115 # process arguments | 119 # process arguments |
116 if args is None: | 120 if args is None: |
117 args = sys.argv[1:] | 121 args = sys.argv[1:] |
118 files, sections = parse_args(*args) | 122 files, sections = parse_args(*args) |
119 | 123 |
120 # display usage information | 124 # display usage information |
121 if not files: | 125 if not files: |
122 print 'Usage:' | 126 print 'Usage:' |
123 print usage % sys.argv[0] | 127 print usage % os.path.basename(sys.argv[0]) |
124 sys.exit(0) | 128 sys.exit(0) |
125 | 129 |
126 # process the files | 130 # process the files |
127 for f in files: | 131 for f in files: |
128 if f == '-': | 132 if f == '-': |
145 fp = file(f, 'w') | 149 fp = file(f, 'w') |
146 config.ConfigMunger(conf).write(fp) | 150 config.ConfigMunger(conf).write(fp) |
147 | 151 |
148 def munge(args=None): | 152 def munge(args=None): |
149 | 153 |
154 usage = "%s file1 [file2] [...]" | |
155 | |
150 # process arguments | 156 # process arguments |
151 if args is None: | 157 if args is None: |
152 args = sys.argv[1:] | 158 args = sys.argv[1:] |
153 | 159 |
154 parser = OptionParser() | 160 parser = OptionParser() |
158 files, sections = parse_options(*args) | 164 files, sections = parse_options(*args) |
159 | 165 |
160 # display usage information | 166 # display usage information |
161 if not files: | 167 if not files: |
162 print 'Usage:' | 168 print 'Usage:' |
163 print usage % sys.argv[0] | 169 print usage % os.path.basename(sys.argv[0]) |
164 sys.exit(0) | 170 sys.exit(0) |
165 | 171 |
166 # munge the files | 172 # munge the files |
167 conf = list(files) | 173 conf = list(files) |
168 conf.append(sections) | 174 conf.append(sections) |