Exemplo n.º 1
0
def test_convenience_clear_not_initialized():
    # This test is put near the top of this module, or at least before
    # the very first time ``init_needed()`` is called.
    dummy = get_needed()
    with pytest.raises(NotImplementedError):
        dummy.clear()
    with pytest.raises(NotImplementedError):
        clear_needed()

    # Initialize a needed resources object.
    needed = init_needed()
    assert get_needed() == needed
    assert thread_local_needed_data.__dict__[NEEDED] == needed

    # Clear it.
    del_needed()

    # It is gone, really.
    with pytest.raises(KeyError):
        thread_local_needed_data.__dict__[NEEDED]

    # Clearing it again is OK.
    del_needed()

    # get_needed still work, dummy-style.
    dummy2 = get_needed()
    assert dummy2 != needed
    with pytest.raises(NotImplementedError):
        dummy.clear()
    with pytest.raises(NotImplementedError):
        clear_needed()
Exemplo n.º 2
0
def test_convenience_clear_not_initialized():
    # This test is put near the top of this module, or at least before
    # the very first time ``init_needed()`` is called.
    dummy = get_needed()
    with pytest.raises(NotImplementedError):
        dummy.clear()
    with pytest.raises(NotImplementedError):
        clear_needed()

    # Initialize a needed resources object.
    needed = init_needed()
    assert get_needed() == needed
    assert thread_local_needed_data.__dict__[NEEDED] == needed

    # Clear it.
    del_needed()

    # It is gone, really.
    with pytest.raises(KeyError):
        thread_local_needed_data.__dict__[NEEDED]

    # Clearing it again is OK.
    del_needed()

    # get_needed still work, dummy-style.
    dummy2 = get_needed()
    assert dummy2 != needed
    with pytest.raises(NotImplementedError):
        dummy.clear()
    with pytest.raises(NotImplementedError):
        clear_needed()
Exemplo n.º 3
0
def test_resource_need_should_pass_slots_to_needed():
    import fanstatic
    lib = Library('lib', '')
    c = Resource(lib, 'c.js')
    slot = Slot(lib, '.js', depends=[c])
    a = Resource(lib, 'a.js', depends=[slot])
    b = Resource(lib, 'b.js', depends=[c])
    needed = fanstatic.init_needed()
    try:
        a.need({slot: c})
    finally:
        fanstatic.del_needed()
    assert slot in needed._slots
Exemplo n.º 4
0
def test_resource_need_should_pass_slots_to_needed():
    import fanstatic

    lib = Library("lib", "")
    c = Resource(lib, "c.js")
    slot = Slot(lib, ".js", depends=[c])
    a = Resource(lib, "a.js", depends=[slot])
    b = Resource(lib, "b.js", depends=[c])
    needed = fanstatic.init_needed()
    try:
        a.need({slot: c})
    finally:
        fanstatic.del_needed()
    assert slot in needed._slots
Exemplo n.º 5
0
    def __call__(self, request):

        # publisher
        if len(request.path_info.split(self.trigger)) > 1:
            path_info = request.path_info
            ignored = request.path_info_pop()
            while ignored != self.publisher_signature:
                ignored = request.path_info_pop()
            response = request.get_response(self.publisher)
            # forward to handler if the resource could not be found
            if response.status_int == 404:
                request.path_info = path_info
                return self.handler(request)
            return response

        # injector
        needed = fanstatic.init_needed(**self.config)
        if self.use_application_uri and not needed.has_base_url():
            base_url = wsgiref.util.application_uri(request.environ)
            # remove trailing slash for fanstatic
            needed.set_base_url(base_url.rstrip('/'))
        request.environ[fanstatic.NEEDED] = needed

        response = self.handler(request)

        if not (response.content_type and
                response.content_type.lower() in ['text/html',
                                                  'text/xml']):
            fanstatic.del_needed()
            return response

        if needed.has_resources():
            # Using response.body may cause a UnicodeDecodeError if some of
            # the libraries contain unicode strings. So it's best to pass
            # unicode_body directly, so the regexps won't fail.
            if self.injector is not None:
                result = self.injector(response.unicode_body,
                                       needed, request, response)
            else:
                result = needed.render_topbottom_into_html(
                    response.unicode_body)
            response.body = ''
            response.write(result)
        fanstatic.del_needed()
        return response
