comparison 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
comparison
equal deleted inserted replaced
22:112cb171b419 23:858fb3bfacfe
1 #!/usr/bin/env python
2
3 """ 1 """
4 main front-end functions for command-line programs 2 main front-end functions for command-line programs
5 """ 3 """
6 4
7 from . import config 5 from . import config
72 # process the files 70 # process the files
73 for f in files: 71 for f in files:
74 if f == '-': 72 if f == '-':
75 fp = sys.stdin 73 fp = sys.stdin
76 else: 74 else:
77 fp = file(f) 75 fp = open(f)
78 munger = config.ConfigMunger(fp, sections) 76 munger = config.ConfigMunger(fp, sections)
79 77
80 if f == '-': 78 if f == '-':
81 fp = sys.stdout 79 fp = sys.stdout
82 else: 80 else:
83 fp.close() 81 fp.close()
84 fp = file(f, "w") 82 fp = open(f, "w")
85 munger.write(fp=fp) 83 munger.write(fp=fp)
86 84
85
87 def get(args=None): 86 def get(args=None):
88 87
89 usage = "%s file1 [file2] [...] --section1 option1 option2 --section2 option3" 88 usage = "%s file1 [file2] [...] --section1 option1 option2 --section2 option3"
90 89
91 # process arguments 90 # process arguments
102 # process the files 101 # process the files
103 for f in files: 102 for f in files:
104 if f == '-': 103 if f == '-':
105 fp = sys.stdin 104 fp = sys.stdin
106 else: 105 else:
107 fp = file(f) 106 fp = open(f)
108 munger = config.ConfigMunger(fp) 107 munger = config.ConfigMunger(fp)
109 for section, options in sections: 108 for section, options in sections:
110 if section in munger.sections(): 109 if section in munger.sections():
111 if options: 110 if options:
112 for option in options: 111 for option in options:
134 # process the files 133 # process the files
135 for f in files: 134 for f in files:
136 if f == '-': 135 if f == '-':
137 fp = sys.stdin 136 fp = sys.stdin
138 else: 137 else:
139 fp = file(f) 138 fp = open(f)
140 conf = config.ConfigMunger(fp).dict() 139 conf = config.ConfigMunger(fp).dict()
141 for section, options in sections: 140 for section, options in sections:
142 if section in conf: 141 if section in conf:
143 if options: 142 if options:
144 for option in options: 143 for option in options:
176 # munge the files 175 # munge the files
177 conf = list(files) 176 conf = list(files)
178 conf.append(sections) 177 conf.append(sections)
179 munger = config.ConfigMunger(*conf) 178 munger = config.ConfigMunger(*conf)
180 if options.output: 179 if options.output:
181 fp = file(options.output, 'w') 180 fp = open(options.output, 'w')
182 else: 181 else:
183 fp = sys.stdout 182 fp = sys.stdout
184 munger.write(fp) 183 munger.write(fp)
185
186 if __name__ == '__main__':
187 set(sys.argv[1:])