Skip to content

fastack.utils

import_attr(module)

Import attributes from a module.

Parameters:

Name Type Description Default
module str

Module name (e.g. "os.path")

required

Returns:

Type Description
Any

Imported attributes

Source code in fastack/utils.py
def import_attr(module: str) -> Any:
    """
    Import attributes from a module.

    Args:
        module: Module name (e.g. "os.path")

    Returns:
        Any: Imported attributes
    """

    package, attr = module.rsplit(".", 1)
    mod = import_module(package)
    return getattr(mod, attr)

load_app(raise_error=True)

Load Fastack app from environment variable FASTACK_APP.

Source code in fastack/utils.py
def load_app(raise_error: bool = True) -> Optional["Fastack"]:
    """
    Load Fastack app from environment variable ``FASTACK_APP``.
    """

    cwd = os.getcwd()
    sys.path.insert(0, cwd)
    app = None
    try:
        src = os.environ.get("FASTACK_APP", "app.main.app")
        app: "Fastack" = import_attr(src)  # type: ignore[no-redef]

    except (ImportError, AttributeError) as e:
        if raise_error:
            infoMsg = '\n  If you use the "fastack" command, you need to be in the root of the project directory '
            infoMsg += "or set the location of the app via the environment:\n"
            infoMsg += "    $ export FASTACK_APP=app.main.app\n"
            infoMsg += "    $ fastack runserver\n\n"
            infoMsg += "  I hope this helps!"
            raise RuntimeError(infoMsg) from e

    return app

lookup_exception_handler(exception_handlers, exc_or_status)

Lookup exception handler. Taken from starlette.exceptions.ExceptionMiddleware.

Source code in fastack/utils.py
def lookup_exception_handler(
    exception_handlers: Dict[Union[int, Type[Exception]], Callable],
    exc_or_status: Union[Exception, int],
) -> Optional[Callable]:
    """
    Lookup exception handler.
    Taken from starlette.exceptions.ExceptionMiddleware.
    """

    handler = None
    if isinstance(exc_or_status, Exception):
        for cls in type(exc_or_status).__mro__:
            if cls in exception_handlers:
                handler = exception_handlers[cls]
                break
    else:
        handler = exception_handlers.get(exc_or_status)

    return handler

url_for(name, **params)

Generate absolute URL for an endpoint.

Parameters:

Name Type Description Default
name str

Name of the endpoint.

required
params Dict[str, Any]

Can be path parameters or query parameters.

{}
Source code in fastack/utils.py
def url_for(name: str, **params: Dict[str, Any]) -> str:
    """
    Generate absolute URL for an endpoint.

    Args:
        name: Name of the endpoint.
        params: Can be path parameters or query parameters.
    """

    path_params = {}
    routes: List[APIRoute] = request.app.routes
    for route in routes:
        if route.name == name:
            paths = list(route.param_convertors.keys())
            for path in paths:
                if path in params:
                    path_value = params.pop(path)
                    path_params[path] = path_value
            break

    url = request.url_for(name, **path_params)
    parsed = list(urlparse(url))
    query = urlencode(params, doseq=True)
    parsed[4] = query
    return urlunparse(parsed)

Last update: January 17, 2022 13:26:42