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