Skip to content

Instantly share code, notes, and snippets.

@thanos
Created July 9, 2012 19:12
Show Gist options
  • Select an option

  • Save thanos/3078304 to your computer and use it in GitHub Desktop.

Select an option

Save thanos/3078304 to your computer and use it in GitHub Desktop.
a kind of simple sed for config files
#!/bin/env python
import sys
from ConfigParser import SafeConfigParser, NoOptionError
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-s", "--set", dest="set", action='append', help="use to add or change an option, eg --set=processor,autostart,true\nwill add autostart=true to the [processor] section")
parser.add_option("-g", "--get", dest="get", help="use to get an option, eg --get=processor,autostart\ncould return true, the default return falue is an empty string:''")
(options, args) = parser.parse_args()
config = SafeConfigParser()
config.readfp(sys.stdin)
if options.set:
for setting in options.set:
section,option,value = setting.split(",")
if not config.has_section(section):
config.add_section(section)
config.set(section,option,value)
config.write(sys.stdout)
elif options.get:
section,option = options.get.split(",")
try:
print config.get(section,option)
except NoOptionError:
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment