Skip to content

get_app_name

fastagency.cli.discover.get_app_name #

get_app_name(
    *,
    mod_data: ModuleData,
    app_name: Union[str, None] = None
) -> Tuple[str, FastAgency]
Source code in fastagency/cli/discover.py
def get_app_name(  # noqa: C901
    *, mod_data: ModuleData, app_name: Union[str, None] = None
) -> "Tuple[str, FastAgency]":
    try:
        mod = importlib.import_module(mod_data.module_import_str)  # nosemgrep
    except (ImportError, ValueError) as e:
        logger.error(f"Import error: {e}")
        logger.warning(
            "Ensure all the package directories have an [blue]__init__.py[/blue] file"
        )
        raise
    if not FastAgency:  # type: ignore[truthy-function]
        raise FastAgencyCLIError(
            "Could not import FastAgency, try running 'pip install fastagency'"
        ) from None
    object_names = dir(mod)
    object_names_set = set(object_names)
    if app_name:
        if app_name not in object_names_set:
            raise FastAgencyCLIError(
                f"Could not find app name {app_name} in {mod_data.module_import_str}"
            )
        app = getattr(mod, app_name)
        if not isinstance(app, FastAgency):
            raise FastAgencyCLIError(
                f"The app name {app_name} in {mod_data.module_import_str} doesn't seem to be a FastAgency app"
            )
        return app_name, app
    for preferred_name in ["app", "api"]:
        if preferred_name in object_names_set:
            obj = getattr(mod, preferred_name)
            if isinstance(obj, FastAgency):
                return preferred_name, obj
    for name in object_names:
        obj = getattr(mod, name)
        if isinstance(obj, FastAgency):
            return name, obj
    raise FastAgencyCLIError("Could not find FastAgency app in module, try using --app")