-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathtoml_config.py
More file actions
70 lines (53 loc) · 2.16 KB
/
toml_config.py
File metadata and controls
70 lines (53 loc) · 2.16 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
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations
import os
from typing import TYPE_CHECKING
from tomlkit import TOMLDocument, exceptions, parse, table
from commitizen.exceptions import InvalidConfigurationError
from .base_config import BaseConfig
if TYPE_CHECKING:
import sys
from pathlib import Path
# Self is Python 3.11+ but backported in typing-extensions
if sys.version_info < (3, 11):
from typing_extensions import Self
else:
from typing import Self
class TomlConfig(BaseConfig):
def __init__(self, *, data: bytes | str, path: Path) -> None:
super().__init__()
self.path = path
self._parse_setting(data)
def init_empty_config_content(self) -> None:
config_doc = TOMLDocument()
if os.path.isfile(self.path):
with open(self.path, "rb") as input_toml_file:
config_doc = parse(input_toml_file.read())
if config_doc.get("tool") is None:
config_doc["tool"] = table()
config_doc["tool"]["commitizen"] = table() # type: ignore[index]
with open(self.path, "wb") as output_toml_file:
output_toml_file.write(
config_doc.as_string().encode(self._settings["encoding"])
)
def set_key(self, key: str, value: object) -> Self:
with open(self.path, "rb") as f:
config_doc = parse(f.read())
config_doc["tool"]["commitizen"][key] = value # type: ignore[index]
with open(self.path, "wb") as f:
f.write(config_doc.as_string().encode(self._settings["encoding"]))
return self
def _parse_setting(self, data: bytes | str) -> None:
"""We expect to have a section in pyproject looking like
```
[tool.commitizen]
name = "cz_conventional_commits"
```
"""
try:
doc = parse(data)
except exceptions.ParseError as e:
raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}")
try:
self.settings.update(doc["tool"]["commitizen"]) # type: ignore[index,typeddict-item] # TODO: fix this
except exceptions.NonExistentKey:
self.is_empty_config = True