Created
July 9, 2012 19:12
-
-
Save thanos/3078304 to your computer and use it in GitHub Desktop.
a kind of simple sed for config files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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