diff martini/main.py @ 0:3c3522ce6e3a

initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
author k0s <k0scist@gmail.com>
date Tue, 08 Dec 2009 15:13:28 -0500
parents
children 5627074cd79b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/martini/main.py	Tue Dec 08 15:13:28 2009 -0500
@@ -0,0 +1,149 @@
+#!/usr/bin/env python
+
+usage = "martini 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.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.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.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)
+
+ 
+if __name__ == '__main__':
+    set(sys.argv[1:])