Beispiel #1
0
def read_cfg() -> BaseConfig:
    conf = BaseConfig()

    git_project_root = git.find_git_project_root()
    if not git_project_root:
        raise NotAGitProjectError()

    cfg_paths = (path / Path(filename)
                 for path in [Path("."), git_project_root]
                 for filename in defaults.config_files)
    for filename in cfg_paths:
        if not filename.exists():
            continue

        with open(filename, "r") as f:
            data: str = f.read()

        _conf: Union[TomlConfig, IniConfig]
        if "toml" in filename.suffix:
            _conf = TomlConfig(data=data, path=filename)
        else:
            _conf = IniConfig(data=data, path=filename)

        if _conf.is_empty_config:
            continue
        else:
            conf = _conf
            break

    if not conf.path:
        global_conf = load_global_conf()
        if global_conf:
            conf = global_conf

    return conf
Beispiel #2
0
def read_cfg() -> BaseConfig:
    conf = BaseConfig()

    git_project_root = git.find_git_project_root()
    cfg_search_paths = [Path(".")]
    if git_project_root:
        cfg_search_paths.append(git_project_root)

    cfg_paths = (path / Path(filename) for path in cfg_search_paths
                 for filename in defaults.config_files)
    for filename in cfg_paths:
        if not filename.exists():
            continue

        _conf: Union[TomlConfig, JsonConfig, YAMLConfig]

        with open(filename, "r") as f:
            data: str = f.read()

        if "toml" in filename.suffix:
            _conf = TomlConfig(data=data, path=filename)
        elif "json" in filename.suffix:
            _conf = JsonConfig(data=data, path=filename)
        elif "yaml" in filename.suffix:
            _conf = YAMLConfig(data=data, path=filename)

        if _conf.is_empty_config:
            continue
        else:
            conf = _conf
            break

    return conf
Beispiel #3
0
def read_cfg() -> BaseConfig:
    conf = BaseConfig()

    git_project_root = git.find_git_project_root()
    if not git_project_root:
        out.error(
            "fatal: not a git repository (or any of the parent directories): .git"
        )
        raise SystemExit(NOT_A_GIT_PROJECT)

    allowed_cfg_files = defaults.config_files
    cfg_paths = (
        path / Path(filename)
        for path in [Path("."), git_project_root]
        for filename in allowed_cfg_files
    )
    for filename in cfg_paths:
        if not filename.exists():
            continue

        with open(filename, "r") as f:
            data: str = f.read()

        if "toml" in filename.suffix:
            _conf = TomlConfig(data=data, path=filename)
        else:
            warnings.warn(
                ".cz, setup.cfg, and .cz.cfg will be deprecated "
                "in next major version. \n"
                'Please use "pyproject.toml", ".cz.toml" instead'
            )
            _conf = IniConfig(data=data, path=filename)

        if _conf.is_empty_config:
            continue
        else:
            conf = _conf
            break

    if not conf.path:
        global_conf = load_global_conf()
        if global_conf:
            conf = global_conf

    return conf
Beispiel #4
0
def read_cfg() -> BaseConfig:
    conf = BaseConfig()

    git_project_root = git.find_git_project_root()
    if not git_project_root:
        out.error(
            "fatal: not a git repository (or any of the parent directories): .git"
        )
        raise SystemExit(NOT_A_GIT_PROJECT)

    cfg_paths = (
        path / Path(filename)
        for path in [Path("."), git_project_root]
        for filename in defaults.config_files
    )
    for filename in cfg_paths:
        if not filename.exists():
            continue

        with open(filename, "r") as f:
            data: str = f.read()

        _conf: Union[TomlConfig, IniConfig]
        if "toml" in filename.suffix:
            _conf = TomlConfig(data=data, path=filename)
        else:
            _conf = IniConfig(data=data, path=filename)

        if _conf.is_empty_config:
            continue
        else:
            conf = _conf
            break

    if not conf.path:
        global_conf = load_global_conf()
        if global_conf:
            conf = global_conf

    return conf
Beispiel #5
0
def test_find_git_project_root(tmpdir):
    assert git.find_git_project_root() == Path(os.getcwd())

    with tmpdir.as_cwd() as _:
        assert git.find_git_project_root() is None