| 21 | |
| 22 | |
| 23 | class YAMLConfig(BaseConfig): |
| 24 | def __init__(self, *, data: bytes | str, path: Path) -> None: |
| 25 | super().__init__() |
| 26 | self.path = path |
| 27 | self._parse_setting(data) |
| 28 | |
| 29 | def init_empty_config_content(self) -> None: |
| 30 | with smart_open( |
| 31 | self.path, "a", encoding=self._settings["encoding"] |
| 32 | ) as json_file: |
| 33 | yaml.dump({"commitizen": {}}, json_file, explicit_start=True) |
| 34 | |
| 35 | def contains_commitizen_section(self) -> bool: |
| 36 | with self.path.open("rb") as yaml_file: |
| 37 | config_doc = yaml.load(yaml_file, Loader=yaml.FullLoader) |
| 38 | return config_doc is not None and config_doc.get("commitizen") is not None |
| 39 | |
| 40 | def _parse_setting(self, data: bytes | str) -> None: |
| 41 | """We expect to have a section in cz.yaml looking like |
| 42 | |
| 43 | ``` |
| 44 | commitizen: |
| 45 | name: cz_conventional_commits |
| 46 | ``` |
| 47 | """ |
| 48 | try: |
| 49 | doc = yaml.safe_load(data) |
| 50 | except yaml.YAMLError as e: |
| 51 | raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}") |
| 52 | |
| 53 | try: |
| 54 | self.settings.update(doc["commitizen"]) |
| 55 | except (KeyError, TypeError): |
| 56 | pass |
| 57 | |
| 58 | def set_key(self, key: str, value: object) -> Self: |
| 59 | with self.path.open("rb") as yaml_file: |
| 60 | config_doc = yaml.load(yaml_file, Loader=yaml.FullLoader) |
| 61 | |
| 62 | config_doc["commitizen"][key] = value |
| 63 | with smart_open( |
| 64 | self.path, "w", encoding=self._settings["encoding"] |
| 65 | ) as yaml_file: |
| 66 | yaml.dump(config_doc, yaml_file, explicit_start=True) |
| 67 | |
| 68 | return self |
no outgoing calls
searching dependent graphs…