示例#1
0
    def __call__(self, environ, start_response):
        """
        Accept ``environ`` and ``start_response``; create a
        :term:`request` and route the request to a :app:`Pyramid`
        view based on introspection of :term:`view configuration`
        within the application registry; call ``start_response`` and
        return an iterable.
        """
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        request = self.request_factory(environ)
        threadlocals = {'registry': registry, 'request': request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)
        request.registry = registry
        try:

            try:
                response = self.handle_request(request)
                has_listeners and notify(NewResponse(request, response))

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                return response(request.environ, start_response)

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#2
0
    def invoke_request(self, request,
                       _use_tweens=True, _apply_extensions=False):
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        threadlocals = {'registry': registry, 'request': request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)

        if _use_tweens:
            handle_request = self.handle_request
        else:
            handle_request = self.orig_handle_request

        try:

            try:
                extensions = self.request_extensions
                if _apply_extensions and extensions is not None:
                    apply_request_extensions(request, extensions=extensions)
                response = handle_request(request)

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                has_listeners and notify(NewResponse(request, response))

                return response

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#3
0
    def __call__(self, environ, start_response):
        """
        Accept ``environ`` and ``start_response``; create a
        :term:`request` and route the request to a :app:`Pyramid`
        view based on introspection of :term:`view configuration`
        within the application registry; call ``start_response`` and
        return an iterable.
        """
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        request = self.request_factory(environ)
        threadlocals = {"registry": registry, "request": request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)
        request.registry = registry
        try:

            try:
                response = self.handle_request(request)
                has_listeners and notify(NewResponse(request, response))

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                return response(request.environ, start_response)

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#4
0
    def invoke_request(self, request,
                       _use_tweens=True, _apply_extensions=False):
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        threadlocals = {'registry': registry, 'request': request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)

        if _use_tweens:
            handle_request = self.handle_request
        else:
            handle_request = self.orig_handle_request

        try:

            try:
                extensions = self.request_extensions
                if _apply_extensions and extensions is not None:
                    apply_request_extensions(request, extensions=extensions)
                response = handle_request(request)

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                has_listeners and notify(NewResponse(request, response))

                return response

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#5
0
def clear_snapshot(signum=None, frame=None):
    global current_xmin_snapshot_id
    if current_xmin_snapshot_id is None:
        return
    transaction.abort()
    manager.pop()
    current_xmin_snapshot_id = None
示例#6
0
文件: router.py 项目: malthe/pyramid
    def __call__(self, environ, start_response):
        """
        Accept ``environ`` and ``start_response``; create a
        :term:`request` and route the request to a :app:`Pyramid`
        view based on introspection of :term:`view configuration`
        within the application registry; call ``start_response`` and
        return an iterable.
        """
        registry = self.registry
        manager = self.threadlocal_manager
        request = None
        threadlocals = {'registry':registry, 'request':request}
        manager.push(threadlocals)

        try:
            try:
                request = self.request_factory(environ)
                threadlocals['request'] = request
                request.registry = registry
                response = self.handle_request(request)
            finally:
                if request is not None and request.finished_callbacks:
                    request._process_finished_callbacks()

            return response(request.environ, start_response)
        finally:
            manager.pop()
示例#7
0
 def test_it(self):
     from pyramid.threadlocal import manager
     try:
         manager.push({'registry':123})
         self.assertEqual(self._callFUT(), 123)
     finally:
         manager.pop()
示例#8
0
def clear_snapshot(signum=None, frame=None):
    global current_xmin_snapshot_id
    if current_xmin_snapshot_id is None:
        return
    transaction.abort()
    manager.pop()
    current_xmin_snapshot_id = None
示例#9
0
 def test_it(self):
     from pyramid.threadlocal import manager
     request = object()
     try:
         manager.push({'request':request})
         self.assertEqual(self._callFUT(), request)
     finally:
         manager.pop()
     self.assertEqual(self._callFUT(), None)
示例#10
0
def init_application(event):
    app = event.object
    registry = app.registry
    request = Request.blank('/application_created')  # path is meaningless
    request.registry = registry
    manager.push({'registry': registry, 'request': request})
    # Set up sms service backend
    registry.registerAdapter(factory=OvhService,
                             required=(IRequest, ),
                             provided=ISMSService)
    root = app.root_factory(request)
    # A transaction.commit() just happened here if this is the first time we
    # start. This is just after all RootAdded subscribers are executed.
    request.root = root
    # other init functions
    if getattr(root, 'locale', None) is None:
        try:
            # This code is actually an evolve step for old novaideo instances.
            # The root.locale is set in the RootAdded subscriber above
            # for new instances.
            root.locale = registry.settings.get('pyramid.default_locale_name')
            transaction.commit()
        except ConflictError:
            # We have a conflict error in case of serveral workers, just abort
            transaction.abort()

    init_contents(registry)  # there is no changes in ZODB here
    # invite initial user if first deployment
    if getattr(root, 'first_invitation_to_add', False):
        # LOGO_FILENAME='marianne.svg' for example
        logo = os.getenv('LOGO_FILENAME', '')
        if logo:
            logo_path = os.path.join(os.path.dirname(__file__), 'static',
                                     'images', logo)
            if os.path.exists(logo_path):
                buf = open(logo_path, mode='rb')
                log_file = File(fp=buf,
                                filename=logo,
                                mimetype='image/svg+xml')
                root.setproperty('picture', log_file)

        title = os.getenv('INITIAL_USER_TITLE', '')
        first_name = os.getenv('INITIAL_USER_FIRSTNAME', '')
        last_name = os.getenv('INITIAL_USER_LASTNAME', '')
        email = os.getenv('INITIAL_USER_EMAIL', '')
        phone = os.getenv('INITIAL_USER_PHONE', '')
        if first_name and last_name and (phone or email):
            _invite_first_user(root, request, title, first_name, last_name,
                               email, phone)

        del root.first_invitation_to_add
        # This is a change in ZODB, but it's ok, it is executed only the first
        # time when we only have one worker.

    transaction.commit()
    manager.pop()
