annotate martini/main.py @ 23:858fb3bfacfe default tip

python3
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Apr 2021 15:04:15 -0700
parents 112cb171b419
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
13d85b455533 add a module docstring
k0s <k0scist@gmail.com>
parents: 4
diff changeset
1 """
13d85b455533 add a module docstring
k0s <k0scist@gmail.com>
parents: 4
diff changeset
2 main front-end functions for command-line programs
13d85b455533 add a module docstring
k0s <k0scist@gmail.com>
parents: 4
diff changeset
3 """
13d85b455533 add a module docstring
k0s <k0scist@gmail.com>
parents: 4
diff changeset
4
22
112cb171b419 python3
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
5 from . import config
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
6 import os
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
7 import sys
2
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
8 from optparse import OptionParser
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
9
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
10 sep = '--' # section separator
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
11
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
12 def parse_args(*args):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
13 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
14 convert command line options to files, sections, and options
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
15 returns a tuple:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
16 ( [files], { 'section': { 'option': 'value', 'option2': 'value2' } } )
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
17 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
18
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
19 # find the files
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
20 # XXX note this prohibits files starting with --
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
21 index = None
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
22 for index, value in enumerate(args):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
23 if value.startswith(sep):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
24 break
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
25 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
26 return (args, [])
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
27
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
28 files = args[:index]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
29 args = args[index:]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
30
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
31 # find the sections
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
32 ini = []
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
33 for arg in args:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
34 if arg.startswith(sep):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
35 arg = arg[len(sep):]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
36 assert arg
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
37 section = []
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
38 ini.append((arg, section))
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
39 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
40 section.append(arg)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
41
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
42 return (files, ini)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
43
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
44 def parse_options(*args):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
45 files, sections = parse_args(*args)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
46 ini = {}
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
47 for section, options in sections:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
48 ini[section] = {}
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
49 for option in options:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
50 key, value = option.split('=', 1)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
51 ini[section][key] = value
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
52
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
53 return (files, ini)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
54
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
55 def set(args=None):
4
f7f335561ceb make special usage strings for each program; again, really should abstract this
k0s <k0scist@gmail.com>
parents: 2
diff changeset
56
f7f335561ceb make special usage strings for each program; again, really should abstract this
k0s <k0scist@gmail.com>
parents: 2
diff changeset
57 usage = "%s file1 [file2] [...] --section1 option1=value1 option2=value2 --section2 option3=value3"
12
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
58
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
59 # process arguments
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
60 if args is None:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
61 args = sys.argv[1:]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
62 files, sections = parse_options(*args)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
63
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
64 # display usage information
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
65 if not files:
12
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
66 print ('Usage:')
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
67 print (usage % os.path.basename(sys.argv[0]))
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
68 sys.exit(0)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
69
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
70 # process the files
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
71 for f in files:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
72 if f == '-':
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
73 fp = sys.stdin
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
74 else:
23
858fb3bfacfe python3
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
75 fp = open(f)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
76 munger = config.ConfigMunger(fp, sections)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
77
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
78 if f == '-':
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
79 fp = sys.stdout
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
80 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
81 fp.close()
23
858fb3bfacfe python3
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
82 fp = open(f, "w")
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
83 munger.write(fp=fp)
23
858fb3bfacfe python3
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
84
858fb3bfacfe python3
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
85
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
86 def get(args=None):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
87
4
f7f335561ceb make special usage strings for each program; again, really should abstract this
k0s <k0scist@gmail.com>
parents: 2
diff changeset
88 usage = "%s file1 [file2] [...] --section1 option1 option2 --section2 option3"
f7f335561ceb make special usage strings for each program; again, really should abstract this
k0s <k0scist@gmail.com>
parents: 2
diff changeset
89
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
90 # process arguments
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
91 if args is None:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
92 args = sys.argv[1:]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
93 files, sections = parse_args(*args)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
94
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
95 # display usage information
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
96 if not files:
21
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
97 print('Usage:')
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
98 print(usage % os.path.basename(sys.argv[0]))
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
99 sys.exit(0)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
100
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
101 # process the files
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
102 for f in files:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
103 if f == '-':
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
104 fp = sys.stdin
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
105 else:
23
858fb3bfacfe python3
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
106 fp = open(f)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
107 munger = config.ConfigMunger(fp)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
108 for section, options in sections:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
109 if section in munger.sections():
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
110 if options:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
111 for option in options:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
112 value = munger.get(section, option)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
113 if value is not None:
21
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
114 print(value)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
115 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
116 config.ConfigMunger({section: munger[section]}).write()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
117
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
118 def delete(args=None):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
119
4
f7f335561ceb make special usage strings for each program; again, really should abstract this
k0s <k0scist@gmail.com>
parents: 2
diff changeset
120 usage = "%s file1 [file2] [...] --section1 option1 option2 --section2 option3"
f7f335561ceb make special usage strings for each program; again, really should abstract this
k0s <k0scist@gmail.com>
parents: 2
diff changeset
121
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
122 # process arguments
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
123 if args is None:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
124 args = sys.argv[1:]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
125 files, sections = parse_args(*args)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
126
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
127 # display usage information
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
128 if not files:
21
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
129 print('Usage:')
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
130 print(usage % os.path.basename(sys.argv[0]))
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
131 sys.exit(0)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
132
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
133 # process the files
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
134 for f in files:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
135 if f == '-':
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
136 fp = sys.stdin
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
137 else:
23
858fb3bfacfe python3
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
138 fp = open(f)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
139 conf = config.ConfigMunger(fp).dict()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
140 for section, options in sections:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
141 if section in conf:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
142 if options:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
143 for option in options:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
144 if option in conf[section]:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
145 conf[section].pop(option)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
146 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
147 conf.pop(section)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
148 if f == '-':
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
149 fp = sys.stdout
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
150 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
151 fp.close()
21
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
152 fp = open(f, 'w')
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
153 config.ConfigMunger(conf).write(fp)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
154
1
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
155 def munge(args=None):
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
156
4
f7f335561ceb make special usage strings for each program; again, really should abstract this
k0s <k0scist@gmail.com>
parents: 2
diff changeset
157 usage = "%s file1 [file2] [...]"
f7f335561ceb make special usage strings for each program; again, really should abstract this
k0s <k0scist@gmail.com>
parents: 2
diff changeset
158
1
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
159 # process arguments
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
160 if args is None:
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
161 args = sys.argv[1:]
2
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
162
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
163 parser = OptionParser()
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
164 parser.add_option('-O', dest='output',
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
165 help="name of file to output (stdout if not specified)")
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
166 options, args = parser.parse_args(args)
1
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
167 files, sections = parse_options(*args)
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
168
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
169 # display usage information
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
170 if not files:
21
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
171 print('Usage:')
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
172 print(usage % os.path.basename(sys.argv[0]))
1
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
173 sys.exit(0)
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
174
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
175 # munge the files
21
85bbb83703fa python3
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
176 conf = list(files)
1
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
177 conf.append(sections)
5627074cd79b * adding ini-munge command
k0s <k0scist@gmail.com>
parents: 0
diff changeset
178 munger = config.ConfigMunger(*conf)
2
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
179 if options.output:
23
858fb3bfacfe python3
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
180 fp = open(options.output, 'w')
2
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
181 else:
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
182 fp = sys.stdout
d172c078f4a2 add -O option for specifying output file
k0s <k0scist@gmail.com>
parents: 1
diff changeset
183 munger.write(fp)