forked from commitizen-tools/commitizen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_config.py
More file actions
66 lines (52 loc) · 1.85 KB
/
json_config.py
File metadata and controls
66 lines (52 loc) · 1.85 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
from __future__ import annotations
import json
from pathlib import Path
from typing import TYPE_CHECKING, Any
from commitizen.exceptions import InvalidConfigurationError
from commitizen.git import smart_open
from .base_config import BaseConfig
if TYPE_CHECKING:
import sys
# 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 JsonConfig(BaseConfig):
def __init__(self, *, data: bytes | str, path: Path | str) -> None:
super().__init__()
self.is_empty_config = False
self.path = path
self._parse_setting(data)
def init_empty_config_content(self) -> None:
with smart_open(self.path, "a", encoding=self.encoding) as json_file:
json.dump({"commitizen": {}}, json_file)
def set_key(self, key: str, value: Any) -> Self:
"""Set or update a key in the conf.
For now only strings are supported.
We use to update the version number.
"""
with open(self.path, "rb") as f:
parser = json.load(f)
parser["commitizen"][key] = value
with smart_open(self.path, "w", encoding=self.encoding) as f:
json.dump(parser, f, indent=2)
return self
def _parse_setting(self, data: bytes | str) -> None:
"""We expect to have a section in .cz.json looking like
```
{
"commitizen": {
"name": "cz_conventional_commits"
}
}
```
"""
try:
doc = json.loads(data)
except json.JSONDecodeError as e:
raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}")
try:
self.settings.update(doc["commitizen"])
except KeyError:
self.is_empty_config = True