示例#11
0
def execute_callback(app, callback, login):
    # set site and interaction that will be memorized in job
    request = DummyRequest()
    request.root = app
    registry = get_current_registry()
    manager.push({'registry': registry, 'request': request})
    user = get_user_by_login(login, request)
    request.user = user
    callback()
    manager.pop()
示例#12
0
文件: util.py 项目: ecreall/dace
def execute_callback(app, callback, login):
    # set site and interaction that will be memorized in job
    request = DummyRequest()
    request.root = app
    registry = get_current_registry()
    manager.push({'registry': registry, 'request': request})
    user = get_user_by_login(login, request)
    request.user = user
    callback()
    manager.pop()
 def test_add_translation_dirs_registers_chameleon_translate(self):
     from pyramid.interfaces import IChameleonTranslate
     from pyramid.threadlocal import manager
     request = DummyRequest()
     config = self._makeOne(autocommit=True)
     manager.push({'request':request, 'registry':config.registry})
     try:
         config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale')
         translate = config.registry.getUtility(IChameleonTranslate)
         self.assertEqual(translate('Approve'), u'Approve')
     finally:
         manager.pop()
示例#14
0
 def _threadContext(self):
   # IMPORTANT: this assumes that APScheduler invokes jobs in a separate
   #            thread per job (as documented)...
   # TODO: this needs confirmation!
   if self.appreg is None:
     yield
   else:
     from pyramid.threadlocal import manager
     reg = dict(manager.get())
     reg['registry'] = self.appreg
     manager.push(reg)
     try:
       yield
     finally:
       manager.pop()
示例#15
0
 def _threadContext(self):
   # IMPORTANT: this assumes that APScheduler invokes jobs in a separate
   #            thread per job (as documented)...
   # TODO: this needs confirmation!
   if self.appreg is None:
     yield
   else:
     from pyramid.threadlocal import manager
     reg = dict(manager.get())
     reg['registry'] = self.appreg
     manager.push(reg)
     try:
       yield
     finally:
       manager.pop()
示例#16
0
 def test_it_raises_if_no_registry(self):
     request = self._makeOne()
     del request.registry
     from pyramid.threadlocal import manager
     manager.push({'registry': None, 'request': request})
     try:
         raise RuntimeError
     except RuntimeError:
         try:
             request.invoke_exception_view()
         except RuntimeError as e:
             self.assertEqual(e.args[0], "Unable to retrieve registry")
     else: # pragma: no cover
         self.fail()
     finally:
         manager.pop()
示例#17
0
def tearDown(unhook_zca=True):
    """Undo the effects of :func:`pyramid.testing.setUp`.  Use this
    function in the ``tearDown`` method of a unit test that uses
    :func:`pyramid.testing.setUp` in its ``setUp`` method.

    If the ``unhook_zca`` argument is ``True`` (the default), call
    :func:`zope.component.getSiteManager.reset`.  This undoes the
    action of :func:`pyramid.testing.setUp` when called with the
    argument ``hook_zca=True``.  If :mod:`zope.component` cannot be
    imported, ``unhook_zca`` is set to ``False``.
    """
    global have_zca
    if unhook_zca and have_zca:
        try:
            from zope.component import getSiteManager
            getSiteManager.reset()
        except ImportError: # pragma: no cover
            have_zca = False
    info = manager.pop()
    manager.clear()
    if info is not None:
        registry = info['registry']
        if hasattr(registry, '__init__') and hasattr(registry, '__name__'):
            try:
                registry.__init__(registry.__name__)
            except TypeError:
                # calling __init__ is largely for the benefit of
                # people who want to use the global ZCA registry;
                # however maybe somebody's using a registry we don't
                # understand, let's not blow up
                pass
示例#18
0
 def test_it_raises_if_no_registry(self):
     request = self._makeOne()
     del request.registry
     from pyramid.threadlocal import manager
     manager.push({'registry': None, 'request': request})
     try:
         raise RuntimeError
     except RuntimeError:
         try:
             request.invoke_exception_view()
         except RuntimeError as e:
             self.assertEqual(e.args[0], "Unable to retrieve registry")
     else:  # pragma: no cover
         self.fail()
     finally:
         manager.pop()
示例#19
0
def tearDown(unhook_zca=True):
    """Undo the effects :func:`pyramid.testing.setUp`.  Use this
    function in the ``tearDown`` method of a unit test that uses
    :func:`pyramid.testing.setUp` in its ``setUp`` method.

    If the ``unhook_zca`` argument is ``True`` (the default), call
    :func:`zope.component.getSiteManager.reset`.  This undoes the
    action of :func:`pyramid.testing.setUp` called with the
    argument ``hook_zca=True``.  If :mod:`zope.component` cannot be
    imported, ignore the argument.
    """
    global have_zca
    if unhook_zca and have_zca:
        try:
            from zope.component import getSiteManager
            getSiteManager.reset()
        except ImportError:  # pragma: no cover
            have_zca = False
    info = manager.pop()
    manager.clear()
    if info is not None:
        registry = info['registry']
        if hasattr(registry, '__init__') and hasattr(registry, '__name__'):
            try:
                registry.__init__(registry.__name__)
            except TypeError:
                # calling __init__ is largely for the benefit of
                # people who want to use the global ZCA registry;
                # however maybe somebody's using a registry we don't
                # understand, let's not blow up
                pass
