Apply a plan for fixing the encoding of text. The plan is a list of tuples of the form (operation, arg). `operation` is one of: - `'encode'`: convert a string to bytes, using `arg` as the encoding - `'decode'`: convert bytes to a string, using `arg` as the encoding - `'tr
(text: str, plan: list[tuple[str, str]])
| 725 | |
| 726 | |
| 727 | def apply_plan(text: str, plan: list[tuple[str, str]]) -> str: |
| 728 | """ |
| 729 | Apply a plan for fixing the encoding of text. |
| 730 | |
| 731 | The plan is a list of tuples of the form (operation, arg). |
| 732 | |
| 733 | `operation` is one of: |
| 734 | |
| 735 | - `'encode'`: convert a string to bytes, using `arg` as the encoding |
| 736 | - `'decode'`: convert bytes to a string, using `arg` as the encoding |
| 737 | - `'transcode'`: convert bytes to bytes, using the function named `arg` |
| 738 | - `'apply'`: convert a string to a string, using the function named `arg` |
| 739 | |
| 740 | The functions that can be applied by 'transcode' and 'apply' are |
| 741 | specifically those that appear in the dictionary named `FIXERS`. They |
| 742 | can also can be imported from the `ftfy.fixes` module. |
| 743 | |
| 744 | Example:: |
| 745 | |
| 746 | >>> mojibake = "schön" |
| 747 | >>> text, plan = fix_and_explain(mojibake) |
| 748 | >>> apply_plan(mojibake, plan) |
| 749 | 'schön' |
| 750 | """ |
| 751 | obj = text |
| 752 | for operation, encoding in plan: |
| 753 | if operation == "encode": |
| 754 | obj = obj.encode(encoding) # type: ignore |
| 755 | elif operation == "decode": |
| 756 | obj = obj.decode(encoding) # type: ignore |
| 757 | elif operation in ("transcode", "apply"): |
| 758 | if encoding in FIXERS: |
| 759 | obj = FIXERS[encoding](obj) |
| 760 | else: |
| 761 | raise ValueError(f"Unknown function to apply: {encoding}") |
| 762 | else: |
| 763 | raise ValueError(f"Unknown plan step: {operation}") |
| 764 | |
| 765 | return obj |
| 766 | |
| 767 | |
| 768 | def explain_unicode(text: str) -> None: |