|  | #!/usr/bin/env python | 
|  |  | 
|  | r""" | 
|  | python_pgm_template: Copy this template as a base to get a start on a python | 
|  | program.  You may remove any generic comments (like this one). | 
|  | See help text for details. | 
|  | """ | 
|  |  | 
|  | import sys | 
|  |  | 
|  | save_path_0 = sys.path[0] | 
|  | del sys.path[0] | 
|  |  | 
|  | from gen_arg import * | 
|  | from gen_print import * | 
|  | from gen_valid import * | 
|  |  | 
|  | # Restore sys.path[0]. | 
|  | sys.path.insert(0, save_path_0) | 
|  |  | 
|  | # Set exit_on_error for gen_valid functions. | 
|  | set_exit_on_error(True) | 
|  |  | 
|  | parser = argparse.ArgumentParser( | 
|  | usage='%(prog)s [OPTIONS]', | 
|  | description="%(prog)s will...", | 
|  | formatter_class=argparse.ArgumentDefaultsHelpFormatter, | 
|  | prefix_chars='-+') | 
|  |  | 
|  | parser.add_argument( | 
|  | '--whatever', | 
|  | default='', | 
|  | help='bla, bla.') | 
|  |  | 
|  | # Populate stock_list with options we want. | 
|  | stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)] | 
|  |  | 
|  |  | 
|  | def exit_function(signal_number=0, | 
|  | frame=None): | 
|  | r""" | 
|  | Execute whenever the program ends normally or with the signals that we | 
|  | catch (i.e. TERM, INT). | 
|  | """ | 
|  |  | 
|  | dprint_executing() | 
|  | dprint_var(signal_number) | 
|  |  | 
|  | # Your cleanup code here. | 
|  |  | 
|  | qprint_pgm_footer() | 
|  |  | 
|  |  | 
|  | def signal_handler(signal_number, | 
|  | frame): | 
|  | r""" | 
|  | Handle signals.  Without a function to catch a SIGTERM or SIGINT, our | 
|  | program would terminate immediately with return code 143 and without | 
|  | calling our exit_function. | 
|  | """ | 
|  |  | 
|  | # Our convention is to set up exit_function with atexit.register() so | 
|  | # there is no need to explicitly call exit_function from here. | 
|  |  | 
|  | dprint_executing() | 
|  |  | 
|  | # Calling exit prevents us from returning to the code that was running | 
|  | # when we received the signal. | 
|  | exit(0) | 
|  |  | 
|  |  | 
|  | def validate_parms(): | 
|  | r""" | 
|  | Validate program parameters, etc. | 
|  | """ | 
|  |  | 
|  | # Your validation code here... | 
|  | # valid_value(whatever) | 
|  |  | 
|  | gen_post_validation(exit_function, signal_handler) | 
|  |  | 
|  |  | 
|  | def main(): | 
|  |  | 
|  | gen_get_options(parser, stock_list) | 
|  |  | 
|  | validate_parms() | 
|  |  | 
|  | qprint_pgm_header() | 
|  |  | 
|  | # Your code here. | 
|  |  | 
|  |  | 
|  | main() |