示例#1
0
def set_defaults_from_config(
    context: click.Context,
    param: click.Parameter,
    value: Union[str, int],
) -> Optional[Path]:
    paths_supplied_via_cli = context.params.get("path")

    search_paths = paths_supplied_via_cli
    if not search_paths:
        search_paths = (".", )

    if not context.default_map:
        context.default_map = {"path": (".", )}

    project_root = find_project_root([Path(path) for path in search_paths])
    if project_root:
        context.params["project_root"] = project_root
    else:
        context.params["project_root"] = None
        context.params["config_path"] = None
        return Path.cwd()

    file_config = read_config_toml(project_root, _CONFIG_FILE)
    validate_config_toml(file_config)

    if file_config:
        config_path: Optional[Path] = project_root / _CONFIG_FILE
    else:
        config_path = None

    context.params["config_path"] = config_path

    multi_defaults = apply_multi_defaults(file_config, context.params)
    file_config.update(multi_defaults)

    # Paths supplied via the CLI should be treated as relative to pwd
    # However, paths supplied via the pyproject.toml should be relative
    # to the directory that file is contained in.
    path_config_keys = ["path", "exclude"]
    for conf_key, paths in file_config.items():
        if conf_key in path_config_keys:
            assert isinstance(
                paths, list), 'value of "path" and "exclude" must be list'
            relative_path_strs = []
            for path_str in paths:
                relative_path_strs.append(str((project_root / path_str)))
            file_config[conf_key] = tuple(relative_path_strs)

    context.default_map.update(file_config)

    return config_path
示例#2
0
def _(root_file, project):
    root = find_project_root([project / "a/b/c", project / "a/d"])
    assert root.resolve() == project.resolve()
    assert (root / root_file).exists()
示例#3
0
def _():
    project_root = find_project_root([])
    assert project_root is None