Changeset 1060
- Timestamp:
- 04/22/06 17:46:48
- Files:
-
- trunk/cherrypy/tools.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/tools.py
r1058 r1060 8 8 docstring. 9 9 10 Function decorators: if the tool exposes a "wrap" callable, that 11 is assumed to be a decorator for use in wrapping individual 12 CherryPy page handlers (methods on the CherryPy tree). 10 Function decorators: if the tool exposes a "wrap" callable, that is 11 assumed to be a decorator for use in wrapping individual CherryPy 12 page handlers (methods on the CherryPy tree). The tool may choose 13 not to call the page handler at all, if the response has already 14 been populated. 13 15 14 16 CherryPy hooks: "hooks" are points in the CherryPy request-handling … … 17 19 this. If a tool exposes a "setup" callable, this will be called 18 20 once per Request (if the feature is enabled via config). 19 20 WSGI middleware:21 22 21 23 22 Tools may be implemented as any object with a namespace. The builtins … … 37 36 38 37 def __call__(self, *args, **kwargs): 39 self.callable(*args, **kwargs)38 return self.callable(*args, **kwargs) 40 39 41 40 def wrap(self, *args, **kwargs): 42 """Make a decorator for this tool.""" 41 """Make a decorator for this tool. 42 43 For example: 44 45 @tools.decode.wrap(encoding='chinese') 46 def mandarin(self, name): 47 return "%s, ni hao shi jie" % name 48 mandarin.exposed = True 49 """ 43 50 def deco(f): 44 51 def wrapper(*a, **kw): 45 print args, kwargs46 52 handled = self.callable(*args, **kwargs) 47 53 return f(*a, **kw) 48 missing = object()49 exposed = getattr(f, "exposed", missing)50 if exposed is not missing:51 wrapper.exposed = exposed52 54 return wrapper 53 55 return deco 54 56 55 57 def setup(self, conf): 56 """Hook this tool into cherrypy.request using the given conf.""" 58 """Hook this tool into cherrypy.request using the given conf. 59 60 The standard CherryPy request object will automatically call this 61 method when the tool is "turned on" in config. 62 """ 57 63 cherrypy.request.hooks.attach(self.point, self.callable, conf) 58 64 … … 69 75 self.callable = callable 70 76 71 def __call__(self, *args, **kwargs): 72 self.callable(*args, **kwargs) 77 def handler(self, *args, **kwargs): 78 """Use this tool as a CherryPy page handler. 79 80 For example: 81 cherrypy.root.nav = tools.staticdir.handler( 82 section="/nav", dir="nav", root=absDir) 83 """ 84 def wrapper(*a, **kw): 85 handled = self.callable(*args, **kwargs) 86 if not handled: 87 raise cherrypy.NotFound() 88 return cherrypy.response.body 89 wrapper.exposed = True 90 return wrapper 73 91 74 92 def wrap(self, *args, **kwargs): 75 """Make a decorator for this tool.""" 93 """Make a decorator for this tool. 94 95 For example: 96 97 @tools.staticdir.wrap(section="/slides", dir="styles", root=absDir) 98 def slides(self, slide=None, style=None): 99 return "No such file" 100 slides.exposed = True 101 """ 76 102 def deco(f): 77 103 def wrapper(*a, **kw): 78 print args, kwargs79 104 handled = self.callable(*args, **kwargs) 80 if not handled: 105 if handled: 106 return cherrypy.response.body 107 else: 81 108 return f(*a, **kw) 82 missing = object()83 exposed = getattr(f, "exposed", missing)84 if exposed is not missing:85 wrapper.exposed = exposed86 109 return wrapper 87 110 return deco 88 111 89 112 def setup(self, conf): 90 """Hook this tool into cherrypy.request using the given conf.""" 113 """Hook this tool into cherrypy.request using the given conf. 114 115 The standard CherryPy request object will automatically call this 116 method when the tool is "turned on" in config. 117 """ 91 118 def wrapper(): 92 119 if self.callable(**conf): … … 98 125 # Builtin tools # 99 126 100 from cherrypy.lib import cptools, encodings, static 101 127 from cherrypy.lib import cptools 102 128 base_url = Tool('before_request_body', cptools.base_url) 103 129 response_headers = Tool('before_finalize', cptools.response_headers) 104 130 virtual_host = Tool('before_request_body', cptools.virtual_host) 131 del cptools 105 132 133 from cherrypy.lib import encodings 106 134 decode = Tool('before_main', encodings.decode) 107 135 encode = Tool('before_finalize', encodings.encode) 108 136 gzip = Tool('before_finalize', encodings.gzip) 137 del encodings 109 138 139 from cherrypy.lib import static 110 140 class _StaticDirTool(MainTool): 111 141 def setup(self, conf): 112 142 """Hook this tool into cherrypy.request using the given conf.""" 113 section = cherrypy.config.get('tools.staticdir.dir', return_section=True)114 conf['section'] = section143 conf['section'] = cherrypy.config.get('tools.staticdir.dir', 144 return_section=True) 115 145 def wrapper(): 116 146 if self.callable(**conf): … … 118 148 # Don't pass conf (or our wrapper will get wrapped!) 119 149 cherrypy.request.hooks.attach(self.point, wrapper) 120 121 150 staticdir = _StaticDirTool(static.get_dir) 122 151 staticfile = MainTool(static.get_file) 152 del static 123 153 124 154 # These modules are themselves Tools

