blob: 167a3d951546671dde728518ee60ed126ce43929 [file] [log] [blame]
Michael Walshde791732016-09-06 14:25:24 -05001#!/usr/bin/env python
2
3# This module provides many valuable functions such as my_parm_file.
4
5# sys and os are needed to get the program dir path and program name.
6import sys
7import os
8import ConfigParser
9import StringIO
10
11
12###############################################################################
13def my_parm_file(prop_file_path):
14
15 r"""
16 Read a properties file, put the keys/values into a dictionary and return
17 the dictionary.
18
19 The properties file must have the following format:
20 var_name<= or :>var_value
21 Comment lines (those beginning with a "#") and blank lines are allowed and
22 will be ignored. Leading and trailing single or double quotes will be
23 stripped from the value. E.g.
24 var1="This one"
25 Quotes are stripped so the resulting value for var1 is:
26 This one
27
28 Description of arguments:
29 prop_file_path The caller should pass the path to the properties file.
30 """
31
32 # ConfigParser expects at least one section header in the file (or you
33 # get ConfigParser.MissingSectionHeaderError). Properties files don't
34 # need those so I'll write a dummy section header.
35
36 string_file = StringIO.StringIO()
37 # Write the dummy section header to the string file.
38 string_file.write('[dummysection]\n')
39 # Write the entire contents of the properties file to the string file.
40 string_file.write(open(prop_file_path).read())
41 # Rewind the string file.
42 string_file.seek(0, os.SEEK_SET)
43
44 # Create the ConfigParser object.
45 config_parser = ConfigParser.ConfigParser()
46 # Make the property names case-sensitive.
47 config_parser.optionxform = str
48 # Read the properties from the string file.
49 config_parser.readfp(string_file)
50 # Return the properties as a dictionary.
51 return dict(config_parser.items('dummysection'))
52
53###############################################################################