示例#20
0
def add_process_definitions(event):
    app = event.object
    registry = app.registry
    settings = getattr(registry, 'settings', {})
    request = Request.blank('/application_created') # path is meaningless
    request.registry = registry
    manager.push({'registry': registry, 'request': request})
    root = app.root_factory(request)
    request.root = root

    # use same env variable as substanced catalog to determine
    # if we want to recreate process definitions
    autosync = asbool(
        os.environ.get(
        'SUBSTANCED_CATALOGS_AUTOSYNC',
        settings.get(
            'substanced.catalogs.autosync',
            settings.get('substanced.autosync_catalogs', False) # bc
            )))
    def_container = root['process_definition_container']
    if autosync:
        for definition in def_container.definitions:
            if hasattr(definition, '_broken_object'):
                name = definition.__name__
                def_container.remove(name, send_events=False)
                def_container._definitions_value.remove(name)

    for definition in processdef_container.DEFINITIONS.values():
        old_def = def_container.get(definition.id, None)
        if old_def is None:
            def_container.add_definition(definition)
        else:
            if autosync:
                def_container.delfromproperty('definitions', old_def)
                def_container.add_definition(definition)

    for definition in def_container.definitions:
        for node in definition.nodes:
            for context in getattr(node, 'contexts', []):
                context.node_definition = node

    if autosync:
        processdef_container.DEFINITIONS.clear()

    transaction.commit()
    registry.notify(DatabaseOpenedWithRoot(root._p_jar.db()))
    manager.pop()
示例#21
0
    def invoke_subrequest(self, request, use_tweens=False):
        """Obtain a response object from the Pyramid application based on
        information in the ``request`` object provided.  The ``request``
        object must be an object that implements the Pyramid request
        interface (such as a :class:`pyramid.request.Request` instance).  If
        ``use_tweens`` is ``True``, the request will be sent to the
        :term:`tween` in the tween stack closest to the request ingress.  If
        ``use_tweens`` is ``False``, the request will be sent to the main
        router handler, and no tweens will be invoked.
        
        See the API for pyramid.request for complete documentation.
        """
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        threadlocals = {'registry':registry, 'request':request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)
        request.registry = registry
        request.invoke_subrequest = self.invoke_subrequest
        
        if use_tweens:
            handle_request = self.handle_request
        else:
            handle_request = self.orig_handle_request

        try:

            try:
                extensions = self.request_extensions
                if extensions is not None:
                    apply_request_extensions(request, extensions=extensions)
                response = handle_request(request)

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                has_listeners and notify(NewResponse(request, response))
                
                return response

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#22
0
    def invoke_subrequest(self, request, use_tweens=False):
        """Obtain a response object from the Pyramid application based on
        information in the ``request`` object provided.  The ``request``
        object must be an object that implements the Pyramid request
        interface (such as a :class:`pyramid.request.Request` instance).  If
        ``use_tweens`` is ``True``, the request will be sent to the
        :term:`tween` in the tween stack closest to the request ingress.  If
        ``use_tweens`` is ``False``, the request will be sent to the main
        router handler, and no tweens will be invoked.
        
        See the API for pyramid.request for complete documentation.
        """
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        threadlocals = {'registry': registry, 'request': request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)
        request.registry = registry
        request.invoke_subrequest = self.invoke_subrequest

        if use_tweens:
            handle_request = self.handle_request
        else:
            handle_request = self.orig_handle_request

        try:

            try:
                extensions = self.request_extensions
                if extensions is not None:
                    request._set_extensions(extensions)
                response = handle_request(request)

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                has_listeners and notify(NewResponse(request, response))

                return response

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#23
0
def test_request_make_body_seekable_cleans_up_threadmanger_on_exception(config):
    from pyramid.threadlocal import manager
    # Clear defaults.
    manager.pop()
    assert len(manager.stack) == 0
    app = config.make_wsgi_app()
    app = webtest.TestApp(app)
    with pytest.raises(Exception):
        # Content-length=1 and empty body causes
        # webob.request.DisconnectionError:
        # The client disconnected while sending the body
        # (1 more bytes were expected) when you call
        # request.make_body_seekable().
        app.get('/', headers={'Content-Length': '1'})
    # len(manager.stack) == 1 when you don't catch exception
    # from request.make_body_seekable() and clean up.
    assert len(manager.stack) == 0
示例#24
0
def test_activate_hook_cleans_up_threadmanager_on_generator_exit(config):
    from pyramid.threadlocal import manager
    # Clear defaults.
    manager.pop()
    assert len(manager.stack) == 0
    def activate_hook(request):
        raise GeneratorExit
    config.add_settings({
        'retry.attempts': 3,
        'retry.activate_hook': activate_hook,
    })
    app = config.make_wsgi_app()
    app = webtest.TestApp(app)
    with pytest.raises(GeneratorExit):
        app.get('/')
    # len(manager.stack) == 1 when you don't catch GeneratorExit
    # from activate_hook and clean up.
    assert len(manager.stack) == 0
示例#25
0
    def execute(self, p_steps=None, request=None):
        registry = self.registry
        if request is None:
            request_factory = registry.queryUtility(IRequestFactory,
                                                    default=Request)
            request = request_factory.blank('/')
            request.registry = registry

        threadlocals = {'registry': registry, 'request': request}
        threadlocal_manager.push(threadlocals)

        steps = self.list_steps(p_steps)

        log = logging.getLogger('ptah')

        for step in steps:
            log.info('Executing populate step: %s', step['name'])
            step['factory'](registry)

        transaction.commit()
        threadlocal_manager.pop()
