web.py wikihome of the web.py community MetaClassAutoURLSweb.py maps URLs regexes to classes via a list like so:
One of the disadvantages of this approach is that for large applications, the urls list can get very long and tedious to maintain. Things get worse during long refactorings/reorgnanizations because it's easy to typo a class or module name or just forget to update the urls list. After spending a couple hours debugging just this problem and then smacking myself on the forehead for missing such a simple mistake, I thought to myself, There's got to be a way to automate this! After some research, I found that metaclasses can do exactly what I want:
Of course, this is a simple example that leaves a lot to be desired (handling attrs lacking a url key, for instance), but it should get you on your way. AaronSw writes: That's a clever idea. One improvement might be to have the classes inherit from a class with the metaclass set, so you don't have that unsightly xunil writes: Yeah, I went w/ this approach in the end since I found that I wanted each action class to inherit some common functionality anyway. You can view the source for my incomplete site which uses a metaclass and decorators. May be it's better to do it with decorators? xunil writes: I originally tried to use decorators to do this since I was already familiar w/ them, but unless I'm missing something, decorators don't work on classes eg.
That won't work. The PEP for decorators describes it, but apparently it was never incorporated into the language (perhaps b/c metaclasses are available and do the same thing). I couldn't figure out how to get this to work with URL classes in separate files, so I did something a little different, based on this Python Cookbook recipe:
And then e.g. cgi_hello.py (in the same directory) would be:
Is this a terrible way to do it? xunil writes: This is not altogether a bad idea, but it again decouples the URL information from the class, making it a module-level tuple or list. You could combine your importing logic w/ my metaclass idea, actually, and achieve the same thing as me eg.
metaclass.py:
cgi_hello.py:
|
Powered by infogami, which uses web.py. |