#!/usr/bin/python ### ### A simple Python module to represent a property file. Property ### files can either be lists or name-value pairs. Either way, a # ### marks at the beginning of a line means it's a comment and will ### be ignored. For name-value pairs, they should be separated by ### a colon and optionally a space after the colon, such as: ### foo:bar or foo: bar. Simple lists will be returned as (you ### guessed it) lists, while name-value lists will be returned as ### dictionaries. ### ### Anthony R. Thompson - April 2003 ### Contact: put @ between art and sigilservices.com ### ### Copyright (C) 1998-2010 by the Free Software Foundation, Inc. ### ### This program is free software; you can redistribute it and/or ### modify it under the terms of the GNU General Public License ### as published by the Free Software Foundation; either version 2 ### of the License, or (at your option) any later version. ### ### This program is distributed in the hope that it will be useful, ### but WITHOUT ANY WARRANTY; without even the implied warranty of ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ### GNU General Public License for more details. ### ### You should have received a copy of the GNU General Public License ### along with this program; if not, write to the Free Software ### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA ### 02110-1301, USA. http://www.fsf.org/licensing/licenses/gpl.html ### import os, sys, string, re class PropertyFile: ### ### Initialize this class ### def __init__(self): pass ### ### Return the contents of the property file as a list ### def contents_as_list(self,path): contents = [] file = open(path,'r') for line in file.readlines(): line = line[:len(line)-1] # kill newline if (not re.search('\w', line)): continue # ignore blank lns if (re.search('^\s*#', line)): continue # ignore comments contents.append(line) file.close() return contents ### ### Return the contents of the property file as a dict ### def contents_as_dict(self,path): contents = {} file = open(path,'r') for line in file.readlines(): line = line[:len(line)-1] # kill newline if (not re.search('\w', line)): continue # ignore blank lns if (re.search('^\s*#', line)): continue # ignore comments vals = string.split(line, ':', 1) if len(vals) == 1: vals.append('') contents[vals[0]] = re.sub('^\s+', '', vals[1]) file.close() return contents if __name__ == '__main__': propfile = PropertyFile() # simple tests here print "Done."