wx.Config encapsulation improved with __getattribute__() 
January 27th, 2007, 11:12 AM
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| |
Still have to prepend "default" when assigning variables to this class, but it now allows val = inst.vaule: - """Encapuslate a Default Values Object and Config File"""
-
-
from inspect import getmembers
-
import wx
-
-
class DefaultValueHolder(object):
-
"""Intended for use with wxConfig (or maybe _winreg) to set up and/or get
-
registry key names and values. Name attrs as default*. "default"
-
will be stripped of when reading and writing to the config file.
-
You may not use the name varDict as one of the variable names."""
-
-
def __init__(self, appName, grpName):
-
"""Open or create the application key"""
-
self.appName = appName
-
self.grpName = grpName # if the key or group doesn't exit, it will be created
-
self.config = wx.Config(appName) # Open the file (HKCU in windows registry)
-
-
def __getattribute__(self, name):
-
try:
-
return object.__getattribute__(self, "default%s" %name)
-
except AttributeError:
-
return object.__getattribute__(self, name)
-
-
def GetVariables(self):
-
return [{"name":var[0][7:], "value":var[1], "type":type(var[1])}
-
for var in getmembers(self) if var[0].startswith('default')]
-
-
def SetVariables(self, varDict={}, **kwargs):
-
kwargs.update(varDict)
-
for name, value in kwargs.items():
-
setattr(self, "default%s" %name, value)
-
-
def InitFromConfig(self):
-
config = self.config
-
group = self.grpName
-
-
if not config.Exists(group):
-
self.WriteRegistryGroup(group)
-
-
else:
-
config.SetPath(group)
-
for var in self.GetVariables():
-
name = var['name']
-
if config.Exists(name):
-
value = self.ReadRegistry(name, var['type'])
-
self.SetVariables({name:value})
-
else:
-
self.WriteRegistry(name, var['value'], var['type'])
-
config.SetPath("")
-
-
def WriteRegistryGroup(self, group):
-
self.config.SetPath(group)
-
for var in self.GetVariables():
-
self.WriteRegistry(var['name'], var['value'], var['type'])
-
self.config.SetPath("")
-
-
def UpdateConfig(self):
-
self.WriteRegistryGroup(self.grpName)
-
-
def ReadRegistry(self, name, type):
-
value = None
-
if type == str:
-
value = self.config.Read(name)
-
elif type in (int, long):
-
value = self.config.ReadInt(name)
-
elif type == float:
-
value = self.config.ReadFloat(name)
-
return value
-
-
def WriteRegistry(self, name, value, type):
-
if type == str:
-
self.config.Write(name, value)
-
elif type in (int, long):
-
self.config.WriteInt(name, value)
-
elif type == float:
-
self.config.WriteFloat(name, value)
-
-
-
if __name__ == "__main__":
-
## class SetSelfFromDefaults(object):
-
## def __init__(self):
-
## self.test = test = DefaultValueHolder("HETAP Pro 2.00", "Database")
-
## test.SetVariables(UserName="peter", Password="pan", ServerName="MyServer", database="")
-
## test.InitFromConfig()
-
##
-
## self.SetupLoginVars(test)
-
##
-
## def SetupLoginVars(self, defaultHolder):
-
## """Cool tricks, use somewhere, someday."""
-
## for var in defaultHolder.GetVariables():
-
## execStr = "self.%s = %s" %(var['name'], (repr(var['value']), var['value'])[var['type'] != str])
-
## exec(execStr) in locals()
-
##
-
## testClass = SetSelfFromDefaults()
-
## print testClass.UserName
-
-
test = DefaultValueHolder("HETAP Pro 2.00", "Database")
-
test.SetVariables(UserName = "peter", Password = "pan", ServerName = "MyServer", database="")
-
test.InitFromConfig()
-
print test.UserName
-
test.defaultvar1=77
-
print test.GetVariables()
-
#### this also works:
-
## test.defaultUserName = "joe"
| 
January 31st, 2007, 10:58 PM
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | re: wx.Config encapsulation improved with __getattribute__()
I cleaned this up too: -
-
def GetVariables(self):
-
return [{"name":name[7:], "value":value, "type":type(value)}
-
for name, value in getmembers(self) if name.startswith('default')]
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|