Mercurial > hg > martINI
view martini/main.py @ 3:9e4da7517815
make a README file and include it in the setup.py
author | k0s <k0scist@gmail.com> |
---|---|
date | Tue, 02 Mar 2010 20:18:15 -0500 |
parents | d172c078f4a2 |
children | f7f335561ceb |
line wrap: on
line source
#!/usr/bin/env python usage = "%s file1 [file2] [...] --section1 option1=value1 option2=value2 --section2 option3=value3" import config import os import sys from optparse import OptionParser sep = '--' # section separator def parse_args(*args): """ convert command line options to files, sections, and options returns a tuple: ( [files], { 'section': { 'option': 'value', 'option2': 'value2' } } ) """ # find the files # XXX note this prohibits files starting with -- index = None for index, value in enumerate(args): if value.startswith(sep): break else: return (args, []) files = args[:index] args = args[index:] # find the sections ini = [] for arg in args: if arg.startswith(sep): arg = arg[len(sep):] assert arg section = [] ini.append((arg, section)) else: section.append(arg) return (files, ini) def parse_options(*args): files, sections = parse_args(*args) ini = {} for section, options in sections: ini[section] = {} for option in options: key, value = option.split('=', 1) ini[section][key] = value return (files, ini) def set(args=None): # process arguments if args is None: args = sys.argv[1:] files, sections = parse_options(*args) # display usage information if not files: print 'Usage:' print usage % sys.argv[0] sys.exit(0) # process the files for f in files: if f == '-': fp = sys.stdin else: fp = file(f) munger = config.ConfigMunger(fp, sections) if f == '-': fp = sys.stdout else: fp.close() fp = file(f, "w") munger.write(fp=fp) def get(args=None): # process arguments if args is None: args = sys.argv[1:] files, sections = parse_args(*args) # display usage information if not files: print 'Usage:' print usage % sys.argv[0] sys.exit(0) # process the files for f in files: if f == '-': fp = sys.stdin else: fp = file(f) munger = config.ConfigMunger(fp) for section, options in sections: if section in munger.sections(): if options: for option in options: value = munger.get(section, option) if value is not None: print value else: config.ConfigMunger({section: munger[section]}).write() def delete(args=None): # process arguments if args is None: args = sys.argv[1:] files, sections = parse_args(*args) # display usage information if not files: print 'Usage:' print usage % sys.argv[0] sys.exit(0) # process the files for f in files: if f == '-': fp = sys.stdin else: fp = file(f) conf = config.ConfigMunger(fp).dict() for section, options in sections: if section in conf: if options: for option in options: if option in conf[section]: conf[section].pop(option) else: conf.pop(section) if f == '-': fp = sys.stdout else: fp.close() fp = file(f, 'w') config.ConfigMunger(conf).write(fp) def munge(args=None): # process arguments if args is None: args = sys.argv[1:] parser = OptionParser() parser.add_option('-O', dest='output', help="name of file to output (stdout if not specified)") options, args = parser.parse_args(args) files, sections = parse_options(*args) # display usage information if not files: print 'Usage:' print usage % sys.argv[0] sys.exit(0) # munge the files conf = list(files) conf.append(sections) munger = config.ConfigMunger(*conf) if options.output: fp = file(options.output, 'w') else: fp = sys.stdout munger.write(fp) if __name__ == '__main__': set(sys.argv[1:])