Created
July 26, 2015 19:29
-
-
Save eiri/6e3d1c945c9b8d0c5db1 to your computer and use it in GitHub Desktop.
Generic python script skeleton
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
#!/usr/bin/env python | |
# coding: utf-8 | |
""" | |
Generic python script skeleton | |
""" | |
import sys, os, argparse, logging, ConfigParser | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-d', '--debug', action='store_true') | |
parser.add_argument('--integer', type=int, default=1) | |
return parser.parse_args() | |
def init_logging(log_file, debug=True): | |
if debug: | |
logging.basicConfig( | |
format='%(asctime)s %(funcName)s:%(lineno)d - %(message)s', | |
datefmt="%H:%M:%S", | |
level=logging.DEBUG) | |
else: | |
logging.basicConfig( | |
format='%(asctime)s [%(levelname)8s] %(module)s:%(funcName)s - %(message)s', | |
filename=log_file, | |
datefmt="%Y-%m-%d %H:%M:%S", | |
level=logging.INFO) | |
def load_config(config_file): | |
Config = ConfigParser.ConfigParser() | |
Config.read(config_file) | |
return Config | |
def main(): | |
args = parse_args() | |
me = os.path.splitext(os.path.basename(__file__))[0] | |
log_file = '{}.log'.format(me) | |
init_logging(log_file, args.debug) | |
config_file = 'config.ini' | |
logging.debug('Reading config {}'.format(config_file)) | |
cfg = load_config(config_file) | |
value = cfg.get('general', 'key') | |
logging.info('Ohai {0}! Config for general/key is {1:s}'.format(me, value)) | |
logging.debug('Done') | |
sys.exit(0) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment