Beispiel #1
0
    def get_controller_handler_pattern(
        self, controller_type: Type, route: RegisteredRoute
    ) -> bytes:
        """
        Returns the full pattern to be used for a route handler,
        defined as controller method.
        """
        base_route = getattr(controller_type, "route", None)

        if base_route is not None:
            if callable(base_route):
                value = base_route()
            elif isinstance(base_route, (str, bytes)):
                value = base_route
            else:
                raise RuntimeError(
                    f"Invalid controller `route` attribute. "
                    f"Controller `{controller_type.__name__}` "
                    f"has an invalid route attribute: it should "
                    f"be callable, or str, or bytes."
                )

            if value:
                return ensure_bytes(join_fragments(value, route.pattern))
        return ensure_bytes(route.pattern)
Beispiel #2
0
def get_files_list_html_response(
    template: str,
    parent_folder_path: str,
    contents: Sequence[Dict[str, str]],
    root_path: str,
) -> Response:
    info_lines = []
    for item in contents:
        rel_path = item.get("rel_path")
        assert rel_path is not None
        full_rel_path = html.escape(
            join_fragments(root_path, parent_folder_path, rel_path))
        info_lines.append(
            f'<li><a href="/{full_rel_path}">{rel_path}</a></li>')
    info = "".join(info_lines)
    p = []
    whole_p = [root_path]
    for fragment in parent_folder_path.split("/"):
        if fragment:
            whole_p.append(html.escape(fragment))
            fragment_path = "/".join(whole_p)
            p.append(f'<a href="/{fragment_path}">{html.escape(fragment)}</a>')

    # TODO: use chunked encoding here, yielding HTML fragments
    return Response(
        200,
        content=HtmlContent(
            template.format_map({
                "path": "/".join(p),
                "info": info
            })),
    )
Beispiel #3
0
 def route(cls) -> Optional[str]:
     cls_name = cls.class_name()
     cls_version = cls.version() or ""
     if cls_version and cls_name.endswith(cls_version.lower()):
         cls_name = cls_name[:-len(cls_version)]
     return join_fragments("api", cls_version, cls_name)
Beispiel #4
0
def test_join_url_fragments(fragments: Sequence[BytesOrStr], expected_value: str):
    joined = join_fragments(*fragments)
    assert joined == expected_value