| 20 | |
| 21 | |
| 22 | class TomlConfig(BaseConfig): |
| 23 | def __init__(self, *, data: bytes | str, path: Path) -> None: |
| 24 | super().__init__() |
| 25 | self.path = path |
| 26 | self._parse_setting(data) |
| 27 | |
| 28 | def contains_commitizen_section(self) -> bool: |
| 29 | config_doc = parse(self.path.read_bytes()) |
| 30 | return config_doc.get("tool", {}).get("commitizen") is not None |
| 31 | |
| 32 | def init_empty_config_content(self) -> None: |
| 33 | config_doc = TOMLDocument() |
| 34 | if self.path.is_file(): |
| 35 | config_doc = parse(self.path.read_bytes()) |
| 36 | |
| 37 | if config_doc.get("tool") is None: |
| 38 | config_doc["tool"] = table() |
| 39 | config_doc["tool"]["commitizen"] = table() # type: ignore[index] |
| 40 | |
| 41 | with self.path.open("wb") as output_toml_file: |
| 42 | output_toml_file.write( |
| 43 | config_doc.as_string().encode(self._settings["encoding"]) |
| 44 | ) |
| 45 | |
| 46 | def set_key(self, key: str, value: object) -> Self: |
| 47 | config_doc = parse(self.path.read_bytes()) |
| 48 | |
| 49 | config_doc["tool"]["commitizen"][key] = value # type: ignore[index] |
| 50 | self.path.write_bytes(config_doc.as_string().encode(self._settings["encoding"])) |
| 51 | |
| 52 | return self |
| 53 | |
| 54 | def _parse_setting(self, data: bytes | str) -> None: |
| 55 | """We expect to have a section in pyproject looking like |
| 56 | |
| 57 | ``` |
| 58 | [tool.commitizen] |
| 59 | name = "cz_conventional_commits" |
| 60 | ``` |
| 61 | """ |
| 62 | try: |
| 63 | doc = parse(data) |
| 64 | except exceptions.ParseError as e: |
| 65 | raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}") |
| 66 | |
| 67 | try: |
| 68 | self.settings.update(doc["tool"]["commitizen"]) # type: ignore[index,typeddict-item] # TODO: fix this |
| 69 | except exceptions.NonExistentKey: |
| 70 | pass |
no outgoing calls
searching dependent graphs…