Exemplo n.º 6
0
    def __call__(self, request):

        # publisher
        if len(request.path_info.split(self.trigger)) > 1:
            path_info = request.path_info
            ignored = request.path_info_pop()
            while ignored != self.publisher_signature:
                ignored = request.path_info_pop()
            response = request.get_response(self.publisher)
            # forward to handler if the resource could not be found
            if response.status_int == 404:
                request.path_info = path_info
                return self.handler(request)
            return response

        # injector
        needed = fanstatic.init_needed(**self.config)
        if self.use_application_uri and not needed.has_base_url():
            base_url = wsgiref.util.application_uri(request.environ)
            # remove trailing slash for fanstatic
            needed.set_base_url(base_url.rstrip('/'))
        request.environ[fanstatic.NEEDED] = needed

        response = self.handler(request)

        if not (response.content_type and
                response.content_type.lower() in ['text/html',
                                                  'text/xml']):
            fanstatic.del_needed()
            return response

        if needed.has_resources():
            if self.injector is not None:
                result = self.injector(response.body,
                                       needed, request, response)
            else:
                result = needed.render_topbottom_into_html(response.body)
            try:
                response.text = ''
            except TypeError:
                response.body = b''
            response.write(result)
        fanstatic.del_needed()
        return response
Exemplo n.º 7
0
            def decorated(request):
                # injector
                needed = fanstatic.init_needed(**self.config)
                request.environ[fanstatic.NEEDED] = needed

                self.need_fanstatic()
                response =  view(request)
                if not (response.content_type and
                        response.content_type.lower() in ['text/html',
                                                          'text/xml']):
                    fanstatic.del_needed()
                    return response

                if needed.has_resources():
                    result = needed.render_topbottom_into_html(response.body)
                    response.body = ''
                    response.write(result)
                fanstatic.del_needed()
                return response
Exemplo n.º 8
0
    def __call__(self, environ, start_response):
        request = webob.Request(environ)
        # We only continue if the request method is appropriate.
        if not request.method in ['GET', 'POST']:
            return self.app(environ, start_response)

        # Initialize a needed resources object.
        # XXX this will set the needed on the thread local data, even
        # if the wrapped framework only gets the needed from the WSGI
        # environ.
        needed = fanstatic.init_needed(
            script_name=request.environ.get('SCRIPT_NAME'), **self.config)

        # Make sure the needed resource object is put in the WSGI
        # environment as well, for frameworks that choose to use it
        # from there.
        request.environ[fanstatic.NEEDED] = needed

        # Get the response from the wrapped application:
        response = request.get_response(self.app)

        # We only continue if the content-type is appropriate.
        if not (response.content_type and
                response.content_type.lower() in CONTENT_TYPES):
            # Clean up after our behinds.
            fanstatic.del_needed()
            return response(environ, start_response)

        # The wrapped application may have `needed` resources.
        if needed.has_resources():
            # Can't use response.text because there might not be any
            # charset. body is not unicode.
            result = needed.render_topbottom_into_html(response.body)
            # Reset the body...
            response.body = b''
            # Write will propely unfolder the previous application and
            # call close. Setting response.text or response.body won't do it.
            response.write(result)

        # Clean up after our behinds.
        fanstatic.del_needed()

        return response(environ, start_response)
Exemplo n.º 9
0
    def __call__(self, request):

        # publisher
        if len(request.path_info.split(self.trigger)) > 1:
            path_info = request.path_info
            ignored = request.path_info_pop()
            while ignored != self.publisher_signature:
                ignored = request.path_info_pop()
            response = request.get_response(self.publisher)
            # forward to handler if the resource could not be found
            if response.status_int == 404:
                request.path_info = path_info
                return self.handler(request)
            return response

        # injector
        needed = fanstatic.init_needed(**self.config)
        if self.use_application_uri and not needed.has_base_url():
            base_url = wsgiref.util.application_uri(request.environ)
            # remove trailing slash for fanstatic
            needed.set_base_url(base_url.rstrip("/"))
        request.environ[fanstatic.NEEDED] = needed

        response = self.handler(request)

        if not (response.content_type and response.content_type.lower() in ["text/html", "text/xml"]):
            fanstatic.del_needed()
            return response

        if needed.has_resources():
            if self.injector is not None:
                result = self.injector(response.body, needed, request, response)
            else:
                result = needed.render_topbottom_into_html(response.body)
            try:
                response.text = ""
            except TypeError:
                response.body = ""
            response.write(result)
        fanstatic.del_needed()
        return response
