Exemplo n.º 1
0
def install(app, *a, **kw):
    """Install Aspen into a Flask app.
    """
    arp = kw.pop('_aspen_request_processor', None)
    if arp is None:

        # Infer project and www roots.
        default_project_root = None
        parent = inspect.stack()[1]
        if parent:
            default_project_root = os.path.dirname(parent[1])
        kw['project_root'] = kw.get('project_root', default_project_root)
        if kw['project_root'] is None:
            raise NoProjectRoot()
        kw['www_root'] = kw.get('www_root', os.path.join(kw['project_root'], 'www'))

        # Instantiate.
        arp = RequestProcessor(*a, **kw)

    # Configure.
    arp.renderer_factories['jinja2'] = Jinja2RendererFactory(arp)
    arp.default_renderers_by_media_type['text/html'] = 'jinja2'

    # Bypass add_url_rule because it forces us to constrain methods.
    app.url_map.add(app.url_rule_class('/', endpoint='aspen', methods=None))
    app.view_functions['aspen'] = FlaskView(app, arp)
    app._aspen_request_processor = arp

    return arp
Exemplo n.º 2
0
def test_ConfigurationError_NOT_raised_if_no_cwd_but_do_have__www_root(harness):
    project_root = harness.fs.project.root
    www_root = harness.fs.www.root
    with chdir(project_root):
        os.rmdir(project_root)
        rp = RequestProcessor(www_root=www_root)
        assert rp.www_root == www_root
Exemplo n.º 3
0
def test_ConfigurationError_NOT_raised_if_no_cwd_but_do_have__www_root(
        harness):
    foo = os.getcwd()
    os.chdir(harness.fs.project.resolve(''))
    os.rmdir(os.getcwd())
    rp = RequestProcessor(www_root=foo)
    assert rp.www_root == foo
Exemplo n.º 4
0
    def __init__(self, **kwargs):
        #: An Aspen :class:`~aspen.request_processor.RequestProcessor` instance.
        self.request_processor = RequestProcessor(**kwargs)

        pando_resources_dir = os.path.join(PANDO_DIR, 'www')
        self.request_processor.resource_directories.append(pando_resources_dir)

        pando_chain = StateChain.from_dotted_name('pando.state_chain')
        pando_chain.functions = [
            getattr(f, 'placeholder_for', f) for f in pando_chain.functions
        ]
        #: The chain of functions used to process an HTTP request, imported from
        #: :mod:`pando.state_chain`.
        self.state_chain = pando_chain

        # configure from defaults and kwargs
        defaults = [(k, v) for k, v in DefaultConfiguration.__dict__.items()
                    if k[0] != '_']
        for name, default in sorted(defaults):
            if name in kwargs:
                self.__dict__[name] = kwargs[name]
            else:
                self.__dict__[name] = copy(default)

        # add ourself to the initial context of simplates
        Simplate.defaults.initial_context['website'] = self

        # load bodyparsers
        #: Mapping of content types to parsing functions.
        self.body_parsers = {
            "application/x-www-form-urlencoded": body_parsers.formdata,
            "multipart/form-data": body_parsers.formdata,
            self.request_processor.media_type_json: body_parsers.jsondata
        }
Exemplo n.º 5
0
    def __init__(self, **kwargs):
        """Takes configuration in kwargs.
        """
        #: An Aspen :class:`~aspen.request_processor.RequestProcessor` instance.
        self.request_processor = RequestProcessor(**kwargs)

        pando_chain = Algorithm.from_dotted_name('pando.state_chain')
        pando_chain.functions = [
            getattr(f, 'placeholder_for', f) for f in pando_chain.functions
        ]
        #: The chain of functions used to process an HTTP request, imported from
        #: :mod:`pando.state_chain`.
        self.state_chain = pando_chain

        # copy aspen's config variables, for backward compatibility
        extra = ['typecasters']
        for key in list(ASPEN_KNOBS) + extra:
            self.__dict__[key] = self.request_processor.__dict__[key]
        for key in ('renderer_factories', 'default_renderers_by_media_type'):
            self.__dict__[key] = Simplate.__dict__[key]

        # load our own config variables
        configure(KNOBS, self.__dict__, 'PANDO_', kwargs)

        # add ourself to the initial context of simplates
        Simplate.defaults.initial_context['website'] = self

        # load bodyparsers
        #: Mapping of content types to parsing functions.
        self.body_parsers = {
            "application/x-www-form-urlencoded": body_parsers.formdata,
            "multipart/form-data": body_parsers.formdata,
            self.media_type_json: body_parsers.jsondata
        }
Exemplo n.º 6
0
def test_defaults_to_defaults(harness):
    rp = RequestProcessor()
    actual = (rp.project_root, rp.www_root, rp.changes_reload,
              rp.charset_static, rp.encode_output_as, rp.indices,
              rp.media_type_default, rp.media_type_json, rp.renderer_default)
    expected = (None, os.getcwd(), False, None, 'UTF-8', [
        'index.html', 'index.json', 'index', 'index.html.spt',
        'index.json.spt', 'index.spt'
    ], 'text/plain', 'application/json', 'stdlib_percent')
    assert actual == expected
Exemplo n.º 7
0
def test_processor_sees_root_option(harness):
    rp = RequestProcessor(www_root=harness.fs.project.resolve(''))
    expected = harness.fs.project.root
    actual = rp.www_root
    assert actual == expected
Exemplo n.º 8
0
def test_www_root_defaults_to_cwd():
    p = RequestProcessor()
    expected = os.path.realpath(os.getcwd())
    actual = p.www_root
    assert actual == expected
Exemplo n.º 9
0
def test_configuration_ignores_blank_indexfilenames():
    rp = RequestProcessor(indices='index.html,, ,default.html')
    assert rp.indices[0] == 'index.html'
    assert rp.indices[1] == 'default.html'
    assert len(rp.indices) == 2, "Too many indexfile entries"
Exemplo n.º 10
0
def test_basic(harness):
    with chdir(harness.fs.www.root):
        rp = RequestProcessor()
        expected = os.getcwd()
        actual = rp.www_root
        assert actual == expected
Exemplo n.º 11
0
def test_basic():
    rp = RequestProcessor()
    expected = os.getcwd()
    actual = rp.www_root
    assert actual == expected