2
2
3
3
import os
4
4
from pathlib import Path
5
- from typing import TYPE_CHECKING , Any
5
+ from typing import TYPE_CHECKING
6
6
7
- from tomlkit import exceptions , parse , table
7
+ from tomlkit import TOMLDocument , exceptions , parse , table
8
8
9
9
from commitizen .exceptions import InvalidConfigurationError
10
10
@@ -28,30 +28,31 @@ def __init__(self, *, data: bytes | str, path: Path | str) -> None:
28
28
self ._parse_setting (data )
29
29
30
30
def init_empty_config_content (self ) -> None :
31
+ config_doc = TOMLDocument ()
31
32
if os .path .isfile (self .path ):
32
33
with open (self .path , "rb" ) as input_toml_file :
33
- parser = parse (input_toml_file .read ())
34
- else :
35
- parser = parse ("" )
34
+ config_doc = parse (input_toml_file .read ())
35
+
36
+ if config_doc .get ("tool" ) is None :
37
+ config_doc ["tool" ] = table ()
38
+ config_doc ["tool" ]["commitizen" ] = table () # type: ignore[index]
36
39
37
40
with open (self .path , "wb" ) as output_toml_file :
38
- if parser .get ("tool" ) is None :
39
- parser ["tool" ] = table ()
40
- parser ["tool" ]["commitizen" ] = table () # type: ignore[index]
41
- output_toml_file .write (parser .as_string ().encode (self .encoding ))
41
+ output_toml_file .write (config_doc .as_string ().encode (self .encoding ))
42
42
43
- def set_key (self , key : str , value : Any ) -> Self :
43
+ def set_key (self , key : str , value : object ) -> Self :
44
44
"""Set or update a key in the conf.
45
45
46
46
For now only strings are supported.
47
47
We use to update the version number.
48
48
"""
49
49
with open (self .path , "rb" ) as f :
50
- parser = parse (f .read ())
50
+ config_doc = parse (f .read ())
51
51
52
- parser ["tool" ]["commitizen" ][key ] = value # type: ignore[index]
52
+ config_doc ["tool" ]["commitizen" ][key ] = value # type: ignore[index]
53
53
with open (self .path , "wb" ) as f :
54
- f .write (parser .as_string ().encode (self .encoding ))
54
+ f .write (config_doc .as_string ().encode (self .encoding ))
55
+
55
56
return self
56
57
57
58
def _parse_setting (self , data : bytes | str ) -> None :
0 commit comments