示例#26
0
文件: populate.py 项目: carlicos/ptah
    def execute(self, p_steps=None, request=None):
        registry = self.registry
        if request is None:
            request_factory = registry.queryUtility(
                IRequestFactory, default=Request)
            request = request_factory.blank('/')
            request.registry = registry

        threadlocals = {'registry':registry, 'request':request}
        threadlocal_manager.push(threadlocals)

        steps = self.list_steps(p_steps)

        log = logging.getLogger('ptah')

        for step in steps:
            log.info('Executing populate step: %s', step['name'])
            step['factory'](registry)

        transaction.commit()
        threadlocal_manager.pop()
示例#27
0
def add_services_definitions(event):
    app = event.object
    registry = app.registry
    settings = getattr(registry, 'settings', {})
    request = Request.blank('/application_created')  # path is meaningless
    request.registry = registry
    manager.push({'registry': registry, 'request': request})
    root = app.root_factory(request)
    request.root = root

    # use same env variable as substanced catalog to determine
    # if we want to upgrade definitions
    autosync = asbool(
        os.environ.get(
            'SUBSTANCED_CATALOGS_AUTOSYNC',
            settings.get(
                'substanced.catalogs.autosync',
                settings.get('substanced.autosync_catalogs', False)  # bc
            )))

    existing_definitions = root.get_services_definition()
    if autosync:
        for definition in existing_definitions.values():
            if hasattr(definition, '_broken_object'):
                root.delfromproperty('services_definition', definition)

    for definition in core.SERVICES_DEFINITION.values():
        old_def = existing_definitions.get(definition.service_id, None)
        if old_def is None:
            root.addtoproperty('services_definition', definition)

    core.SERVICES_DEFINITION.clear()

    # other init functions
    init_site_folders(root)
    init_contents(registry)
    init_sites_social_login(root)
    transaction.commit()
    manager.pop()
示例#28
0
def add_services_definitions(event):
    app = event.object
    registry = app.registry
    settings = getattr(registry, 'settings', {})
    request = Request.blank('/application_created') # path is meaningless
    request.registry = registry
    manager.push({'registry': registry, 'request': request})
    root = app.root_factory(request)
    request.root = root

    # use same env variable as substanced catalog to determine
    # if we want to upgrade definitions
    autosync = asbool(
        os.environ.get(
        'SUBSTANCED_CATALOGS_AUTOSYNC',
        settings.get(
            'substanced.catalogs.autosync',
            settings.get('substanced.autosync_catalogs', False) # bc
            )))

    existing_definitions = root.get_services_definition()
    if autosync:
        for definition in existing_definitions.values():
            if hasattr(definition, '_broken_object'):
                root.delfromproperty('services_definition', definition)

    for definition in core.SERVICES_DEFINITION.values():
        old_def = existing_definitions.get(definition.service_id, None)
        if old_def is None:
            root.addtoproperty('services_definition', definition)

    core.SERVICES_DEFINITION.clear()

    # other init functions
    init_site_folders(root)
    init_contents(registry)
    init_sites_social_login(root)
    transaction.commit()
    manager.pop()
示例#29
0
文件: router.py 项目: matkins/pyramid
    def invoke_request(self, request, _use_tweens=True):
        """
        Execute a request through the request processing pipeline and
        return the generated response.

        """
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        threadlocals = {'registry': registry, 'request': request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)

        if _use_tweens:
            handle_request = self.handle_request
        else:
            handle_request = self.orig_handle_request

        try:

            try:
                response = handle_request(request)

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                has_listeners and notify(NewResponse(request, response))

                return response

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#30
0
def tearDown(unhook_zca=True):
    """Undo the effects :func:`pyramid.testing.setUp`.  Use this
    function in the ``tearDown`` method of a unit test that uses
    :func:`pyramid.testing.setUp` in its ``setUp`` method.

    If the ``unhook_zca`` argument is ``True`` (the default), call
    :func:`zope.component.getSiteManager.reset`.  This undoes the
    action of :func:`pyramid.testing.setUp` called with the
    argument ``hook_zca=True``.  If :mod:`zope.component` cannot be
    imported, ignore the argument.

    .. warning:: Although this method of tearing a test setup down
                 will never disappear, after :app:`Pyramid` 1.0,
                 using the ``begin`` and ``end`` methods of a
                 ``Configurator`` are preferred to using
                 ``pyramid.testing.setUp`` and
                 ``pyramid.testing.tearDown``.  See
                 :ref:`unittesting_chapter` for more information.

    """
    if unhook_zca:
        try:
            from zope.component import getSiteManager

            getSiteManager.reset()
        except ImportError:  # pragma: no cover
            pass
    info = manager.pop()
    manager.clear()
    if info is not None:
        registry = info["registry"]
        if hasattr(registry, "__init__") and hasattr(registry, "__name__"):
            try:
                registry.__init__(registry.__name__)
            except TypeError:
                # calling __init__ is largely for the benefit of
                # people who want to use the global ZCA registry;
                # however maybe somebody's using a registry we don't
                # understand, let's not blow up
                pass
    _clearContext()  # XXX why?
