blob: 260a207ff47b332b12d6628eaf0dd6b4462eb450 [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).
Michael Walshe5391dc2019-07-22 17:18:30 -05006See help text for details.
Michael Walsh4cae87d2017-01-12 14:46:05 -06007"""
8
9import sys
10
Michael Walsh4cae87d2017-01-12 14:46:05 -060011save_path_0 = sys.path[0]
12del sys.path[0]
13
14from gen_arg import *
15from gen_print import *
16from gen_valid import *
17
18# Restore sys.path[0].
19sys.path.insert(0, save_path_0)
20
Michael Walsh2d8d7f32018-06-01 15:21:09 -050021# Set exit_on_error for gen_valid functions.
22set_exit_on_error(True)
23
Michael Walsh4cae87d2017-01-12 14:46:05 -060024parser = argparse.ArgumentParser(
25 usage='%(prog)s [OPTIONS]',
26 description="%(prog)s will...",
Michael Walshd0741f82017-12-21 14:04:21 -060027 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Michael Walsh4cae87d2017-01-12 14:46:05 -060028 prefix_chars='-+')
29
Michael Walsh4cae87d2017-01-12 14:46:05 -060030parser.add_argument(
31 '--whatever',
Michael Walsh2d8d7f32018-06-01 15:21:09 -050032 default='',
Michael Walsh4cae87d2017-01-12 14:46:05 -060033 help='bla, bla.')
34
Michael Walsh41dda1b2017-09-14 11:42:29 -050035# Populate stock_list with options we want.
Michael Walsh4cae87d2017-01-12 14:46:05 -060036stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
Michael Walsh4cae87d2017-01-12 14:46:05 -060037
38
Michael Walsh4cae87d2017-01-12 14:46:05 -060039def exit_function(signal_number=0,
40 frame=None):
Michael Walsh4cae87d2017-01-12 14:46:05 -060041 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 Walsh4cae87d2017-01-12 14:46:05 -060053
Michael Walsh4cae87d2017-01-12 14:46:05 -060054def signal_handler(signal_number,
55 frame):
Michael Walsh4cae87d2017-01-12 14:46:05 -060056 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 Walsh4cae87d2017-01-12 14:46:05 -060071
Michael Walsh4cae87d2017-01-12 14:46:05 -060072def validate_parms():
Michael Walsh4cae87d2017-01-12 14:46:05 -060073 r"""
Michael Walsh2d8d7f32018-06-01 15:21:09 -050074 Validate program parameters, etc.
Michael Walsh4cae87d2017-01-12 14:46:05 -060075 """
76
Michael Walsh2d8d7f32018-06-01 15:21:09 -050077 # Your validation code here...
78 # valid_value(whatever)
Michael Walsh4cae87d2017-01-12 14:46:05 -060079
80 gen_post_validation(exit_function, signal_handler)
81
Michael Walsh4cae87d2017-01-12 14:46:05 -060082
Michael Walsh4cae87d2017-01-12 14:46:05 -060083def main():
84
Michael Walsh2d8d7f32018-06-01 15:21:09 -050085 gen_get_options(parser, stock_list)
Michael Walsh4cae87d2017-01-12 14:46:05 -060086
Michael Walsh2d8d7f32018-06-01 15:21:09 -050087 validate_parms()
Michael Walsh4cae87d2017-01-12 14:46:05 -060088
89 qprint_pgm_header()
90
91 # Your code here.
92
Michael Walsh4cae87d2017-01-12 14:46:05 -060093
Michael Walsh2d8d7f32018-06-01 15:21:09 -050094main()