Skip to content

Instantly share code, notes, and snippets.

@mckern
Last active August 29, 2015 14:08
Show Gist options
  • Save mckern/13a7f78bacf75d6e2ba0 to your computer and use it in GitHub Desktop.
Save mckern/13a7f78bacf75d6e2ba0 to your computer and use it in GitHub Desktop.
Convert a set of INI file configurations into shell variables... for... thing?
#!/usr/bin/env python
"""A simple ini config parser, which parses data passed across STDIN
and echos the values of an ini file to the local environment
as KEY=VALUE pairs based on the section of the ini file that they're in.
For example, the key "name" in section "config" would look like this:
\t[config]
\tname=value
This would be echoed back as:
CONFIG_NAME=value
"""
import sys, re, argparse, ConfigParser
PARSER = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
PARSER.add_argument('-p', '--prefix', default='',
help='prepend each value with an arbitrary string')
ARGS = PARSER.parse_args()
CONFIG = ConfigParser.ConfigParser()
CONFIG.readfp(sys.stdin)
for sec in CONFIG.sections():
for key, val in CONFIG.items(sec):
print("%s%s_%s=\"%s\"" % (ARGS.prefix, re.sub(r'[\s.:-]', '_', sec.upper()), key.upper(), val))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment