Robert Brewer avatar Robert Brewer committed db44da4

Made cherrypy.request and .response more sef-documenting by populating their attribute dicts outside a request.

Comments (0)

Files changed (3)

 # in a thread-safe way.
 serving = _local()
 
-class _ThreadLocalProxy:
+# Bind dummy instances of default request/response
+# (in the main thread only!) to help introspection.
+serving.request = _cprequest.Request("localhost", "11111", "localhost")
+serving.response = _cprequest.Response()
+
+
+class _ThreadLocalProxy(object):
+    
+    __slots__ = ['__attrname__', '__dict__']
     
     def __init__(self, attrname):
-        self.__dict__["__attrname__"] = attrname
+        self.__attrname__ = attrname
     
     def __getattr__(self, name):
-        try:
-            childobject = getattr(serving, self.__attrname__)
-        except AttributeError:
-            raise AttributeError("cherrypy.%s has no properties outside of "
-                                 "an HTTP request." % self.__attrname__)
+        childobject = getattr(serving, self.__attrname__)
         return getattr(childobject, name)
     
     def __setattr__(self, name, value):
-        try:
+        if name == "__attrname__":
+            object.__setattr__(self, "__attrname__", value)
+        else:
             childobject = getattr(serving, self.__attrname__)
-        except AttributeError:
-            raise AttributeError("cherrypy.%s has no properties outside of "
-                                 "an HTTP request." % self.__attrname__)
-        setattr(childobject, name, value)
+            setattr(childobject, name, value)
     
     def __delattr__(self, name):
-        try:
-            childobject = getattr(serving, self.__attrname__)
-        except AttributeError:
-            raise AttributeError("cherrypy.%s has no properties outside of "
-                                 "an HTTP request." % self.__attrname__)
+        childobject = getattr(serving, self.__attrname__)
         delattr(childobject, name)
+    
+    def _get_dict(self):
+        childobject = getattr(serving, self.__attrname__)
+        d = childobject.__class__.__dict__.copy()
+        d.update(childobject.__dict__)
+        return d
+    __dict__ = property(_get_dict)
+
 
 # Create request and response object (the same objects will be used
 #   throughout the entire life of the webserver, but will redirect
 class Request(object):
     """An HTTP request."""
     
+    # Conversation attributes
+    remote_addr = "localhost"
+    remote_port = 1111
+    remote_host = "localhost"
+    scheme = "http"
+    base = ""
+    
+    # Request-Line attributes
+    request_line = ""
+    method = "GET"
+    path = ""
+    query_string = ""
+    protocol = ""
+    params = {}
+    version = http.version_from_http("HTTP/1.1")
+    
+    # Message attributes
+    header_list = []
+    headers = http.HeaderMap()
+    simple_cookie = Cookie.SimpleCookie()
+    rfile = None
+    process_request_body = True
+    body = None
+    
+    # Dispatch attributes
+    script_name = ""
+    path_info = "/"
+    app = None
+    handler = None
+    toolmap = {}
+    config = None
+    error_response = cherrypy.HTTPError(500).set_response
+    hookpoints = ['on_start_resource', 'before_request_body',
+                  'before_main', 'before_finalize',
+                  'on_end_resource', 'on_end_request',
+                  'before_error_response', 'after_error_response']
+    
     def __init__(self, remote_addr, remote_port, remote_host, scheme="http"):
         """Populate a new Request object.
         
         """Generate a response for the resource at self.path_info."""
         try:
             try:
-                pts = ['on_start_resource', 'before_request_body',
-                       'before_main', 'before_finalize',
-                       'on_end_resource', 'on_end_request',
-                       'before_error_response', 'after_error_response']
-                self.hooks = HookMap(pts)
+                self.hooks = HookMap(self.hookpoints)
                 self.hooks.failsafe = ['on_start_resource', 'on_end_resource',
                                        'on_end_request']
                 
 class Response(object):
     """An HTTP Response."""
     
+    status = None
+    header_list = None
+    headers = http.HeaderMap()
+    simple_cookie = Cookie.SimpleCookie()
     body = Body()
     
     def __init__(self):
     
     try:
         conf = cherrypy.request.config
+        if conf is None:
+            conf = globalconf
     except AttributeError:
         # There's no request, so just use globalconf.
         conf = globalconf
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.