1#!/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).
6See help text for details.
7"""
8
9import sys
10
11save_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
21# Set exit_on_error for gen_valid functions.
22set_exit_on_error(True)
23
24parser = argparse.ArgumentParser(
25    usage='%(prog)s [OPTIONS]',
26    description="%(prog)s will...",
27    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
28    prefix_chars='-+')
29
30parser.add_argument(
31    '--whatever',
32    default='',
33    help='bla, bla.')
34
35# Populate stock_list with options we want.
36stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
37
38
39def exit_function(signal_number=0,
40                  frame=None):
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
53
54def signal_handler(signal_number,
55                   frame):
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
71
72def validate_parms():
73    r"""
74    Validate program parameters, etc.
75    """
76
77    # Your validation code here...
78    # valid_value(whatever)
79
80    gen_post_validation(exit_function, signal_handler)
81
82
83def main():
84
85    gen_get_options(parser, stock_list)
86
87    validate_parms()
88
89    qprint_pgm_header()
90
91    # Your code here.
92
93
94main()
95