Write a learned preference into the Conventions section of CODEY.md.
(self, key: str, value: str)
| 417 | self._sync_to_codeymd(key, value) |
| 418 | |
| 419 | def _sync_to_codeymd(self, key: str, value: str): |
| 420 | """Write a learned preference into the Conventions section of CODEY.md.""" |
| 421 | try: |
| 422 | from core.codeymd import find_codeymd |
| 423 | import os |
| 424 | codeymd_path = find_codeymd() |
| 425 | if not codeymd_path: |
| 426 | return |
| 427 | text = codeymd_path.read_text(encoding="utf-8", errors="replace") |
| 428 | label_map = { |
| 429 | "test_framework": "Test framework", |
| 430 | "code_style": "Code style", |
| 431 | "naming_convention": "Naming", |
| 432 | "import_style": "Imports", |
| 433 | "docstring_style": "Docstrings", |
| 434 | "error_handling": "Error handling", |
| 435 | "type_hints": "Type hints", |
| 436 | "async_style": "Async", |
| 437 | "http_library": "HTTP library", |
| 438 | "cli_library": "CLI library", |
| 439 | "log_style": "Logging", |
| 440 | } |
| 441 | label = label_map.get(key, key) |
| 442 | entry = f"- {label}: {value}" |
| 443 | # If a Conventions section exists, update or append the entry |
| 444 | if "# Conventions" in text: |
| 445 | lines = text.splitlines() |
| 446 | new_lines = [] |
| 447 | in_conv = False |
| 448 | entry_written = False |
| 449 | for line in lines: |
| 450 | if line.strip() == "# Conventions": |
| 451 | in_conv = True |
| 452 | new_lines.append(line) |
| 453 | continue |
| 454 | if in_conv and line.startswith("# ") and line.strip() != "# Conventions": |
| 455 | # Next section — write entry before leaving if not done |
| 456 | if not entry_written: |
| 457 | new_lines.append(entry) |
| 458 | entry_written = True |
| 459 | in_conv = False |
| 460 | if in_conv and line.startswith(f"- {label}:"): |
| 461 | new_lines.append(entry) |
| 462 | entry_written = True |
| 463 | continue |
| 464 | new_lines.append(line) |
| 465 | if in_conv and not entry_written: |
| 466 | new_lines.append(entry) |
| 467 | codeymd_path.write_text("\n".join(new_lines) + "\n", encoding="utf-8") |
| 468 | else: |
| 469 | # No Conventions section — append one |
| 470 | codeymd_path.write_text( |
| 471 | text.rstrip() + f"\n\n# Conventions\n{entry}\n", |
| 472 | encoding="utf-8" |
| 473 | ) |
| 474 | except Exception: |
| 475 | pass |
| 476 |
no test coverage detected