0

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.

Mark
  • 2,137
  • 4
  • 27
  • 42

2 Answers2

0

Not really the best way but here's an idea. You'll have to save the path of the config file somewhere.

You could even save the path of the config file inside the config file...

something like that:

config_file=%(here)s/development.ini

When this file will be loaded there will be an entry for it in config.registry.settings. From there you have two choices. You either write a file just like pid does with its pid but with your current setting file. Or you can create an api call to query the setting from the live application. to be honest, i'd write it in a file. Querying the setting from the app seems not so good.

Then you'd have to open that file and read the path to the file.ini and then load it as a config file.

Or you can try something like that:

ps -ax | egrep "`cat pyramid.pid`.*[s]tart" | cut -d" " -f14

While running from python you could omit "cut" and cut it yourself from python to get the config ini file.

in other words, you're getting the command line used for the pid contained inside pyramid.pid this file will only be created when in daemon mode. When you have the command line, you can get the config file an so on... The downside is that it will only work with pserve or anything that actually save the PID and use the config as argument. if you're using uWSGI and such, it might not be possible to do something like that.

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
  • the script is starting a different process or it is attaching itself to the current server? – Loïc Faure-Lacroix Aug 22 '12 at 08:47
  • I don't know how to put it. It's just a separate script that does not have access to the request or config parameter. This script is actually a wrapper on top of Tummy's memcached python wrapper. – Mark Aug 24 '12 at 08:11
0

Within your event (NewRequest) you can retrieve the registry, and thus all settings inside of it.

event.request.registry

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 the ApplicationCreated event if you wanted to set up your memcache right after configuration is complete but before any requests have been made.

Community
  • 1
  • 1
X-Istence
  • 16,324
  • 6
  • 57
  • 74