Skip to content

Instantly share code, notes, and snippets.

@oogali
Last active January 4, 2016 09:29
Show Gist options
  • Save oogali/8602744 to your computer and use it in GitHub Desktop.
Save oogali/8602744 to your computer and use it in GitHub Desktop.
Easy peasy configurations
import os
import sys
import logging
import ConfigParser
def main(argv=None):
if argv is None:
argv = sys.argv
logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', level=logging.DEBUG)
config_file = os.path.expanduser('~/.test-project-config')
if not os.path.exists(config_file):
logging.error('Configuration file ({}) does not exist'.format(config_file))
return -1
config = ConfigParser.SafeConfigParser()
try:
config.read(config_file)
server = config.get('section', 'server')
port = int(config.get('section', 'port'))
is_prod = config.getboolean('section', 'live')
except Exception, e:
logging.error('Could not read configuration settings')
logging.exception(e)
# do stuff
# ...
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment