1#!/usr/bin/env python 2 3r""" 4python_pgm_template: Copy this template as a base to get a start on a python 5program. You may remove any generic comments (like this one). 6""" 7 8import sys 9 10save_path_0 = sys.path[0] 11del sys.path[0] 12 13from gen_arg import * 14from gen_print import * 15from gen_valid import * 16 17# Restore sys.path[0]. 18sys.path.insert(0, save_path_0) 19 20# Set exit_on_error for gen_valid functions. 21set_exit_on_error(True) 22 23parser = argparse.ArgumentParser( 24 usage='%(prog)s [OPTIONS]', 25 description="%(prog)s will...", 26 formatter_class=argparse.ArgumentDefaultsHelpFormatter, 27 prefix_chars='-+') 28 29parser.add_argument( 30 '--whatever', 31 default='', 32 help='bla, bla.') 33 34# Populate stock_list with options we want. 35stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)] 36 37 38def exit_function(signal_number=0, 39 frame=None): 40 r""" 41 Execute whenever the program ends normally or with the signals that we 42 catch (i.e. TERM, INT). 43 """ 44 45 dprint_executing() 46 dprint_var(signal_number) 47 48 # Your cleanup code here. 49 50 qprint_pgm_footer() 51 52 53def signal_handler(signal_number, 54 frame): 55 r""" 56 Handle signals. Without a function to catch a SIGTERM or SIGINT, our 57 program would terminate immediately with return code 143 and without 58 calling our exit_function. 59 """ 60 61 # Our convention is to set up exit_function with atexit.register() so 62 # there is no need to explicitly call exit_function from here. 63 64 dprint_executing() 65 66 # Calling exit prevents us from returning to the code that was running 67 # when we received the signal. 68 exit(0) 69 70 71def validate_parms(): 72 r""" 73 Validate program parameters, etc. 74 """ 75 76 # Your validation code here... 77 # valid_value(whatever) 78 79 gen_post_validation(exit_function, signal_handler) 80 81 82def main(): 83 84 gen_get_options(parser, stock_list) 85 86 validate_parms() 87 88 qprint_pgm_header() 89 90 # Your code here. 91 92 93main() 94