pythonwise / pycfg / config.py

# Simple Python based configuration system

# Usage:
# If this file is in PYTHONPATH, then 'import config'
# Otherwise, something like the following should work (Python 2x)
#
#    def load_config(path):
#        class _cfg: pass
#        cfg = _cfg()
#        cfg.__file__ = path
#
#        execfile(path, {}, cfg.__dict__)
#        return cfg

# Overrideable configuration, keep it flat
web_host = 'localhost'
web_port = 8080

# Overriding is easy, just add config_local.py and write the values you want to
# change. In this example we have only the line 'web_port = 8000' in
# config_local.py
try:
    import sys as __sys
    from os.path import dirname as __dirname
    __sys.path.insert(0, __dirname(__file__))

    from config_local import *  # NOQA
except ImportError:  # Catch only ImportError here
    pass
finally:
    __sys.path.pop(0)
    del __sys, __dirname  # Keep namespace clean

# Here's it after overriding, we can construct more Pythonic objects here

web = {
    'host': web_host,
    'port': web_port
}

# Print configuration, for use in shell scripts
# (e.g. webhost=$(python config.py web_host))

if __name__ == '__main__':
    # Start with _ so they won't show up as configuration key
    from argparse import ArgumentParser as _AP
    _parser = _AP('configuration')
    _parser.add_argument('key', nargs='?', help='configuration key')

    _args = _parser.parse_args()

    env = dict(
        (key, value) for key, value in locals().items() if key[0] != '_')

    if _args.key:
        if _args.key not in env:
            raise SystemExit('error: {} not found'.format(_args.key))
        print(env[_args.key])
    else:
        for key, value in sorted(env.iteritems()):
            print('{}={}'.format(key, value))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.