示例#31
0
    def invoke_exception_view(
        self,
        exc_info=None,
        request=None,
        secure=True,
        reraise=False,
    ):
        """ Executes an exception view related to the request it's called upon.
        The arguments it takes are these:

        ``exc_info``

            If provided, should be a 3-tuple in the form provided by
            ``sys.exc_info()``.  If not provided,
            ``sys.exc_info()`` will be called to obtain the current
            interpreter exception information.  Default: ``None``.

        ``request``

            If the request to be used is not the same one as the instance that
            this method is called upon, it may be passed here.  Default:
            ``None``.

        ``secure``

            If the exception view should not be rendered if the current user
            does not have the appropriate permission, this should be ``True``.
            Default: ``True``.

        ``reraise``

            A boolean indicating whether the original error should be reraised
            if a :term:`response` object could not be created. If ``False``
            then an :class:`pyramid.httpexceptions.HTTPNotFound`` exception
            will be raised. Default: ``False``.

        If a response is generated then ``request.exception`` and
        ``request.exc_info`` will be left at the values used to render the
        response. Otherwise the previous values for ``request.exception`` and
        ``request.exc_info`` will be restored.

        .. versionadded:: 1.7

        .. versionchanged:: 1.9
           The ``request.exception`` and ``request.exc_info`` properties will
           reflect the exception used to render the response where previously
           they were reset to the values prior to invoking the method.

           Also added the ``reraise`` argument.

        """
        if request is None:
            request = self
        registry = getattr(request, 'registry', None)
        if registry is None:
            registry = get_current_registry()

        if registry is None:
            raise RuntimeError("Unable to retrieve registry")

        if exc_info is None:
            exc_info = sys.exc_info()

        exc = exc_info[1]
        attrs = request.__dict__
        context_iface = providedBy(exc)

        # clear old generated request.response, if any; it may
        # have been mutated by the view, and its state is not
        # sane (e.g. caching headers)
        with hide_attrs(request, 'response', 'exc_info', 'exception'):
            attrs['exception'] = exc
            attrs['exc_info'] = exc_info
            # we use .get instead of .__getitem__ below due to
            # https://github.com/Pylons/pyramid/issues/700
            request_iface = attrs.get('request_iface', IRequest)

            manager.push({'request': request, 'registry': registry})

            try:
                response = _call_view(
                    registry,
                    request,
                    exc,
                    context_iface,
                    '',
                    view_types=None,
                    view_classifier=IExceptionViewClassifier,
                    secure=secure,
                    request_iface=request_iface.combined,
                )
            except:
                if reraise:
                    reraise_(*exc_info)
                raise
            finally:
                manager.pop()

        if response is None:
            if reraise:
                reraise_(*exc_info)
            raise HTTPNotFound

        # successful response, overwrite exception/exc_info
        attrs['exception'] = exc
        attrs['exc_info'] = exc_info
        return response
示例#32
0
文件: base.py 项目: festrade/assembl
 def fin():
     # The request was not called
     manager.pop()
示例#33
0
def add_process_definitions(event):
    app = event.object
    registry = app.registry
    settings = getattr(registry, 'settings', {})
    request = Request.blank('/application_created')  # path is meaningless
    request.registry = registry
    manager.push({'registry': registry, 'request': request})
    root = app.root_factory(request)
    request.root = root

    # use same env variable as substanced catalog to determine
    # if we want to recreate process definitions.
    # autosync is True only in development mode.
    autosync = asbool(
        os.environ.get(
            'SUBSTANCED_CATALOGS_AUTOSYNC',
            settings.get(
                'substanced.catalogs.autosync',
                settings.get('substanced.autosync_catalogs', False)  # bc
            )))
    try:
        # This code block must be in sync with what we do in
        # process_definitions_evolve minus the autosync conditions
        def_container = root['process_definition_container']
        if autosync:
            for definition in def_container.definitions:
                if hasattr(definition, '_broken_object'):
                    name = definition.__name__
                    def_container.remove(name, send_events=False)
                    def_container._definitions_value.remove(name)

        for definition in processdef_container.DEFINITIONS.values():
            old_def = def_container.get(definition.id, None)
            if old_def is None:
                # We add the definition at startup when creating the application
                # the first time where we normally have one worker.
                # If we have more that one worker, the other workers will do
                # a ConflictError here.
                if getattr(def_container, '_initializing', False) or autosync:
                    def_container.add_definition(definition)
            else:
                if autosync:
                    def_container.delfromproperty('definitions', old_def)
                    def_container.add_definition(definition)

        if autosync:
            # if not autosync, we still need this global constant for the
            # process_definitions_evolve step
            processdef_container.DEFINITIONS.clear()

        if getattr(def_container, '_initializing', False):
            del def_container._initializing

        transaction.commit()
    except ConflictError:
        # The first worker did the changes, simply abort to get the changes.
        transaction.abort()

    # After the restart of the application, we always need to resync
    # the node_definition attributes to be the node definition instances
    # currently in ZODB.
    for definition in def_container.definitions:
        for node in definition.nodes:
            for context in getattr(node, 'contexts', []):
                # context here is a class, we set the class attribute
                # node_definition to the current node definition in ZODB
                context.node_definition = node

    registry.notify(DatabaseOpenedWithRoot(root._p_jar.db()))
    manager.pop()
示例#34
0
 def closer():
     threadlocal_manager.pop()
示例#35
0
 def fin():
     # The request was not called
     manager.pop()
示例#36
0
 def get_response(self, app, catch_exc_info=True):
     try:
         super(PyramidWebTestRequest,
               app).get_response(catch_exc_info=catch_exc_info)
     finally:
         manager.pop()
