blob: c406246669fc869c771e52c1b774073558cf4bf0 [file] [log] [blame]
Michael Walshde791732016-09-06 14:25:24 -05001#!/usr/bin/env python
2
Michael Walsh7423c012016-10-04 10:27:21 -05003r"""
4This module provides many valuable functions such as my_parm_file.
5"""
Michael Walshde791732016-09-06 14:25:24 -05006
7# sys and os are needed to get the program dir path and program name.
8import sys
9import os
10import ConfigParser
11import StringIO
12
Michael Walsh7423c012016-10-04 10:27:21 -050013import gen_print as gp
14
Michael Walshde791732016-09-06 14:25:24 -050015
16###############################################################################
Michael Walsh7db77942017-01-10 11:37:06 -060017def add_trailing_slash(path):
18
19 r"""
20 Add a trailing slash to path if it doesn't already have one and return it.
21
22 """
23
24 return os.path.normpath(path) + os.path.sep
25
26###############################################################################
27
28
29###############################################################################
30def set_mod_global(var_value,
31 mod_name="__main__",
32 var_name=None):
33
34 r"""
35 Set a global variable for a given module.
36
37 Description of arguments:
38 var_value The value to set in the variable.
39 mod_name The name of the module whose variable is
40 to be set.
41 var_name The name of the variable to set. This
42 defaults to the name of the variable used
43 for var_value when calling this function.
44 """
45
46 try:
47 module = sys.modules[mod_name]
48 except KeyError:
49 gp.print_error_report("Programmer error - The mod_name passed to" +
50 " this function is invalid:\n" +
51 gp.sprint_var(mod_name))
52 raise ValueError('Programmer error.')
53
54 if var_name is None:
55 var_name = gp.get_arg_name(None, 1, 2)
56
57 setattr(module, var_name, var_value)
58
59###############################################################################
60
61
62###############################################################################
Michael Walshde791732016-09-06 14:25:24 -050063def my_parm_file(prop_file_path):
64
65 r"""
66 Read a properties file, put the keys/values into a dictionary and return
67 the dictionary.
68
69 The properties file must have the following format:
70 var_name<= or :>var_value
71 Comment lines (those beginning with a "#") and blank lines are allowed and
72 will be ignored. Leading and trailing single or double quotes will be
73 stripped from the value. E.g.
74 var1="This one"
75 Quotes are stripped so the resulting value for var1 is:
76 This one
77
78 Description of arguments:
Michael Walsh7423c012016-10-04 10:27:21 -050079 prop_file_path The caller should pass the path to the
80 properties file.
Michael Walshde791732016-09-06 14:25:24 -050081 """
82
83 # ConfigParser expects at least one section header in the file (or you
84 # get ConfigParser.MissingSectionHeaderError). Properties files don't
85 # need those so I'll write a dummy section header.
86
87 string_file = StringIO.StringIO()
88 # Write the dummy section header to the string file.
89 string_file.write('[dummysection]\n')
90 # Write the entire contents of the properties file to the string file.
91 string_file.write(open(prop_file_path).read())
92 # Rewind the string file.
93 string_file.seek(0, os.SEEK_SET)
94
95 # Create the ConfigParser object.
96 config_parser = ConfigParser.ConfigParser()
97 # Make the property names case-sensitive.
98 config_parser.optionxform = str
99 # Read the properties from the string file.
100 config_parser.readfp(string_file)
101 # Return the properties as a dictionary.
102 return dict(config_parser.items('dummysection'))
103
104###############################################################################
Michael Walsh7423c012016-10-04 10:27:21 -0500105
106
107###############################################################################
108def return_path_list():
109
110 r"""
111 This function will split the PATH environment variable into a PATH_LIST
112 and return it. Each element in the list will be normalized and have a
113 trailing slash added.
114 """
115
116 PATH_LIST = os.environ['PATH'].split(":")
117 PATH_LIST = [os.path.normpath(path) + os.sep for path in PATH_LIST]
118
119 return PATH_LIST
120
121###############################################################################
Michael Walsh7db77942017-01-10 11:37:06 -0600122
123
124###############################################################################
125def quote_bash_parm(parm):
126
127 r"""
128 Return the bash command line parm with single quotes if they are needed.
129
130 Description of arguments:
131 parm The string to be quoted.
132 """
133
134 # If any of these characters are found in the parm string, then the
135 # string should be quoted. This list is by no means complete and should
136 # be expanded as needed by the developer of this function.
137 bash_special_chars = set(' $')
138
139 if any((char in bash_special_chars) for char in parm):
140 return "'" + parm + "'"
141
142 return parm
143
144###############################################################################