view martini/main.py @ 1:5627074cd79b

* adding ini-munge command * bumping version
author k0s <k0scist@gmail.com>
date Tue, 08 Dec 2009 15:43:16 -0500
parents 3c3522ce6e3a
children d172c078f4a2
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

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:]
    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)
    munger.write(sys.stdout)
 
if __name__ == '__main__':
    set(sys.argv[1:])