Created
May 27, 2011 18:26
-
-
Save cgibson/995851 to your computer and use it in GitHub Desktop.
Property loading function in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prop file load | |
# Based off of http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch34s04.html | |
def load_prop(filename): | |
try: | |
f = file( filename, 'rU' ) | |
except: | |
print "Error: no such property file %s" % filename | |
sys.exit() | |
d = dict() | |
for propline in f: | |
pdef = propline.strip() | |
if len(pdef) == 0: | |
continue | |
if pdef[0] in ( '!', '#' ): | |
continue | |
punctuation = [ pdef.find(c) for c in ':= ' ] + [ len(pdef) ] | |
found = min( [ pos for pos in punctuation if pos != -1 ] ) | |
name = pdef[:found].rstrip() | |
value = pdef[found:].lstrip(":= ").rstrip() | |
d[name] = value | |
f.close() | |
return d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment