-
Notifications
You must be signed in to change notification settings - Fork 0
jojosati/classroute
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
classroute
is a "route" decorator replacement for bottle,
with ability to wrap whole class definition
or late binding to a class instance or specfic instance method.
can safely use @classroute as replacement of @route
classroute can use with function like original route.
classroute can use with class definition.
classroute can use with instance method (late binding).
classroute can use with class instance (late binding).
classroute use bottle's yieldroutes function to generate path,
with special handling to skip first argument of class instance in the method.
classroute can auto bind to directory path for function/method named 'index'.
classroute support "PathItem" variable "{$root}","{$class}","{$name}",
"PathItem" variable can use chained property like "{$root.@lower} "
---------------------
classroute example
----------------------
from classroute import *
debug() # enable verbose route path binding
class A(object) :
def __init__(self,name='World') :
self.name = name
def hello(self,name=None) :
yield 'Hello '
yield name or self.name
# bind to "/hello","/hello/<name>"
@classroute
class A1(A) :
pass
# bind to "/a2/hello","/a2/hello/<name>"
@classroute('{$class}',['POST','GET'])
class A2(A) :
pass
# bind to "/a/hello","/a/hello/<name>","/aa/hello","/aa/<name>"
# same as @classroute(['/aa','/a')
@classroute('/a')
@classroute('/aa')
class A3(A):
pass
# bind different root with different instance of same class
# bind to "/john/hello","/john/hello/<name>"
classroute('john')(A)('john')
# bind to "/jane/hello","/jane/hello/<name>"
classroute('jane')(A)('jane')
#with helper function "mroute" and "skiproute"
#fine tune for individual method in the class is possible.
# bind to "/B/hello","/B/hello/<name>","/B/say/hello","/B/say/hello/<name>"
@classroute('{$class}')
class B(A):
@mroute('{$root}/say/hello',['GET','POST'])
def say__hi(self,name=None) :
redirect('{$root}/hello/{0}',name or self.name,**self.__routeitems__)
@skiproute
def myfunc(self,name='') :
yield 'myfunc'
if name :
yield ' ('+name+')'
# or use as route replacement, with special meaning of 'index'
# bind to "/","/index"
@classroute
def index() :
redirect('/hello')
# create instance, to bind all route definition
A1()
A2()
A3()
b = B()
# support for late binding to individual instance's method
# bind to "/a-bottle"
classroute('{$class}-bottle')(A('Bottle').hello)
# force late binding to @skiproute method
# bind to "/my","/my/:name"
classroute('/my/**')(b.myfunc)
# also support for late binding to instance
# bind to "/bottle/..."
classroute('/bottle')(A('Bottle'))
#start server
run()
----------
PathItem
-----------
a str represent for path variable {$root},{$class},{$name}
with support property-like access to all method of str via
".@methodname" or ".@methodname(argument)" .
example :
>>> '{0.@title}'.format(PathItem('hello john'))
'Helo John'
>>> '{0.@split(" ")[-1]}'.format(PathItem('hello john'))
'john'
and more path related method
.@path - tranform to path format "/xxxx".
.@path(True) - path transform with normalize "__" to "/".
.@items[n] - same as split("/") but element in items is PathItem.
.@value - original value
all properties are chainable
example:
>>> "{0.@path(True).@strip('/').@items[0].@title}".format(PathItem('//abc__def__'))
'Abc'
About
classroute decorator, a bottle's "route" replacement.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published