This repository has been archived by the owner on Oct 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ExposeAs
86 lines (63 loc) · 2.17 KB
/
ExposeAs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
= Expose As =
Expose As is a decorator meant to replace the built in @cherrypy.expose. It provides a couple of extra features.
(updated for CherryPy 2.1 beta)
== Features ==
* Expose methods with unicode names, spaces, file extensions:
{{{
#!python
@expose(as=u'∞')
@expose(as='foo bar')
@expose(as='default.css')
}}}
(solves tickets #115 and #87 )
* Trailing Slashes
Remove trailing slashes in the URI by default. Add trailing slashes with slash=True.
{{{
#!python
@expose(slash=True)
}}}
== The Code ==
{{{
#!python
import cherrypy
from cherrypy import _cphttptools
from cherrypy.lib import httptools
import sys, types
def expose(as=None, slash=False):
def expose(func):
# remove/add trailing slashes
def wrapper(*args, **kwargs):
if len(cherrypy.request.path) > 1:
if not slash and cherrypy.request.path[-1] == "/":
httptools.redirect(cherrypy.request.path[:-1])
elif slash and cherrypy.request.path[-1] != "/":
httptools.redirect(cherrypy.request.path + "/")
else:
return func(*args, **kwargs)
else:
return func(*args, **kwargs)
wrapper.exposed = True
wrapper.func_name = func.func_name
# 'rename' the function
if as:
# work around the . to _ hack (#87)
name = as.replace(".", "_")
nameUnicode = name.encode("utf-8")
classDict = sys._getframe(1).f_locals
classDict[nameUnicode] = wrapper
# Firefox hack
try:
nameIso = name.encode("iso-8859-1")
classDict[nameIso] = wrapper
except UnicodeEncodeError:
pass
return None
else:
return wrapper
if isinstance(as, types.FunctionType) or isinstance(as, types.MethodType):
func = as
as = None
return expose(func)
else:
return expose
}}}