| Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python | 
|  | 2 |  | 
| Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 3 | r""" | 
|  | 4 | This module provides many valuable functions such as my_parm_file. | 
|  | 5 | """ | 
| Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 6 |  | 
|  | 7 | # sys and os are needed to get the program dir path and program name. | 
|  | 8 | import sys | 
|  | 9 | import os | 
|  | 10 | import ConfigParser | 
|  | 11 | import StringIO | 
|  | 12 |  | 
| Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 13 | import gen_print as gp | 
|  | 14 |  | 
| Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 15 |  | 
|  | 16 | ############################################################################### | 
| Michael Walsh | 7db7794 | 2017-01-10 11:37:06 -0600 | [diff] [blame] | 17 | def 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 | ############################################################################### | 
|  | 30 | def 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 Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 63 | def 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 Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 79 | prop_file_path                  The caller should pass the path to the | 
|  | 80 | properties file. | 
| Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 81 | """ | 
|  | 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 Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 105 |  | 
|  | 106 |  | 
|  | 107 | ############################################################################### | 
|  | 108 | def 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 Walsh | 7db7794 | 2017-01-10 11:37:06 -0600 | [diff] [blame] | 122 |  | 
|  | 123 |  | 
|  | 124 | ############################################################################### | 
|  | 125 | def 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 | ############################################################################### |