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