示例#37
0
def add_process_definitions(event):
    app = event.object
    registry = app.registry
    settings = getattr(registry, 'settings', {})
    request = Request.blank('/application_created') # path is meaningless
    request.registry = registry
    manager.push({'registry': registry, 'request': request})
    root = app.root_factory(request)
    request.root = root

    # use same env variable as substanced catalog to determine
    # if we want to recreate process definitions.
    # autosync is True only in development mode.
    autosync = asbool(
        os.environ.get(
        'SUBSTANCED_CATALOGS_AUTOSYNC',
        settings.get(
            'substanced.catalogs.autosync',
            settings.get('substanced.autosync_catalogs', False) # bc
            )))
    try:
        # This code block must be in sync with what we do in
        # process_definitions_evolve minus the autosync conditions
        def_container = root['process_definition_container']
        if autosync:
            for definition in def_container.definitions:
                if hasattr(definition, '_broken_object'):
                    name = definition.__name__
                    def_container.remove(name, send_events=False)
                    def_container._definitions_value.remove(name)

        for definition in processdef_container.DEFINITIONS.values():
            old_def = def_container.get(definition.id, None)
            if old_def is None:
                # We add the definition at startup when creating the application
                # the first time where we normally have one worker.
                # If we have more that one worker, the other workers will do
                # a ConflictError here.
                if getattr(def_container, '_initializing', False) or autosync:
                    def_container.add_definition(definition)
            else:
                if autosync:
                    def_container.delfromproperty('definitions', old_def)
                    def_container.add_definition(definition)

        if autosync:
            # if not autosync, we still need this global constant for the
            # process_definitions_evolve step
            processdef_container.DEFINITIONS.clear()

        if getattr(def_container, '_initializing', False):
            del def_container._initializing

        transaction.commit()
    except ConflictError:
        # The first worker did the changes, simply abort to get the changes.
        transaction.abort()

    # After the restart of the application, we always need to resync
    # the node_definition attributes to be the node definition instances
    # currently in ZODB.
    for definition in def_container.definitions:
        for node in definition.nodes:
            for context in getattr(node, 'contexts', []):
                # context here is a class, we set the class attribute
                # node_definition to the current node definition in ZODB
                context.node_definition = node

    registry.notify(DatabaseOpenedWithRoot(root._p_jar.db()))
    manager.pop()
示例#38
0
文件: util.py 项目: ecreall/dace
 def tearDown(self, app):
     manager.pop()
     app._p_jar.close()
示例#39
0
文件: view.py 项目: Pylons/pyramid
    def invoke_exception_view(
        self, exc_info=None, request=None, secure=True, reraise=False
    ):
        """ Executes an exception view related to the request it's called upon.
        The arguments it takes are these:

        ``exc_info``

            If provided, should be a 3-tuple in the form provided by
            ``sys.exc_info()``.  If not provided,
            ``sys.exc_info()`` will be called to obtain the current
            interpreter exception information.  Default: ``None``.

        ``request``

            If the request to be used is not the same one as the instance that
            this method is called upon, it may be passed here.  Default:
            ``None``.

        ``secure``

            If the exception view should not be rendered if the current user
            does not have the appropriate permission, this should be ``True``.
            Default: ``True``.

        ``reraise``

            A boolean indicating whether the original error should be reraised
            if a :term:`response` object could not be created. If ``False``
            then an :class:`pyramid.httpexceptions.HTTPNotFound`` exception
            will be raised. Default: ``False``.

        If a response is generated then ``request.exception`` and
        ``request.exc_info`` will be left at the values used to render the
        response. Otherwise the previous values for ``request.exception`` and
        ``request.exc_info`` will be restored.

        .. versionadded:: 1.7

        .. versionchanged:: 1.9
           The ``request.exception`` and ``request.exc_info`` properties will
           reflect the exception used to render the response where previously
           they were reset to the values prior to invoking the method.

           Also added the ``reraise`` argument.

        """
        if request is None:
            request = self
        registry = getattr(request, 'registry', None)
        if registry is None:
            registry = get_current_registry()

        if registry is None:
            raise RuntimeError("Unable to retrieve registry")

        if exc_info is None:
            exc_info = sys.exc_info()

        exc = exc_info[1]
        attrs = request.__dict__
        context_iface = providedBy(exc)

        # clear old generated request.response, if any; it may
        # have been mutated by the view, and its state is not
        # sane (e.g. caching headers)
        with hide_attrs(request, 'response', 'exc_info', 'exception'):
            attrs['exception'] = exc
            attrs['exc_info'] = exc_info
            # we use .get instead of .__getitem__ below due to
            # https://github.com/Pylons/pyramid/issues/700
            request_iface = attrs.get('request_iface', IRequest)

            manager.push({'request': request, 'registry': registry})

            try:
                response = _call_view(
                    registry,
                    request,
                    exc,
                    context_iface,
                    '',
                    view_types=None,
                    view_classifier=IExceptionViewClassifier,
                    secure=secure,
                    request_iface=request_iface.combined,
                )
            except Exception:
                if reraise:
                    reraise_(*exc_info)
                raise
            finally:
                manager.pop()

        if response is None:
            if reraise:
                reraise_(*exc_info)
            raise HTTPNotFound

        # successful response, overwrite exception/exc_info
        attrs['exception'] = exc
        attrs['exc_info'] = exc_info
        return response
