Advertisement
Guest User

Untitled

a guest
Sep 26th, 2010
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.63 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3.    werkzeug.local
  4.    ~~~~~~~~~~~~~~
  5.  
  6.    This module implements context-local objects.
  7.  
  8.    :copyright: (c) 2010 by the Werkzeug Team, see AUTHORS for more details.
  9.    :license: BSD, see LICENSE for more details.
  10. """
  11. try:
  12.     from greenlet import getcurrent as get_current_greenlet
  13. except ImportError: # pragma: no cover
  14.     try:
  15.         from py.magic import greenlet
  16.         get_current_greenlet = greenlet.getcurrent
  17.         del greenlet
  18.     except:
  19.         # catch all, py.* fails with so many different errors.
  20.         get_current_greenlet = int
  21. try:
  22.     from thread import get_ident as get_current_thread, allocate_lock
  23. except ImportError: # pragma: no cover
  24.     from dummy_thread import get_ident as get_current_thread, allocate_lock
  25.  
  26. from werkzeug.wsgi import ClosingIterator
  27. from werkzeug._internal import _patch_wrapper
  28.  
  29.  
  30. # get the best ident function.  if greenlets are not installed we can
  31. # safely just use the builtin thread function and save a python methodcall
  32. # and the cost of calculating a hash.
  33. if get_current_greenlet is int: # pragma: no cover
  34.     get_ident = get_current_thread
  35. else:
  36.     get_ident = lambda: (get_current_thread(), get_current_greenlet())
  37.  
  38.  
  39. def release_local(local):
  40.     """Releases the contents of the local for the current context.
  41.    This makes it possible to use locals without a manager.
  42.  
  43.    Example::
  44.  
  45.        >>> loc = Local()
  46.        >>> loc.foo = 42
  47.        >>> release_local(loc)
  48.        >>> hasattr(loc, 'foo')
  49.        False
  50.  
  51.    With this function one can release :class:`Local` objects as well
  52.    as :class:`StackLocal` objects.  However it is not possible to
  53.    release data held by proxies that way, one always has to retain
  54.    a reference to the underlying local object in order to be able
  55.    to release it.
  56.  
  57.    .. versionadded:: 0.6.1
  58.    """
  59.     local.__release_local__()
  60.  
  61.  
  62. class Local(object):
  63.     __slots__ = ('__storage__', '__lock__')
  64.  
  65.     def __init__(self):
  66.         object.__setattr__(self, '__storage__', {})
  67.         object.__setattr__(self, '__lock__', allocate_lock())
  68.  
  69.     def __iter__(self):
  70.         return self.__storage__.iteritems()
  71.  
  72.     def __call__(self, proxy):
  73.         """Create a proxy for a name."""
  74.         return LocalProxy(self, proxy)
  75.  
  76.     def __release_local__(self):
  77.         self.__storage__.pop(get_ident(), None)
  78.  
  79.     def __getattr__(self, name):
  80.         self.__lock__.acquire()
  81.         try:
  82.             try:
  83.                 return self.__storage__[get_ident()][name]
  84.             except KeyError:
  85.                 raise AttributeError(name)
  86.         finally:
  87.             self.__lock__.release()
  88.  
  89.     def __setattr__(self, name, value):
  90.         self.__lock__.acquire()
  91.         try:
  92.             ident = get_ident()
  93.             storage = self.__storage__
  94.             if ident in storage:
  95.                 storage[ident][name] = value
  96.             else:
  97.                 storage[ident] = {name: value}
  98.         finally:
  99.             self.__lock__.release()
  100.  
  101.     def __delattr__(self, name):
  102.         self.__lock__.acquire()
  103.         try:
  104.             try:
  105.                 del self.__storage__[get_ident()][name]
  106.             except KeyError:
  107.                 raise AttributeError(name)
  108.         finally:
  109.             self.__lock__.release()
  110.  
  111.  
  112. class LocalStack(object):
  113.     """This class works similar to a :class:`Local` but keeps a stack
  114.    of objects instead.  This is best explained with an example::
  115.  
  116.        >>> ls = LocalStack()
  117.        >>> ls.push(42)
  118.        >>> ls.top
  119.        42
  120.        >>> ls.push(23)
  121.        >>> ls.top
  122.        23
  123.        >>> ls.pop()
  124.        23
  125.        >>> ls.top
  126.        42
  127.  
  128.    They can be force released by using a :class:`LocalManager` or with
  129.    the :func:`release_local` function but the correct way is to pop the
  130.    item from the stack after using.  When the stack is empty it will
  131.    no longer be bound to the current context (and as such released).
  132.  
  133.    By calling the stack without arguments it returns a proxy that resolves to
  134.    the topmost item on the stack.
  135.  
  136.    .. versionadded:: 0.6.1
  137.    """
  138.  
  139.     def __init__(self):
  140.         self._local = Local()
  141.         self._lock = allocate_lock()
  142.  
  143.     def __release_local__(self):
  144.         self._local.__release_local__()
  145.  
  146.     def __call__(self):
  147.         def _lookup():
  148.             rv = self.top
  149.             if rv is None:
  150.                 raise RuntimeError('object unbound')
  151.             return rv
  152.         return LocalProxy(_lookup)
  153.  
  154.     def push(self, obj):
  155.         """Pushes a new item to the stack"""
  156.         self._lock.acquire()
  157.         try:
  158.             rv = getattr(self._local, 'stack', None)
  159.             if rv is None:
  160.                 self._local.stack = rv = []
  161.             rv.append(obj)
  162.             return rv
  163.         finally:
  164.             self._lock.release()
  165.  
  166.     def pop(self):
  167.         """Removes the topmost item from the stack, will return the
  168.        old value or `None` if the stack was already empty.
  169.        """
  170.         self._lock.acquire()
  171.         try:
  172.             stack = getattr(self._local, 'stack', None)
  173.             if stack is None:
  174.                 return None
  175.             elif len(stack) == 1:
  176.                 release_local(self._local)
  177.                 return stack[-1]
  178.             else:
  179.                 return stack.pop()
  180.         finally:
  181.             self._lock.release()
  182.  
  183.     @property
  184.     def top(self):
  185.         """The topmost item on the stack.  If the stack is empty,
  186.        `None` is returned.
  187.        """
  188.         try:
  189.             return self._local.stack[-1]
  190.         except (AttributeError, IndexError):
  191.             return None
  192.  
  193.  
  194. class LocalManager(object):
  195.     """Local objects cannot manage themselves. For that you need a local
  196.    manager.  You can pass a local manager multiple locals or add them later
  197.    by appending them to `manager.locals`.  Everytime the manager cleans up
  198.    it, will clean up all the data left in the locals for this context.
  199.  
  200.    .. versionchanged:: 0.6.1
  201.       Instead of a manager the :func:`release_local` function can be used
  202.       as well.
  203.    """
  204.  
  205.     def __init__(self, locals=None):
  206.         if locals is None:
  207.             self.locals = []
  208.         elif isinstance(locals, Local):
  209.             self.locals = [locals]
  210.         else:
  211.             self.locals = list(locals)
  212.  
  213.     def get_ident(self):
  214.         """Return the context identifier the local objects use internally for
  215.        this context.  You cannot override this method to change the behavior
  216.        but use it to link other context local objects (such as SQLAlchemy's
  217.        scoped sessions) to the Werkzeug locals.
  218.        """
  219.         return get_ident()
  220.  
  221.     def cleanup(self):
  222.         """Manually clean up the data in the locals for this context.  Call
  223.        this at the end of the request or use `make_middleware()`.
  224.        """
  225.         ident = self.get_ident()
  226.         for local in self.locals:
  227.             release_local(local)
  228.  
  229.     def make_middleware(self, app):
  230.         """Wrap a WSGI application so that cleaning up happens after
  231.        request end.
  232.        """
  233.         def application(environ, start_response):
  234.             return ClosingIterator(app(environ, start_response), self.cleanup)
  235.         return application
  236.  
  237.     def middleware(self, func):
  238.         """Like `make_middleware` but for decorating functions.
  239.  
  240.        Example usage::
  241.  
  242.            @manager.middleware
  243.            def application(environ, start_response):
  244.                ...
  245.  
  246.        The difference to `make_middleware` is that the function passed
  247.        will have all the arguments copied from the inner application
  248.        (name, docstring, module).
  249.        """
  250.         return _patch_wrapper(func, self.make_middleware(func))
  251.  
  252.     def __repr__(self):
  253.         return '<%s storages: %d>' % (
  254.             self.__class__.__name__,
  255.             len(self.locals)
  256.         )
  257.  
  258.  
  259. class LocalProxy(object):
  260.     """Acts as a proxy for a werkzeug local.  Forwards all operations to
  261.    a proxied object.  The only operations not supported for forwarding
  262.    are right handed operands and any kind of assignment.
  263.  
  264.    Example usage::
  265.  
  266.        from werkzeug import Local
  267.        l = Local()
  268.  
  269.        # these are proxies
  270.        request = l('request')
  271.        user = l('user')
  272.  
  273.  
  274.        from werkzeug import LocalStack
  275.        _response_local = LocalStack()
  276.  
  277.        # this is a proxy
  278.        response = _response_local()
  279.  
  280.    Whenever something is bound to l.user / l.request the proxy objects
  281.    will forward all operations.  If no object is bound a :exc:`RuntimeError`
  282.    will be raised.
  283.  
  284.    To create proxies to :class:`Local` or :class:`LocalStack` objects,
  285.    call the object as shown above.  If you want to have a proxy to an
  286.    object looked up by a function, you can (as of Werkzeug 0.6.1) pass
  287.    a function to the :class:`LocalProxy` constructor::
  288.  
  289.        session = LocalProxy(lambda: get_current_request().session)
  290.  
  291.    .. versionchanged:: 0.6.1
  292.       The class can be instanciated with a callable as well now.
  293.    """
  294.     __slots__ = ('__local', '__dict__', '__name__')
  295.  
  296.     def __init__(self, local, name=None):
  297.         object.__setattr__(self, '_LocalProxy__local', local)
  298.         object.__setattr__(self, '__name__', name)
  299.  
  300.     def _get_current_object(self):
  301.         """Return the current object.  This is useful if you want the real
  302.        object behind the proxy at a time for performance reasons or because
  303.        you want to pass the object into a different context.
  304.        """
  305.         if not hasattr(self.__local, '__release_local__'):
  306.             return self.__local()
  307.         try:
  308.             return getattr(self.__local, self.__name__)
  309.         except AttributeError:
  310.             raise RuntimeError('no object bound to %s' % self.__name__)
  311.  
  312.     @property
  313.     def __dict__(self):
  314.         try:
  315.             return self._get_current_object().__dict__
  316.         except RuntimeError:
  317.             return AttributeError('__dict__')
  318.  
  319.     def __repr__(self):
  320.         try:
  321.             obj = self._get_current_object()
  322.         except RuntimeError:
  323.             return '<%s unbound>' % self.__class__.__name__
  324.         return repr(obj)
  325.  
  326.     def __nonzero__(self):
  327.         try:
  328.             return bool(self._get_current_object())
  329.         except RuntimeError:
  330.             return False
  331.  
  332.     def __unicode__(self):
  333.         try:
  334.             return unicode(self._get_current_object())
  335.         except RuntimeError:
  336.             return repr(self)
  337.  
  338.     def __dir__(self):
  339.         try:
  340.             return dir(self._get_current_object())
  341.         except RuntimeError:
  342.             return []
  343.  
  344.     def __getattr__(self, name):
  345.         if name == '__members__':
  346.             return dir(self._get_current_object())
  347.         return getattr(self._get_current_object(), name)
  348.  
  349.     def __setitem__(self, key, value):
  350.         self._get_current_object()[key] = value
  351.  
  352.     def __delitem__(self, key):
  353.         del self._get_current_object()[key]
  354.  
  355.     def __setslice__(self, i, j, seq):
  356.         self._get_current_object()[i:j] = seq
  357.  
  358.     def __delslice__(self, i, j):
  359.         del self._get_current_object()[i:j]
  360.  
  361.     __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
  362.     __delattr__ = lambda x, n: delattr(x._get_current_object(), n)
  363.     __str__ = lambda x: str(x._get_current_object())
  364.     __lt__ = lambda x, o: x._get_current_object() < o
  365.     __le__ = lambda x, o: x._get_current_object() <= o
  366.     __eq__ = lambda x, o: x._get_current_object() == o
  367.     __ne__ = lambda x, o: x._get_current_object() != o
  368.     __gt__ = lambda x, o: x._get_current_object() > o
  369.     __ge__ = lambda x, o: x._get_current_object() >= o
  370.     __cmp__ = lambda x, o: cmp(x._get_current_object(), o)
  371.     __hash__ = lambda x: hash(x._get_current_object())
  372.     __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
  373.     __len__ = lambda x: len(x._get_current_object())
  374.     __getitem__ = lambda x, i: x._get_current_object()[i]
  375.     __iter__ = lambda x: iter(x._get_current_object())
  376.     __contains__ = lambda x, i: i in x._get_current_object()
  377.     __getslice__ = lambda x, i, j: x._get_current_object()[i:j]
  378.     __add__ = lambda x, o: x._get_current_object() + o
  379.     __sub__ = lambda x, o: x._get_current_object() - o
  380.     __mul__ = lambda x, o: x._get_current_object() * o
  381.     __floordiv__ = lambda x, o: x._get_current_object() // o
  382.     __mod__ = lambda x, o: x._get_current_object() % o
  383.     __divmod__ = lambda x, o: x._get_current_object().__divmod__(o)
  384.     __pow__ = lambda x, o: x._get_current_object() ** o
  385.     __lshift__ = lambda x, o: x._get_current_object() << o
  386.     __rshift__ = lambda x, o: x._get_current_object() >> o
  387.     __and__ = lambda x, o: x._get_current_object() & o
  388.     __xor__ = lambda x, o: x._get_current_object() ^ o
  389.     __or__ = lambda x, o: x._get_current_object() | o
  390.     __div__ = lambda x, o: x._get_current_object().__div__(o)
  391.     __truediv__ = lambda x, o: x._get_current_object().__truediv__(o)
  392.     __neg__ = lambda x: -(x._get_current_object())
  393.     __pos__ = lambda x: +(x._get_current_object())
  394.     __abs__ = lambda x: abs(x._get_current_object())
  395.     __invert__ = lambda x: ~(x._get_current_object())
  396.     __complex__ = lambda x: complex(x._get_current_object())
  397.     __int__ = lambda x: int(x._get_current_object())
  398.     __long__ = lambda x: long(x._get_current_object())
  399.     __float__ = lambda x: float(x._get_current_object())
  400.     __oct__ = lambda x: oct(x._get_current_object())
  401.     __hex__ = lambda x: hex(x._get_current_object())
  402.     __index__ = lambda x: x._get_current_object().__index__()
  403.     __coerce__ = lambda x, o: x.__coerce__(x, o)
  404.     __enter__ = lambda x: x.__enter__()
  405.     __exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement