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).
6"""
7
8import sys
9
10# python puts the program's directory path in sys.path[0].  In other words,
11# the user ordinarily has no way to override python's choice of a module from
12# its own dir.  We want to have that ability in our environment.  However, we
13# don't want to break any established python modules that depend on this
14# behavior.  So, we'll save the value from sys.path[0], delete it, import our
15# modules and then restore sys.path to its original value.
16
17save_path_0 = sys.path[0]
18del sys.path[0]
19
20from gen_arg import *
21from gen_print import *
22from gen_valid import *
23
24# Restore sys.path[0].
25sys.path.insert(0, save_path_0)
26
27###############################################################################
28# Create parser object to process command line parameters and args.
29
30# Create parser object.
31parser = argparse.ArgumentParser(
32    usage='%(prog)s [OPTIONS]',
33    description="%(prog)s will...",
34    formatter_class=argparse.RawTextHelpFormatter,
35    prefix_chars='-+')
36
37# Create arguments.
38parser.add_argument(
39    '--whatever',
40    help='bla, bla.')
41
42# The stock_list will be passed to gen_get_options.  We populate it with the
43# names of stock parm options we want.  These stock parms are pre-defined by
44# gen_get_options.
45stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
46###############################################################################
47
48
49###############################################################################
50def exit_function(signal_number=0,
51                  frame=None):
52
53    r"""
54    Execute whenever the program ends normally or with the signals that we
55    catch (i.e. TERM, INT).
56    """
57
58    dprint_executing()
59    dprint_var(signal_number)
60
61    # Your cleanup code here.
62
63    qprint_pgm_footer()
64
65###############################################################################
66
67
68###############################################################################
69def signal_handler(signal_number,
70                   frame):
71
72    r"""
73    Handle signals.  Without a function to catch a SIGTERM or SIGINT, our
74    program would terminate immediately with return code 143 and without
75    calling our exit_function.
76    """
77
78    # Our convention is to set up exit_function with atexit.register() so
79    # there is no need to explicitly call exit_function from here.
80
81    dprint_executing()
82
83    # Calling exit prevents us from returning to the code that was running
84    # when we received the signal.
85    exit(0)
86
87###############################################################################
88
89
90###############################################################################
91def validate_parms():
92
93    r"""
94    Validate program parameters, etc.  Return True or False (i.e. pass/fail)
95    accordingly.
96    """
97
98    # Your validation code here.
99
100    gen_post_validation(exit_function, signal_handler)
101
102    return True
103
104###############################################################################
105
106
107###############################################################################
108def main():
109
110    r"""
111    This is the "main" function.  The advantage of having this function vs
112    just doing this in the true mainline is that you can:
113    - Declare local variables
114    - Use "return" instead of "exit".
115    - Indent 4 chars like you would in any function.
116    This makes coding more consistent, i.e. it's easy to move code from here
117    into a function and vice versa.
118    """
119
120    if not gen_get_options(parser, stock_list):
121        return False
122
123    if not validate_parms():
124        return False
125
126    qprint_pgm_header()
127
128    # Your code here.
129
130    return True
131
132###############################################################################
133
134
135###############################################################################
136# Main
137
138if not main():
139    exit(1)
140
141###############################################################################
142