示例#40
0
    def invoke_subrequest(self, request, use_tweens=False):
        """
        Obtain a response object from the Pyramid application based on
        information in the ``request`` object provided.  The ``request``
        object must be an object that implements the Pyramid request
        interface (such as a :class:`pyramid.request.Request` instance).  If
        ``use_tweens`` is ``True``, the request will be sent to the
        :term:`tween` in the tween stack closest to the request ingress.  If
        ``use_tweens`` is ``False``, the request will be sent to the main
        router handler, and no tweens will be invoked.  This function also:
        
        - manages the threadlocal stack (so that
          :func:`~pyramid.threadlocal.get_current_request` and
          :func:`~pyramid.threadlocal.get_current_registry` work during a
          request)

        - Adds a ``registry`` attribute and a ``invoke_subrequest`` attribute
          (a callable) to the request object it's handed.

        - sets request extensions (such as those added via
          :meth:`~pyramid.config.Configurator.add_request_method` or
          :meth:`~pyramid.config.Configurator.set_request_property`) on the
          request it's passed.

        - causes a :class:`~pyramid.event.NewRequest` event to be sent at the
          beginning of request processing.

        - causes a :class:`~pyramid.event.ContextFound` event to be sent
          when a context resource is found.
          
        - causes a :class:`~pyramid.event.NewResponse` event to be sent when
          the Pyramid application returns a response.

        - Calls any :term:`response callback` functions defined within the
          request's lifetime if a response is obtained from the Pyramid
          application.

        - Calls any :term:`finished callback` functions defined within the
          request's lifetime.

        See also :ref:`subrequest_chapter`.
        """
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        threadlocals = {'registry':registry, 'request':request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)
        request.registry = registry
        request.invoke_subrequest = self.invoke_subrequest
        
        if use_tweens:
            handle_request = self.handle_request
        else:
            handle_request = self.orig_handle_request

        try:

            try:
                extensions = self.request_extensions
                if extensions is not None:
                    request._set_extensions(extensions)
                response = handle_request(request)
                has_listeners and notify(NewResponse(request, response))

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                return response

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#41
0
def threadlocals(request, dummy_request, registry):
    from pyramid.threadlocal import manager
    manager.push({'request': dummy_request, 'registry': registry})
    yield dummy_request
    manager.pop()
示例#42
0
def clear_manager(signum=None, frame=None):
    manager.pop()
示例#43
0
文件: router.py 项目: cjw296/pyramid
                        msg = why[0]
                    except:
                        msg = ''

                    # repoze.bfg.message docs-deprecated in Pyramid 1.0
                    environ['repoze.bfg.message'] = msg

                    result = view_callable(why, request)

                # process the response
                response = registry.queryAdapterOrSelf(result, IResponse)
                if response is None:
                    raise ValueError(
                        'Could not convert view return value "%s" into a '
                        'response object' % (result,))

                has_listeners and registry.notify(NewResponse(request,response))

                if request.response_callbacks:
                    request._process_response_callbacks(response)

            finally:
                if request is not None and request.finished_callbacks:
                    request._process_finished_callbacks()

            return response(request.environ, start_response)

        finally:
            manager.pop()

示例#44
0
文件: base.py 项目: festrade/assembl
 def fin():
     print "finalizer test_webrequest"
     # The request was not called
     manager.pop()
示例#45
0
 def fin():
     print("finalizer test_webrequest")
     # The request was not called
     manager.pop()
示例#46
0
 def tearDown(self, app):
     manager.pop()
     app._p_jar.close()
示例#47
0
    def invoke_subrequest(self, request, use_tweens=False):
        """
        Obtain a response object from the Pyramid application based on
        information in the ``request`` object provided.  The ``request``
        object must be an object that implements the Pyramid request
        interface (such as a :class:`pyramid.request.Request` instance).  If
        ``use_tweens`` is ``True``, the request will be sent to the
        :term:`tween` in the tween stack closest to the request ingress.  If
        ``use_tweens`` is ``False``, the request will be sent to the main
        router handler, and no tweens will be invoked.  This function also:
        
        - manages the threadlocal stack (so that
          :func:`~pyramid.threadlocal.get_current_request` and
          :func:`~pyramid.threadlocal.get_current_registry` work during a
          request)

        - Adds a ``registry`` attribute and a ``invoke_subrequest`` attribute
          (a callable) to the request object it's handed.

        - sets request extensions (such as those added via
          :meth:`~pyramid.config.Configurator.add_request_method` or
          :meth:`~pyramid.config.Configurator.set_request_property`) on the
          request it's passed.

        - causes a :class:`~pyramid.event.NewRequest` event to be sent at the
          beginning of request processing.

        - causes a :class:`~pyramid.event.ContextFound` event to be sent
          when a context resource is found.
          
        - Calls any :term:`response callback` functions defined within the
          request's lifetime if a response is obtained from the Pyramid
          application.

        - causes a :class:`~pyramid.event.NewResponse` event to be sent if a
          response is obtained.

        - Calls any :term:`finished callback` functions defined within the
          request's lifetime.

        See also :ref:`subrequest_chapter`.
        """
        registry = self.registry
        has_listeners = self.registry.has_listeners
        notify = self.registry.notify
        threadlocals = {'registry': registry, 'request': request}
        manager = self.threadlocal_manager
        manager.push(threadlocals)
        request.registry = registry
        request.invoke_subrequest = self.invoke_subrequest

        if use_tweens:
            handle_request = self.handle_request
        else:
            handle_request = self.orig_handle_request

        try:

            try:
                extensions = self.request_extensions
                if extensions is not None:
                    request._set_extensions(extensions)
                response = handle_request(request)

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                has_listeners and notify(NewResponse(request, response))

                return response

            finally:
                if request.finished_callbacks:
                    request._process_finished_callbacks()

        finally:
            manager.pop()
示例#48
0
 def closer():
     threadlocal_manager.pop()
示例#49
0
                    environ['repoze.bfg.message'] = msg

                    response = view_callable(why, request)

                # process the response

                has_listeners and registry.notify(
                    NewResponse(request, response))

                if request.response_callbacks:
                    request._process_response_callbacks(response)

                try:
                    headers = response.headerlist
                    app_iter = response.app_iter
                    status = response.status
                except AttributeError:
                    raise ValueError(
                        'Non-response object returned from view named %s '
                        '(and no renderer): %r' % (view_name, response))

            finally:
                if request is not None and request.finished_callbacks:
                    request._process_finished_callbacks()

            start_response(status, headers)
            return app_iter

        finally:
            manager.pop()
