Last active
January 4, 2016 09:29
-
-
Save oogali/8602744 to your computer and use it in GitHub Desktop.
Easy peasy configurations
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
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