| 20 | |
| 21 | |
| 22 | class JsonConfig(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 | with self.path.open("rb") as json_file: |
| 30 | try: |
| 31 | config_doc = json.load(json_file) |
| 32 | except json.JSONDecodeError: |
| 33 | return False |
| 34 | return config_doc.get("commitizen") is not None |
| 35 | |
| 36 | def init_empty_config_content(self) -> None: |
| 37 | with smart_open( |
| 38 | self.path, "a", encoding=self._settings["encoding"] |
| 39 | ) as json_file: |
| 40 | json.dump({"commitizen": {}}, json_file) |
| 41 | |
| 42 | def set_key(self, key: str, value: object) -> Self: |
| 43 | with self.path.open("rb") as f: |
| 44 | config_doc = json.load(f) |
| 45 | |
| 46 | config_doc["commitizen"][key] = value |
| 47 | with smart_open(self.path, "w", encoding=self._settings["encoding"]) as f: |
| 48 | json.dump(config_doc, f, indent=2) |
| 49 | return self |
| 50 | |
| 51 | def _parse_setting(self, data: bytes | str) -> None: |
| 52 | """We expect to have a section in .cz.json looking like |
| 53 | |
| 54 | ``` |
| 55 | { |
| 56 | "commitizen": { |
| 57 | "name": "cz_conventional_commits" |
| 58 | } |
| 59 | } |
| 60 | ``` |
| 61 | """ |
| 62 | try: |
| 63 | doc = json.loads(data) |
| 64 | except json.JSONDecodeError as e: |
| 65 | raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}") |
| 66 | |
| 67 | try: |
| 68 | self.settings.update(doc["commitizen"]) |
| 69 | except KeyError: |
| 70 | pass |
no outgoing calls
searching dependent graphs…