|
| 1 | +"""Arabic Presentation Form.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import functools |
| 6 | +import pathlib |
| 7 | +import re |
| 8 | +import sys |
| 9 | +from typing import Any, Dict, Sequence |
| 10 | + |
| 11 | +from ..util import ( |
| 12 | + ABCArgs, |
| 13 | + ABCHook, |
| 14 | + ExitCode, |
| 15 | + HashableDict, |
| 16 | + load_json_source, |
| 17 | +) |
| 18 | +from . import char_map |
| 19 | + |
| 20 | +sys.stdout.reconfigure(encoding="utf-8") # For Windows: we want to be sure to use UTF-8 |
| 21 | +RulesDict = Dict[re.Pattern[Any], str] |
| 22 | + |
| 23 | + |
| 24 | +def apply_rules_to_lines( |
| 25 | + line: str, |
| 26 | + rules: RulesDict, |
| 27 | + exclude: re.Pattern, |
| 28 | + file_name: str, |
| 29 | + line_no: str, |
| 30 | +) -> tuple[ExitCode, str]: |
| 31 | + """Check the text for rules. |
| 32 | +
|
| 33 | + Args: |
| 34 | + line (str): Line to check the rules. |
| 35 | + rules (RulesDict): The rules to check form. |
| 36 | + exclude (re.Pattern): characters to exclude from check. |
| 37 | + file_name (str): the name of the file being checked. |
| 38 | + line_no (int): The line number being checked. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + (ExitCode, str): (The PASS/FAIL state, The new line). |
| 42 | + """ |
| 43 | + exit_code = ExitCode.OK |
| 44 | + new_line = exclude.sub(" ", line) # Replace with space to not affect col numbers |
| 45 | + |
| 46 | + if not char_map.is_contains_non_general_form(max(new_line)): |
| 47 | + return exit_code, line |
| 48 | + |
| 49 | + new_chars: list[str] = [] |
| 50 | + exit_code = ExitCode.FAIL |
| 51 | + |
| 52 | + for col_no, c in enumerate(line, start=1): |
| 53 | + new_c = apply_rule(rules=HashableDict(rules), character=c) |
| 54 | + new_c_as_unicode_hex = [f"\\u{ord(c):04x}" for c in new_c] |
| 55 | + fix_char_loc = ( |
| 56 | + f"{file_name}:{line_no}:{col_no} [{new_c} ({new_c_as_unicode_hex})]" |
| 57 | + ) |
| 58 | + if c != new_c: |
| 59 | + output_str = f"[Fixed] {fix_char_loc}" |
| 60 | + elif char_map.is_contains_non_general_form(new_c): |
| 61 | + output_str = f"[Not Fixed] {fix_char_loc}" |
| 62 | + else: |
| 63 | + output_str = "" |
| 64 | + |
| 65 | + if output_str: |
| 66 | + print(output_str) |
| 67 | + output_str = "" |
| 68 | + |
| 69 | + new_chars.append(new_c) |
| 70 | + |
| 71 | + new_line = "".join(new_chars) |
| 72 | + |
| 73 | + return exit_code, new_line |
| 74 | + |
| 75 | + |
| 76 | +def get_rules(custom_rules: dict[str, dict[str, str]]) -> RulesDict: |
| 77 | + """Return the rules from a given config string. |
| 78 | +
|
| 79 | + Args: |
| 80 | + custom_rules (str): Any additional rules to apply. |
| 81 | +
|
| 82 | + Returns: |
| 83 | + RulesDict: The compiles rules. |
| 84 | +
|
| 85 | + """ |
| 86 | + regex_rules = {} |
| 87 | + complete_rules: char_map.CHAR_MAP_TYPE = {} |
| 88 | + complete_rules.update(char_map.CHAR_MAP) |
| 89 | + complete_rules.update(custom_rules) |
| 90 | + for _rule_name, char_mapping_rule in complete_rules.items(): |
| 91 | + for expected_out, expected_regex in char_mapping_rule["rule"].items(): |
| 92 | + regex_rules.update({re.compile(expected_regex): expected_out}) |
| 93 | + return regex_rules |
| 94 | + |
| 95 | + |
| 96 | +@functools.lru_cache |
| 97 | +def apply_rule(rules: RulesDict, character: str) -> str: |
| 98 | + """Apply the rule from the list of rules to the character. |
| 99 | +
|
| 100 | + Args: |
| 101 | + rules (RulesDict): rules to apply for the character. |
| 102 | + character (str): The letter/character to check against. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + str: The character after applying any rules. |
| 106 | + """ |
| 107 | + new_char = character |
| 108 | + for reg_pattern, replace_char in rules.items(): |
| 109 | + if reg_pattern.match(character): |
| 110 | + new_char = reg_pattern.sub(replace_char, character) |
| 111 | + break |
| 112 | + return new_char |
| 113 | + |
| 114 | + |
| 115 | +class ArabicPresentationFormArgs(ABCArgs): |
| 116 | + """Args.""" |
| 117 | + |
| 118 | + excluded_chars: str |
| 119 | + custom_rules: char_map.CHAR_MAP_TYPE |
| 120 | + |
| 121 | + |
| 122 | +class ArabicPresentationFormChecker(ABCHook): |
| 123 | + """Checker for Header and Footer.""" |
| 124 | + |
| 125 | + def setup_parser(self) -> None: |
| 126 | + """Custom arguments.""" |
| 127 | + self.parser.add_argument( |
| 128 | + "--excluded-chars", |
| 129 | + type=str, |
| 130 | + default="", |
| 131 | + metavar="exclude-char-regex", |
| 132 | + help="Regex for characters to exclude. e.g. (ﷺ)", |
| 133 | + ) |
| 134 | + self.parser.add_argument( |
| 135 | + "--custom-rules", |
| 136 | + type=load_json_source, |
| 137 | + default=dict(), |
| 138 | + metavar="Path-OR-JSON-String", |
| 139 | + help=( |
| 140 | + '"RuleName": {"rule": {"ReplacementCharacter(s)": "RegexOfApplicableCharacter(s)"}}' # noqa: E501 |
| 141 | + '. e.g. "ʾalif": {"rule": {"\u0627": "(\ufe8d|\ufe8e)"}},' # noqa: RUF001 |
| 142 | + + ". To exclude a unicode character, overwrite its default entry." |
| 143 | + ), |
| 144 | + ) |
| 145 | + |
| 146 | + def implementation( |
| 147 | + self, |
| 148 | + file_name: pathlib.Path, |
| 149 | + args: ArabicPresentationFormArgs, |
| 150 | + ) -> ExitCode: |
| 151 | + """Check Implementation.""" |
| 152 | + exit_code = int(ExitCode.OK) |
| 153 | + exclude_regex = re.compile(args.excluded_chars) |
| 154 | + |
| 155 | + new_file_lines = [] |
| 156 | + with file_name.open("r", encoding="utf-8") as f: |
| 157 | + for line_no, line in enumerate(iter(f.readlines()), start=1): |
| 158 | + intermediate_exit_code, new_line = apply_rules_to_lines( |
| 159 | + line=line, |
| 160 | + line_no=line_no, |
| 161 | + file_name=file_name, |
| 162 | + rules=get_rules(args.custom_rules), |
| 163 | + exclude=exclude_regex, |
| 164 | + ) |
| 165 | + exit_code |= intermediate_exit_code |
| 166 | + |
| 167 | + if char_map.is_contains_non_general_form( |
| 168 | + max(exclude_regex.sub("", new_line) or " ") |
| 169 | + ): |
| 170 | + print(f"Incomplete Fixes Applied: {file_name}:{line_no}") |
| 171 | + |
| 172 | + new_file_lines.append(new_line) |
| 173 | + |
| 174 | + with file_name.open("w", encoding="utf-8") as f: |
| 175 | + f.writelines(new_file_lines) |
| 176 | + return ExitCode(exit_code) |
| 177 | + |
| 178 | + |
| 179 | +def main(argv: Sequence[str] | None = None) -> int: |
| 180 | + """Main entrypoint.""" |
| 181 | + argparser = ArabicPresentationFormChecker() |
| 182 | + return argparser.run(argv=argv) |
0 commit comments