Exemple #1
0
class Context(object):
    locals = Local()
    local_manager = LocalManager([locals])
    current_context = locals('context')

    def __init__(self, app, environ=None, request=None, args=None):
        self.app = app
        self.environ = environ
        self.request = request
        self.args = args
        self.response = None

    def __enter__(self):
        self.locals.context = self
        for fn in self.app.get_hook_functions('context-start'):
            fn(self)
        return self

    def __exit__(self, type, value, traceback):
        for fn in self.app.get_hook_functions('context-end'):
            fn(self)
        del self.locals.context
# -*- coding: utf-8 -*-
"""
    lodgeit.utils
    ~~~~~~~~~~~~~

    Serveral utilities used by LodgeIt.

    :copyright: 2008 by Christopher Grebs.
    :license: BSD
"""
from werkzeug import Local, LocalManager, LocalProxy

#: context locals
ctx = Local()
_local_manager = LocalManager(ctx)

#: local objects
request = LocalProxy(ctx, 'request')
application = LocalProxy(ctx, 'application')
Exemple #3
0
# -*- coding: utf-8 -*-
from werkzeug import Local, LocalManager
from werkzeug.routing import Map, Rule
from os import path
from urlparse import urlparse
from werkzeug import Response
from jinja2 import Environment, FileSystemLoader
from json import dumps

local = Local()
local_manager = LocalManager([local])
application = local('application')

url_map = Map([Rule('/static/<file>', endpoint='static', build_only=True)])


def expose(rule, **kw):
    def decorate(f):
        kw['endpoint'] = f.__name__ + " " + f.__module__
        url_map.add(Rule(rule, **kw))
        return f

    return decorate


def url_for(endpoint, _external=False, **values):
    return local.url_adapter.build(endpoint, values, force_external=_external)


ALLOWED_SCHEMES = frozenset(['http'])
TEMPLATE_PATH = 'template'
Exemple #4
0
    if top is None:
        raise RuntimeError('working outside of request context')
    return getattr(top, name)


# Context Locals
# -------------
#
# Use these context locals with caution and only where
# you don't have access to the current request/application
# object at all.  If there are easy ways of *not* using
# context locals, you should not use them.

_request_ctx_stack = LocalStack()
local = Local()
local_manager = LocalManager(local)

# Proxy definitions of commonly used objects.
ctx = local('ctx')
request = LocalProxy(partial(_lookup_object, 'request'))


class LocalProperty(object):
    """Class/Instance property that returns something from the local stack.

    Note that if some value is not present in the current thread local
    it does *not* raise an `RuntimeError` but returns `None`.
    """
    def __init__(self, name):
        self.__name__ = name
Exemple #5
0
# -*- coding: utf-8 -*-
"""
    solace.utils.ctxlocal
    ~~~~~~~~~~~~~~~~~~~~~

    The context local that is used in the application and i18n system.  The
    application makes this request-bound.

    :copyright: (c) 2010 by the Solace Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""
from werkzeug import Local, LocalManager


local = Local()
local_mgr = LocalManager([local])


class LocalProperty(object):
    """Class/Instance property that returns something from the local."""

    def __init__(self, name):
        self.__name__ = name

    def __get__(self, obj, type=None):
        return getattr(local, self.__name__, None)


# make sure the request local is removed at the end of the request
from solace.signals import after_request_shutdown
after_request_shutdown.connect(local_mgr.cleanup)