By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

about recursive load

P: n/a
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_config("project.test.app")
settings.load_config("project.test.*")
settings.load_config("project.test")
settings.load_config("*")

this allows me to load them as:

settings.project.CONFIG_PARAMETER_1 # project configuration
settings.project.test.CONFIG_PARAMETER_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__(self, 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_structure( 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.__dict__['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_config_structure( pkg [pkg.find(".")+1:],father)
else:
if not parent.__dict__['flags'].has_key:
parent.__dict__['flags'][pkg]=Settings(pkg,parent)
return parent.__dict__['flags'][pkg]

def load_config ( self, pkg= None ):
config_module_object = self.create_config_structure( pkg )

# the loading configuration part
try:
if pkg is not None:
mod = __import__( pkg + ".settings", {},{},[''])
else:
mod = __import__( "settings", {},{},[''])
except:
raise ImportError("Settings 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_object.__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
Oct 30 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Oct 30, 2:22*pm, Michel Perez <ops...@infomed.sld.cuwrote:
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_config("project.test.app")
settings.load_config("project.test.*")
settings.load_config("project.test")
settings.load_config("*")

this allows me to load them as:

settings.project.CONFIG_PARAMETER_1 * * *# project configuration
settings.project.test.CONFIG_PARAMETER_1 # sub project

and so on.
I'm not sure if I follow your full requirements, but are you after
something like this?

class Settings(object):
def load_config(self, module_name):
module = __import__(module_name)
self.__dict__[module_name] = module
>>settings = Settings()
settings.load_config('project.test')
settings.load_config('project')
settings.project.CONFIG_PARAMETER_1
'project.config_parameter_1'
>>settings.project.test.CONFIG_PARAMETER_1
'project.test.config_parameter_1'

Oct 31 '08 #2

P: n/a
On Oct 31, 4:58*pm, alex23 <wuwe...@gmail.comwrote:
"project.test.app", which the load_config module imports and then
Sorry, that should be "load_config *method*".
Oct 31 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.