Exemplo n.º 10
0
    def __call__(self, request):
        # We only continue if the request method is appropriate.
        if not request.method in ['GET', 'POST']:
            return request.get_response(self.app)

        # Initialize a needed resources object.
        # XXX this will set the needed on the thread local data, even
        # if the wrapped framework only gets the needed from the WSGI
        # environ.
        needed = fanstatic.init_needed(
            script_name=request.environ.get('SCRIPT_NAME'), **self.config)

        # Make sure the needed resource object is put in the WSGI
        # environment as well, for frameworks that choose to use it
        # from there.
        request.environ[fanstatic.NEEDED] = needed

        # Get the response from the wrapped application:
        response = request.get_response(self.app)

        # We only continue if the content-type is appropriate.
        if not (response.content_type
                and response.content_type.lower() in CONTENT_TYPES):
            # Clean up after our behinds.
            fanstatic.del_needed()
            return response

        # The wrapped application may have `needed` resources.
        if needed.has_resources():
            result = needed.render_topbottom_into_html(response.unicode_body)
            # Reset the body...
            response.body = ''
            # And write the result. The `write` method handles unicode results.
            response.write(result)

        # Clean up after our behinds.
        fanstatic.del_needed()

        return response
Exemplo n.º 11
0
    def __call__(self, request):
        needed = fanstatic.init_needed(**self.config)
        if self.use_application_uri and not needed.has_base_url():
            base_url = wsgiref.util.application_uri(request.environ)
            # remove trailing slash for fanstatic
            needed.set_base_url(base_url.rstrip('/'))
        request.environ[fanstatic.NEEDED] = needed

        try:
            response = self.handler(request)

            if response.content_type \
                   and response.content_type.lower() in CONTENT_TYPES \
                   and needed.has_resources():

                result = self.injector(response.body, needed, request, response)
                response.body = ''
                response.write(result)

            return response
        finally:
            fanstatic.del_needed()
Exemplo n.º 12
0
    def __call__(self, request):
        # We only continue if the request method is appropriate.
        if not request.method in ['GET', 'POST']:
            return request.get_response(self.app)

        # Initialize a needed resources object.
        # XXX this will set the needed on the thread local data, even
        # if the wrapped framework only gets the needed from the WSGI
        # environ.
        needed = fanstatic.init_needed(**self.config)

        # Make sure the needed resource object is put in the WSGI
        # environment as well, for frameworks that choose to use it
        # from there.
        request.environ[fanstatic.NEEDED] = needed

        # Get the response from the wrapped application:
        response = request.get_response(self.app)

        # We only continue if the content-type is appropriate.
        if not (response.content_type and
                response.content_type.lower() in ['text/html', 'text/xml']):
            # Clean up after our behinds.
            fanstatic.del_needed()
            return response

        # The wrapped application may have `needed` resources.
        if needed.has_resources():
            result = needed.render_topbottom_into_html(response.body)
            # Reset the body...
            response.body = ''
            # And write the result. The `write` method handles unicode results.
            response.write(result)

        # Clean up after our behinds.
        fanstatic.del_needed()

        return response
Exemplo n.º 13
0
    def __call__(self, request):
        needed = fanstatic.init_needed(**self.config)
        if self.use_application_uri and not needed.has_base_url():
            base_url = wsgiref.util.application_uri(request.environ)
            # remove trailing slash for fanstatic
            needed.set_base_url(base_url.rstrip('/'))
        request.environ[fanstatic.NEEDED] = needed

        try:
            response = self.handler(request)

            if response.content_type \
                   and response.content_type.lower() in CONTENT_TYPES \
                   and needed.has_resources():

                result = self.injector(response.body, needed, request,
                                       response)
                response.text = six.u('')
                response.write(result)

            return response
        finally:
            fanstatic.del_needed()
Exemplo n.º 14
0
 def teardown_request(self, *args):
   del_needed()
Exemplo n.º 15
0
 def teardown_request(self, *args):
     del_needed()