blob: 2b199487815c84e5ff9cfe7cb87e309fd47b3b00 [file] [log] [blame]
Michael Walsh4cae87d2017-01-12 14:46:05 -06001#!/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
Michael Walsh4cae87d2017-01-12 14:46:05 -060010save_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
Michael Walsh2d8d7f32018-06-01 15:21:09 -050020# Set exit_on_error for gen_valid functions.
21set_exit_on_error(True)
22
Michael Walsh4cae87d2017-01-12 14:46:05 -060023parser = argparse.ArgumentParser(
24 usage='%(prog)s [OPTIONS]',
25 description="%(prog)s will...",
Michael Walshd0741f82017-12-21 14:04:21 -060026 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Michael Walsh4cae87d2017-01-12 14:46:05 -060027 prefix_chars='-+')
28
Michael Walsh4cae87d2017-01-12 14:46:05 -060029parser.add_argument(
30 '--whatever',
Michael Walsh2d8d7f32018-06-01 15:21:09 -050031 default='',
Michael Walsh4cae87d2017-01-12 14:46:05 -060032 help='bla, bla.')
33
Michael Walsh41dda1b2017-09-14 11:42:29 -050034# Populate stock_list with options we want.
Michael Walsh4cae87d2017-01-12 14:46:05 -060035stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
Michael Walsh4cae87d2017-01-12 14:46:05 -060036
37
Michael Walsh4cae87d2017-01-12 14:46:05 -060038def exit_function(signal_number=0,
39 frame=None):
Michael Walsh4cae87d2017-01-12 14:46:05 -060040 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 Walsh4cae87d2017-01-12 14:46:05 -060052
Michael Walsh4cae87d2017-01-12 14:46:05 -060053def signal_handler(signal_number,
54 frame):
Michael Walsh4cae87d2017-01-12 14:46:05 -060055 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 Walsh4cae87d2017-01-12 14:46:05 -060070
Michael Walsh4cae87d2017-01-12 14:46:05 -060071def validate_parms():
Michael Walsh4cae87d2017-01-12 14:46:05 -060072 r"""
Michael Walsh2d8d7f32018-06-01 15:21:09 -050073 Validate program parameters, etc.
Michael Walsh4cae87d2017-01-12 14:46:05 -060074 """
75
Michael Walsh2d8d7f32018-06-01 15:21:09 -050076 # Your validation code here...
77 # valid_value(whatever)
Michael Walsh4cae87d2017-01-12 14:46:05 -060078
79 gen_post_validation(exit_function, signal_handler)
80
Michael Walsh4cae87d2017-01-12 14:46:05 -060081
Michael Walsh4cae87d2017-01-12 14:46:05 -060082def main():
83
Michael Walsh2d8d7f32018-06-01 15:21:09 -050084 gen_get_options(parser, stock_list)
Michael Walsh4cae87d2017-01-12 14:46:05 -060085
Michael Walsh2d8d7f32018-06-01 15:21:09 -050086 validate_parms()
Michael Walsh4cae87d2017-01-12 14:46:05 -060087
88 qprint_pgm_header()
89
90 # Your code here.
91
Michael Walsh4cae87d2017-01-12 14:46:05 -060092
Michael Walsh2d8d7f32018-06-01 15:21:09 -050093main()