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