Save to path. If the destination file exists, modify it in-place. Raises OptionsError if the existing data is corrupt.
(opts: OptManager, path: Path | str, defaults: bool = False)
| 606 | |
| 607 | |
| 608 | def save(opts: OptManager, path: Path | str, defaults: bool = False) -> None: |
| 609 | """ |
| 610 | Save to path. If the destination file exists, modify it in-place. |
| 611 | |
| 612 | Raises OptionsError if the existing data is corrupt. |
| 613 | """ |
| 614 | path = Path(path).expanduser() |
| 615 | if path.exists() and path.is_file(): |
| 616 | with path.open(encoding="utf8") as f: |
| 617 | try: |
| 618 | data = f.read() |
| 619 | except UnicodeDecodeError as e: |
| 620 | raise exceptions.OptionsError(f"Error trying to modify {path}: {e}") |
| 621 | else: |
| 622 | data = "" |
| 623 | |
| 624 | with path.open("w", encoding="utf8") as f: |
| 625 | serialize(opts, f, data, defaults) |
| 626 | |
| 627 | |
| 628 | def relative_path(script_path: Path | str, *, relative_to: Path | str) -> Path: |