Commits

Anonymous committed b4548e5

updates on the proxy thing

  • Participants
  • Parent commits 5909870
  • Branches trunk
  • Tags 0.1

Comments (0)

Files changed (2)

docs/src/local.txt

     Manually clean up the data in the locals for this context.  Call this at
     the end of the request or use `make_middleware()`.
 
-`make_middleware(application)`
+`make_middleware(app)`
     Encapsulate the application and call `cleanup()` at the end of a request.
 
+`middleware()`
+    Like `make_middleware` but works as decorator.  The main difference is
+    that the returned object will have the original docstring etc.
+
+    .. sourcecode:: python
+
+        @manager.middleware
+        def application(environ, start_response):
+            ...
+
 `get_ident()`
     Return the context identifier the local objects use internally for this
     context.  You cannot override this method to change the behavior but use
     it to link other context local objects (such as SQLAlchemy's scoped
     sessions) to the werkzeug locals.
+
+
+Proxies
+=======
+
+Werkzeug can also create proxy objects for you.  A proxy object forwards nearly
+all operations to an object bound to an object on a local.  This is for example
+useful if you want a global request object:
+
+.. sourcecode:: python
+
+    from werkzeug import Local
+    local = Local()
+    request = local('request')
+    user = local('user')
+
+Whenever you access anything on user it will forward the operation to the object
+`local.user`.  This affects all operations except of any sort of assignment.
+
+Keep in mind that the repr is also forwarded so if you want to find out if you
+are dealing with a proxy you can do an isinstance check:
+
+.. sourcecode:: pycon
+
+    >>> from werkzeug import LocalProxy
+    >>> isinstance(request, LocalProxy)
+    True
+
+You can also create proxy objects by hand:
+
+.. sourcecode:: python
+
+    from werkzeug import Local, LocalProxy
+    local = Local()
+    request = LocalProxy(local, 'request')

werkzeug/local.py

     def __iter__(self):
         return self.__dict__['__storage'].iteritems()
 
+    def __call__(self, proxy):
+        """Create a proxy for a name."""
+        return LocalProxy(self, proxy)
+
     def __getattr__(self, name):
         self.__dict__['__lock'].acquire()
         try:
     """
 
     def __init__(self, locals=None):
-        self.locals = locals and list(locals) or []
+        if locals is None:
+            self.locals = []
+        else:
+            try:
+                self.locals = list(locals)
+            except TypeError:
+                self.locals = [locals]
 
     def get_ident(self):
         """Returns the current identifier for this context."""
                                    self.cleanup)
         return application
 
+    def middleware(self, func):
+        """
+        Like `make_middleware` but for decorating functions.  Example
+        usage::
+
+            @manager.middleware
+            def application(environ, start_response):
+                ...
+
+        The difference to `make_middleware` is that the function passed
+        will have all the arguments copied from the inner application
+        (name, docstring, module).
+        """
+        new_func = self.make_middleware(func)
+        try:
+            new_func.__name__ = func.__name__
+            new_func.__doc__ = func.__doc__
+            new_func.__module__ = func.__module__
+        except:
+            pass
+        return new_func
+
     def __repr__(self):
         return '<%s storages: %d>' % (
             self.__class__.__name__,
     def __current_object(self):
         try:
             return getattr(self.__local, self.__name__)
-        except:
+        except AttributeError:
             raise RuntimeError('no object bound to %s' % self.__name__)
     __current_object = property(__current_object)
 
     def __dict__(self):
-        return self.__current_object.__dict__
+        try:
+            return self.__current_object.__dict__
+        except RuntimeError:
+            return AttributeError('__dict__')
     __dict__ = property(__dict__)
 
     def __repr__(self):
         try:
             obj = self.__current_object
-        except:
+        except RuntimeError:
             return '<%s unbound>' % self.__class__.__name__
         return repr(obj)
 
     def __nonzero__(self):
         try:
             return bool(self.__current_object)
-        except:
+        except RuntimeError:
             return False
 
     def __unicode__(self):
         try:
             return unicode(self.__current_oject)
-        except:
+        except RuntimeError:
             return repr(self)
 
     def __dir__(self):
         try:
             return dir(self.__current_object)
-        except:
+        except RuntimeError:
             return []
 
     def __getattr__(self, name):