1#!/usr/bin/env python 2 3r""" 4This module provides many valuable functions such as my_parm_file. 5""" 6 7# sys and os are needed to get the program dir path and program name. 8import sys 9import os 10import ConfigParser 11import StringIO 12 13import gen_print as gp 14 15 16############################################################################### 17def 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: 33 prop_file_path The caller should pass the path to the 34 properties file. 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############################################################################### 59 60 61############################################################################### 62def 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############################################################################### 76