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 | ############################################################################### |
| 17 | def my_parm_file(prop_file_path): |
| 18 | |
| 19 | r""" |
| 20 | Read a properties file, put the keys/values into a dictionary and return |
| 21 | the dictionary. |
| 22 | |
| 23 | The properties file must have the following format: |
| 24 | var_name<= or :>var_value |
| 25 | Comment lines (those beginning with a "#") and blank lines are allowed and |
| 26 | will be ignored. Leading and trailing single or double quotes will be |
| 27 | stripped from the value. E.g. |
| 28 | var1="This one" |
| 29 | Quotes are stripped so the resulting value for var1 is: |
| 30 | This one |
| 31 | |
| 32 | Description of arguments: |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 33 | prop_file_path The caller should pass the path to the |
| 34 | properties file. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 35 | """ |
| 36 | |
| 37 | # ConfigParser expects at least one section header in the file (or you |
| 38 | # get ConfigParser.MissingSectionHeaderError). Properties files don't |
| 39 | # need those so I'll write a dummy section header. |
| 40 | |
| 41 | string_file = StringIO.StringIO() |
| 42 | # Write the dummy section header to the string file. |
| 43 | string_file.write('[dummysection]\n') |
| 44 | # Write the entire contents of the properties file to the string file. |
| 45 | string_file.write(open(prop_file_path).read()) |
| 46 | # Rewind the string file. |
| 47 | string_file.seek(0, os.SEEK_SET) |
| 48 | |
| 49 | # Create the ConfigParser object. |
| 50 | config_parser = ConfigParser.ConfigParser() |
| 51 | # Make the property names case-sensitive. |
| 52 | config_parser.optionxform = str |
| 53 | # Read the properties from the string file. |
| 54 | config_parser.readfp(string_file) |
| 55 | # Return the properties as a dictionary. |
| 56 | return dict(config_parser.items('dummysection')) |
| 57 | |
| 58 | ############################################################################### |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 59 | |
| 60 | |
| 61 | ############################################################################### |
| 62 | def return_path_list(): |
| 63 | |
| 64 | r""" |
| 65 | This function will split the PATH environment variable into a PATH_LIST |
| 66 | and return it. Each element in the list will be normalized and have a |
| 67 | trailing slash added. |
| 68 | """ |
| 69 | |
| 70 | PATH_LIST = os.environ['PATH'].split(":") |
| 71 | PATH_LIST = [os.path.normpath(path) + os.sep for path in PATH_LIST] |
| 72 | |
| 73 | return PATH_LIST |
| 74 | |
| 75 | ############################################################################### |