[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] setting user variables in a config or preferences file
Hi guys.
I just coded up a handy utility to save user-specific preferences (things
like keyboard controls, username, difficulty settings, etc). I thought
I'd share it with the list and ask if anyone could test it on other
platforms (I've tested it out on linux)
To test it, run `python userprofile.py -w`. That should put a preferences
folder and file in your $HOME/.glarf/ directory.
Any interest in including something like this into Pygame itself?
Shandy
"""
A way to get access to a userProfile that can be stored for future invocations
of the program.
Original Authour: Shandy Brown
License: Public Domain
"""
import os
import ConfigParser
projectName = "glarf"
section = 'global'
userProfile = {}
#------------------------------------------------------------------------------
# Functions to write preferences file
#------------------------------------------------------------------------------
def Dict2ConfigParser( theDict, theParser, sectionName, ignoreList ):
"""Recursive function to convert a dict into a ConfigParser object"""
theParser.add_section( section )
for key in theDict.keys():
if key in ignoreList:
continue
if type(theDict[key]) == str:
theParser.set( sectionName, key, theDict[key] )
elif type(theDict[key]) == dict:
Dict2ConfigParser( theDict[key], theParser,
key, ignoreList )
else:
raise "Dict contains something other than strings"
def WriteProfile( userProfile, section ):
"""function to write the preferences to a ConfigParser-compatible file
"""
#TODO: catch exceptions (perhaps outside)
#don't write these locations. They're decided when the userprofile
#module is loaded
ignores = 'homeDir', 'prefDir', 'prefFile'
parser = ConfigParser.SafeConfigParser()
Dict2ConfigParser( userProfile, parser, section, ignores )
if not os.path.isdir( userProfile['prefDir'] ):
os.mkdir( userProfile['prefDir'], 0755 )
fp = file( userProfile['prefFile'], 'w' )
try:
parser.write( fp )
finally:
fp.close()
#------------------------------------------------------------------------------
if os.path.exists( os.path.expanduser('~') ):
userProfile['homeDir'] = os.path.expanduser('~')
elif os.path.exists( os.getenv( 'HOME' ) ):
userProfile['homeDir'] = os.getenv( 'HOME' )
elif os.path.exists( os.getenv( 'USERPROFILE' ) ):
userProfile['homeDir'] = os.getenv( 'USERPROFILE' )
else:
userProfile['homeDir'] = os.getcwd()
userProfile['prefDir'] = os.path.join(userProfile['homeDir'], '.'+ projectName)
userProfile['prefFile'] = os.path.join(userProfile['prefDir'], 'preferences' )
parser = ConfigParser.SafeConfigParser()
parser.read( userProfile['prefFile'] )
try:
userProfile['username'] = parser.get( section, 'username' )
except:
try:
userProfile['username'] = os.getenv('USER')
except:
try:
userProfile['username'] = os.getenv('USERNAME')
except:
userProfile['username'] = 'anonymous'
#------------------------------------------------------------------------------
# Call it from the command line to test it
#------------------------------------------------------------------------------
if __name__ == "__main__":
import sys
from pprint import pprint
pprint( userProfile )
try:
if sys.argv[1] == '-w':
WriteProfile( userProfile, section )
except:
print 'use the -w flag to test writing'