#!/usr/bin/python import sys from configobj import ConfigObj SYNOPSIS='SYNOPSIS: inised
' ## first testing for file existence try: f = open(sys.argv[1],'r') except IOError: print "file '%s' not found" % (sys.argv[1]) sys.exit(1) except IndexError: print SYNOPSIS sys.exit(1) # file exist ... filename = sys.argv[1] print "# file %s found ..." % (filename) ini = ConfigObj(filename) try: sys.argv[2] except IndexError: print "command 'add-key' or 'remove-key' where expected" print SYNOPSIS sys.exit(1) cmd = sys.argv[2] if cmd == 'add-key': sectionName = sys.argv[3] key = sys.argv[4] value = sys.argv[5] try: ini[sectionName] print "section '%s' found ..." % (sectionName) except KeyError: ini[sectionName]={} ini.write() print "section '%s' added ..." % (sectionName) print "add/update key '%s' with value '%s' in section '%s' ..." % (key,value,sectionName) ini[sectionName][key]=value ini.write() elif cmd == 'remove-key': sectionName = sys.argv[3] key = sys.argv[4] try: section = ini[sectionName] try: section.__delitem__(key) except KeyError: print "key '%s' not found..." % (key) except KeyError: print "section '%s' not found..." % (sectionName) ini.write() print " key '%s' from section '%s' removed ..." % (key,sectionName) else: print "command '%s' not found" % (cmd) print SYNOPSIS sys.exit(1)