| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python | 
 | 2 |  | 
 | 3 | r""" | 
 | 4 | python_pgm_template: Copy this template as a base to get a start on a python | 
 | 5 | program.  You may remove any generic comments (like this one). | 
 | 6 | """ | 
 | 7 |  | 
 | 8 | import sys | 
 | 9 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 10 | save_path_0 = sys.path[0] | 
 | 11 | del sys.path[0] | 
 | 12 |  | 
 | 13 | from gen_arg import * | 
 | 14 | from gen_print import * | 
 | 15 | from gen_valid import * | 
 | 16 |  | 
 | 17 | # Restore sys.path[0]. | 
 | 18 | sys.path.insert(0, save_path_0) | 
 | 19 |  | 
| Michael Walsh | 2d8d7f3 | 2018-06-01 15:21:09 -0500 | [diff] [blame] | 20 | # Set exit_on_error for gen_valid functions. | 
 | 21 | set_exit_on_error(True) | 
 | 22 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 23 | parser = argparse.ArgumentParser( | 
 | 24 |     usage='%(prog)s [OPTIONS]', | 
 | 25 |     description="%(prog)s will...", | 
| Michael Walsh | d0741f8 | 2017-12-21 14:04:21 -0600 | [diff] [blame] | 26 |     formatter_class=argparse.ArgumentDefaultsHelpFormatter, | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 27 |     prefix_chars='-+') | 
 | 28 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 29 | parser.add_argument( | 
 | 30 |     '--whatever', | 
| Michael Walsh | 2d8d7f3 | 2018-06-01 15:21:09 -0500 | [diff] [blame] | 31 |     default='', | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 32 |     help='bla, bla.') | 
 | 33 |  | 
| Michael Walsh | 41dda1b | 2017-09-14 11:42:29 -0500 | [diff] [blame] | 34 | # Populate stock_list with options we want. | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 35 | stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)] | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 36 |  | 
 | 37 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 38 | def exit_function(signal_number=0, | 
 | 39 |                   frame=None): | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 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 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 52 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 53 | def signal_handler(signal_number, | 
 | 54 |                    frame): | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 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 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 70 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 71 | def validate_parms(): | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 72 |     r""" | 
| Michael Walsh | 2d8d7f3 | 2018-06-01 15:21:09 -0500 | [diff] [blame] | 73 |     Validate program parameters, etc. | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 74 |     """ | 
 | 75 |  | 
| Michael Walsh | 2d8d7f3 | 2018-06-01 15:21:09 -0500 | [diff] [blame] | 76 |     # Your validation code here... | 
 | 77 |     # valid_value(whatever) | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 78 |  | 
 | 79 |     gen_post_validation(exit_function, signal_handler) | 
 | 80 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 81 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 82 | def main(): | 
 | 83 |  | 
| Michael Walsh | 2d8d7f3 | 2018-06-01 15:21:09 -0500 | [diff] [blame] | 84 |     gen_get_options(parser, stock_list) | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 85 |  | 
| Michael Walsh | 2d8d7f3 | 2018-06-01 15:21:09 -0500 | [diff] [blame] | 86 |     validate_parms() | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 87 |  | 
 | 88 |     qprint_pgm_header() | 
 | 89 |  | 
 | 90 |     # Your code here. | 
 | 91 |  | 
| Michael Walsh | 4cae87d | 2017-01-12 14:46:05 -0600 | [diff] [blame] | 92 |  | 
| Michael Walsh | 2d8d7f3 | 2018-06-01 15:21:09 -0500 | [diff] [blame] | 93 | main() |