forked from commitizen-tools/commitizen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
58 lines (44 loc) · 1.61 KB
/
__init__.py
File metadata and controls
58 lines (44 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from __future__ import annotations
from pathlib import Path
from commitizen import defaults, git
from commitizen.exceptions import ConfigFileIsEmpty, ConfigFileNotFound
from .base_config import BaseConfig
from .json_config import JsonConfig
from .toml_config import TomlConfig
from .yaml_config import YAMLConfig
def read_cfg(filepath: str | None = None) -> BaseConfig:
conf = BaseConfig()
if filepath is not None:
if not Path(filepath).exists():
raise ConfigFileNotFound()
cfg_paths = (path for path in (Path(filepath),))
else:
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: TomlConfig | JsonConfig | YAMLConfig
with open(filename, "rb") as f:
data: bytes = 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 filepath is not None and _conf.is_empty_config:
raise ConfigFileIsEmpty()
elif _conf.is_empty_config:
continue
else:
conf = _conf
break
return conf