Hi, am very newbie in Python, but as part of a project i need to load
configuration -a settings.py file in the package dir- of my apps
recursively, something like this:
settings.load_c onfig("project. test.app")
settings.loa d_config("proje ct.test.*")
settings.loa d_config("proje ct.test")
settings.loa d_config("*")
this allows me to load them as:
settings.projec t.CONFIG_PARAME TER_1 # project configuration
settings.projec t.test.CONFIG_P ARAMETER_1 # sub project
and so on.
This it's what i've done,
class Settings:
def __getattr__( self, attr):
return self.__dict__['flags'][attr]
def __setattr__(sel f, attr, value):
self.__dict__['flags'][attr]=value
def __init__(self, package = None, parent = None):
self.__package = package
self.__parent = None
self.__dict__['flags']={}
def get_parent ( self):
return self.__parent
def create_config_s tructure( self, pkg, parent = None ):
# ... assuming no error and all that
if pkg.count(".") 0:
if parent is None:
if not self.__dict__['flags'].has_key(pkg[:pkg.find(".")]):
father=self.__d ict__['flags'][pkg]=Settings( \
pkg[:pkg.find(".")],self)
else:
father = parent
else:
if not parent.__dict__['flags'].has_key(pkg[:pkg.find(".")]):
father=parent._ _dict__['flags'][pkg[:pkg.find(".")]]= \
Settings(pkg[:pkg.find(".")], parent)
else:
father = parent
self.create_con fig_structure( pkg [pkg.find(".")+1 :],father)
else:
if not parent.__dict__['flags'].has_key:
parent.__dict__['flags'][pkg]=Settings(pkg,p arent)
return parent.__dict__['flags'][pkg]
def load_config ( self, pkg= None ):
config_module_o bject = self.create_con fig_structure( pkg )
# the loading configuration part
try:
if pkg is not None:
mod = __import__( pkg + ".settings" , {},{},[''])
else:
mod = __import__( "settings", {},{},[''])
except:
raise ImportError("Se ttings not found")
data={}
for setting in dir(mod):
if setting == setting.upper() :
data[setting]=getattr(mod, setting)
for key in data:
if pkg is not None:
setattr( config_module_o bject.__dict__['flags'], key, data[key])
else:
setattr(self.__ dict__['flags'], key, data[key])
Any idea it's welcome
---------------------------------------
Red Telematica de Salud - Cuba
CNICM - Infomed
configuration -a settings.py file in the package dir- of my apps
recursively, something like this:
settings.load_c onfig("project. test.app")
settings.loa d_config("proje ct.test.*")
settings.loa d_config("proje ct.test")
settings.loa d_config("*")
this allows me to load them as:
settings.projec t.CONFIG_PARAME TER_1 # project configuration
settings.projec t.test.CONFIG_P ARAMETER_1 # sub project
and so on.
This it's what i've done,
class Settings:
def __getattr__( self, attr):
return self.__dict__['flags'][attr]
def __setattr__(sel f, attr, value):
self.__dict__['flags'][attr]=value
def __init__(self, package = None, parent = None):
self.__package = package
self.__parent = None
self.__dict__['flags']={}
def get_parent ( self):
return self.__parent
def create_config_s tructure( self, pkg, parent = None ):
# ... assuming no error and all that
if pkg.count(".") 0:
if parent is None:
if not self.__dict__['flags'].has_key(pkg[:pkg.find(".")]):
father=self.__d ict__['flags'][pkg]=Settings( \
pkg[:pkg.find(".")],self)
else:
father = parent
else:
if not parent.__dict__['flags'].has_key(pkg[:pkg.find(".")]):
father=parent._ _dict__['flags'][pkg[:pkg.find(".")]]= \
Settings(pkg[:pkg.find(".")], parent)
else:
father = parent
self.create_con fig_structure( pkg [pkg.find(".")+1 :],father)
else:
if not parent.__dict__['flags'].has_key:
parent.__dict__['flags'][pkg]=Settings(pkg,p arent)
return parent.__dict__['flags'][pkg]
def load_config ( self, pkg= None ):
config_module_o bject = self.create_con fig_structure( pkg )
# the loading configuration part
try:
if pkg is not None:
mod = __import__( pkg + ".settings" , {},{},[''])
else:
mod = __import__( "settings", {},{},[''])
except:
raise ImportError("Se ttings not found")
data={}
for setting in dir(mod):
if setting == setting.upper() :
data[setting]=getattr(mod, setting)
for key in data:
if pkg is not None:
setattr( config_module_o bject.__dict__['flags'], key, data[key])
else:
setattr(self.__ dict__['flags'], key, data[key])
Any idea it's welcome
---------------------------------------
Red Telematica de Salud - Cuba
CNICM - Infomed
Comment