Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

edit

  • Loading branch information...
commit abcf273059c0e548b962383b94d48fb7793e89bb 1 parent 6218709
anonymous authored anandology committed
Showing with 40 additions and 0 deletions.
  1. +40 −0 MetaClassAutoURLS.md
40 MetaClassAutoURLS.md
View
@@ -5,3 +5,43 @@ title: MetaClassAutoURLS
# MetaClassAutoURLS
+web.py maps URLs regexes to classes via a list like so:
+
+ class Default(object):
+ ''' Action for / ''' def GET(self):
+ pass
+
+ class Login(object):
+ ''' Action for /login ''' def GET(self):
+ pass
+
+ def POST(self):
+ pass
+
+ urls = ["/", "Default", "/login", "Login"]
+
+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:
+
+ urls = [ ]
+
+ class ActionMetaClass(type):
+ def __init__(klass, name, bases, attrs):
+ urls.append(attrs["url"])
+ urls.append("%s.%s" % (klass.__module__, name))
+
+ class Default(object):
+ __metaclass__ = ActionMetaClass
+ url = "/"
+ def GET(self):
+ pass
+
+ class Login(object):
+ __metaclass__ = ActionMetaClass
+ url = "/login"
+ def GET(self):
+ pass
+
+ def POST(self):
+ pass
+
+Of course, this is a simple example that leaves a lot to be desired (handling attrs lacking a urls key, for instance), but it should get you on your way.
Please sign in to comment.
Something went wrong with that request. Please try again.