示例#50
0
 def get_response(self, app, catch_exc_info=True):
     try:
         super(PyramidWebTestRequest, app).get_response(
             catch_exc_info=catch_exc_info)
     finally:
         manager.pop()
示例#51
0
def postrun_end_request(sender, task, kwargs={}, **kw):
    logger.debug('signal postrun for %r' % task)
    request = kwargs.get('request', None)
    if request and request.finished_callbacks:
        request._process_finished_callbacks()
    threadlocal_manager.pop()
示例#52
0
def get_exported_file_by_func(func,
                              discussion,
                              lang='fr',
                              anon='false',
                              social_columns=True,
                              start=None,
                              end=None,
                              interval=None):
    """
    Helper function to get a specific export a file to be downloaded. Downloaded into the S3 bucket.
    To be used within a shell environment
    """
    import boto3
    from datetime import datetime
    from pyramid.threadlocal import manager
    from assembl.lib.config import get

    class FakeContext(object):
        def __init__(self, discussion):
            self._instance = discussion

    class FakeLocalizer(object):
        def translate(self, message):
            return message

    class FakeRequest(object):
        authenticated_userid = None

        def __init__(self, discussion, lang='fr'):
            self.discussion = discussion
            self.GET = {}
            self.context = FakeContext(discussion)
            self.localizer = FakeLocalizer()
            self.locale_name = lang
            self.matchdict = {'discussion_id': discussion.id}

    r = FakeRequest(discussion, lang=lang)
    manager.push({'request': r})
    r.GET['as_buffer'] = 'true'
    r.GET['lang'] = lang
    r.GET['anon'] = anon
    if not social_columns:
        r.GET['no_extra_columns'] = 'true'
    if start:
        r.GET['start'] = start
    if end:
        r.GET['end'] = end
    if interval:
        r.GET['interval'] = interval
    try:
        output, mime_type = func(r)
    finally:
        manager.pop()

    account_number = get('aws_client', None)
    _now = datetime.utcnow()
    func_name = func.__name__
    name = "%s-%s" % (func_name, _now.isoformat())

    def write(o):
        with open(name, mode='w') as f:
            f.write(o)

    if account_number:
        try:
            s3 = boto3.resource('s3')
            bucket = s3.Bucket('assembl-data-%s' % account_number.strip())
            bucket.put_object(Body=output, ContentType=mime_type, Key=name)
            print("The file was uploaded as %s" % name)
        except Exception:
            write(output)
            print("Failed to upload, creating file locally with name %s" %
                  name)
    else:
        write(output)
        print(
            "The account number could not be found. Saving the file to disk as %s"
            % name)
示例#53
0
文件: conftest.py 项目: kidaa/encoded
def threadlocals(request, dummy_request, registry):
    from pyramid.threadlocal import manager
    manager.push({'request': dummy_request, 'registry': registry})
    yield dummy_request
    manager.pop()
示例#54
0
    def run(self):
        logger.debug("Started thread worker.")
        tasks = GetQueue("tasks")
        results = GetQueue("results", query=True)
        manager_pop = 0
        while True:
            self.waiting = True
            self.task = None
            # logger.debug("waiting for a task.")
            wrk = getUtility(IWorker, name="queue")
            if logger.isEnabledFor(logging.DEBUG):
                logger.debug(
                    "Queue %s has %d tasks. Workers occupied: %d; free:%d"
                    % (tasks, tasks.qsize(), wrk.processing(), wrk.waiting())
                )
                logger.debug("Singleton tasks: {}".format(tasks.singletons))
                logger.debug("Active Tasks:{}".format(wrk.tasks()))
            del wrk
            self.task = task = tasks.get()
            task.locks = []

            # Setup a context (request and registry)
            for _ in range(manager_pop):
                manager.pop()
            manager_pop = 0
            print(
                """

                       1: {}

                       """.format(
                    task.context_thread_data
                )
            )
            if task.context_thread_data:
                print(
                    """

                       2: {}

                       """.format(
                        task.context_thread_data
                    )
                )
                manager.push(task.context_thread_data)
                manager_pop += 1

            self.waiting = False
            logger.info("got a task %s" % task)
            if ITerminationTask.providedBy(task):
                task_done()
                logger.info("finished task %s" % task)
                return
            task.phase = "prepare"
            try:
                task.prepare()
            except BaseException as e:
                # traceback.print_exception(type, value, err.__traceback__)
                f = io.StringIO()
                traceback.print_exc(file=f)
                logger.error(f.getvalue())
                logger.error("{!r}; cancelling main task run.".format(e))
                task.task_done()
                logger.info("finished task %s (exception)" % task)
                self.release_locks(task)
                continue
            task.phase = "run"
            try:
                rc = task.run()
            except BaseException as e:
                f = io.StringIO()
                traceback.print_exc(file=f)
                logger.error(f.getvalue())
                logger.error("{!r}; some code did not run correctly, proceed with finalization.".format(e))
                rc = None
                self.release_locks(task)
            if rc != None and results != None:
                task.result = rc
                results.put(task)
            task.phase = "finalize"
            try:
                task.finalize()
            except BaseException as e:
                f = io.StringIO()
                traceback.print_exc(file=f)
                logger.error(f.getvalue())
                logger.error("{!r}; finalization failde try next task.".format(e))
                self.release_locks(task)
            task.phase = None
            tasks.task_done()
            logger.info("finished task %s (complete)" % task)