I have a python script that serves as an additional wrapper around tummy’s memcached python wrapper. I have something like this:
class CacheIt(object):
def __init__(self, host=<hostname>, port=11211, **kwargs):
...
I want to be able to retrieve the memcached hostname from my ini file, but I don’t want to specify which file because it may be taken from development.ini or production.ini or test.ini. I need the “finding” of the current ini file to be dynamic, meaning the application should know what the current ini file is and hence retrieve the hostname from the settings.
I can’t use thread locals get_current_request and in turn to retrieve the settings file because the return value is None at application startup time (I attach an instance of my class to a NewRequest event).
This:
from paste.deploy.loadwsgi import appconfig
config = appconfig('config:development.ini', 'myapp', relative_to='.')
Still, I need to know the path first before I can use this. That means if I use this, I need to change the value of development.ini to production.ini everytime I deploy and to test.ini if I test. Surely there has to be a better way to do this.
Within your event (
NewRequest) you can retrieve the registry, and thus all settings inside of it.will contain the full registry.
The other suggestion I have is that you create an instance of your class and do all configuration in
main()when the rest of the configuration is done. This is how the SQLAlchemy setup works as well. For a simple example, take a look at the CouchDB and Pyramid integration. The same answer was provided in Pyramid and .ini configuration. You could also attached to theApplicationCreatedevent if you wanted to set up your memcache right after configuration is complete but before any requests have been made.