Add ability to load PasteDeploy config file from package/egg?

22 views
Skip to first unread message

Wyatt Baldwin

unread,
Jul 30, 2011, 1:08:47 PM7/30/11
to paste...@googlegroups.com
I would like to be able to load a PasteDeploy config file from within a Python package. The easiest way I could see to accomplish this was to add a new loader. Here's a simple example that illustrates what I want to do:

# base.ini in package MyFramework
[app:main]
use = egg:SomeEgg
a = 1
b = 2

# app.ini
[app:main]
use = configfrompackage:MyFramework:base.ini
b = 4

I whipped a rough patch that accomplishes this. If there's any interest in adding something like this, I'll polish this up and submit a pull request with at least one new test.

diff -r 9921ad7ac983 paste/deploy/loadwsgi.py
--- a/paste/deploy/loadwsgi.py    Wed May 25 00:34:47 2011 +0300
+++ b/paste/deploy/loadwsgi.py    Sat Jul 30 09:42:40 2011 -0700
@@ -322,6 +322,15 @@
 _loaders['config'] = _loadconfig
 
 
+def _loadconfigfrompackage(object_type, uri, path, name, relative_to,
+                           global_conf):
+    package, rel_path = path.split(':', 1)
+    path = pkg_resources.resource_filename(package, rel_path)
+    return _loadconfig(object_type, uri, path, name, relative_to, global_conf)
+
+_loaders['configfrompackage'] = _loadconfigfrompackage
+
+
 def _loadegg(object_type, uri, spec, name, relative_to,
              global_conf):
     loader = EggLoader(spec)

Mike Orr

unread,
Jul 31, 2011, 2:08:32 PM7/31/11
to paste...@googlegroups.com

I use paste.deploy.loadwsgi.loadapp fairly often in my utilities, and
I wish it had a better name and documentation. Sometimes I also have
to mimic the way Paste parses a config file, and that requires using
NicerConfigParser and hand-adding the '__here__' key and stuff, which
again required trial-and-error because it's undocumented. It that what
this patch does, or what does it do? What can you do with a 'loaded'
config file?

--
Mike Orr <slugg...@gmail.com>

Wyatt Baldwin

unread,
Jul 31, 2011, 9:52:16 PM7/31/11
to paste...@googlegroups.com
It lets you specify a config file relative to a package. There are two things this might be useful for. The first is config inheritance, which is what I need it for. At work, I've developed a framework on top of Pyramid. It contains some config files that applications based on it inherit. The problem is that I have to know the full file system path to the config files in the framework package.

You could also use this to load an app that's specified in a config file in a package:

    app = loadapp('configfrompackage:MyFramework:config/base.ini')
    serve(app)

 

Reply all
Reply to author
Forward
0 new messages