| 1 | # -*- coding: utf-8 -*- |
| 2 | from .categoriesscip-python python confusable_homoglyphs 3.3.1 categories/__init__: import aliasscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.categories`/alias()., unique_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.categories`/unique_aliases(). |
| 3 | from .utilsscip-python python confusable_homoglyphs 3.3.1 utils/__init__: import loadscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.utils`/load(). |
| 4 | |
| 5 | confusables_datascip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/confusables_data. = loadscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.utils`/load().('confusables.json') |
| 6 | |
| 7 | |
| 8 | class Foundscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/Found#(Exceptionscip-python python python-stdlib 3.11 builtins/Exception#): |
| 9 | pass |
| 10 | |
| 11 | |
| 12 | def is_mixed_scriptscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_mixed_script().(stringscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_mixed_script().(string), allowed_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_mixed_script().(allowed_aliases)=['COMMON']): |
| 13 | """Checks if ``string`` contains mixed-scripts content, excluding script |
| 14 | blocks aliases in ``allowed_aliases``. |
| 15 | |
| 16 | E.g. ``B. C`` is not considered mixed-scripts by default: it contains characters |
| 17 | from **Latin** and **Common**, but **Common** is excluded by default. |
| 18 | |
| 19 | >>> confusables.is_mixed_script('Abç') |
| 20 | False |
| 21 | >>> confusables.is_mixed_script('ρτ.τ') |
| 22 | False |
| 23 | >>> confusables.is_mixed_script('ρτ.τ', allowed_aliases=[]) |
| 24 | True |
| 25 | >>> confusables.is_mixed_script('Alloτ') |
| 26 | True |
| 27 | |
| 28 | :param string: A unicode string |
| 29 | :type string: str |
| 30 | :param allowed_aliases: Script blocks aliases not to consider. |
| 31 | :type allowed_aliases: list(str) |
| 32 | :return: Whether ``string`` is considered mixed-scripts or not. |
| 33 | :rtype: bool |
| 34 | """ |
| 35 | allowed_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_mixed_script().(allowed_aliases) = [a.upper() for a in allowed_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_mixed_script().(allowed_aliases)] |
| 36 | cats = unique_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.categories`/unique_aliases().(stringscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_mixed_script().(string)) - setscip-python python python-stdlib 3.11 builtins/set#(allowed_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_mixed_script().(allowed_aliases)) |
| 37 | return len(cats) > 1 |
| 38 | |
| 39 | |
| 40 | def is_confusablescip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(stringscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(string), greedyscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(greedy)=False, preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(preferred_aliases)=[]): |
| 41 | """Checks if ``string`` contains characters which might be confusable with |
| 42 | characters from ``preferred_aliases``. |
| 43 | |
| 44 | If ``greedy=False``, it will only return the first confusable character |
| 45 | found without looking at the rest of the string, ``greedy=True`` returns |
| 46 | all of them. |
| 47 | |
| 48 | ``preferred_aliases=[]`` can take an array of unicode block aliases to |
| 49 | be considered as your 'base' unicode blocks: |
| 50 | |
| 51 | - considering ``paρa``, |
| 52 | |
| 53 | - with ``preferred_aliases=['latin']``, the 3rd character ``ρ`` |
| 54 | would be returned because this greek letter can be confused with |
| 55 | latin ``p``. |
| 56 | - with ``preferred_aliases=['greek']``, the 1st character ``p`` |
| 57 | would be returned because this latin letter can be confused with |
| 58 | greek ``ρ``. |
| 59 | - with ``preferred_aliases=[]`` and ``greedy=True``, you'll discover |
| 60 | the 29 characters that can be confused with ``p``, the 23 |
| 61 | characters that look like ``a``, and the one that looks like ``ρ`` |
| 62 | (which is, of course, *p* aka *LATIN SMALL LETTER P*). |
| 63 | |
| 64 | >>> confusables.is_confusable('paρa', preferred_aliases=['latin'])[0]['character'] |
| 65 | 'ρ' |
| 66 | >>> confusables.is_confusable('paρa', preferred_aliases=['greek'])[0]['character'] |
| 67 | 'p' |
| 68 | >>> confusables.is_confusable('Abç', preferred_aliases=['latin']) |
| 69 | False |
| 70 | >>> confusables.is_confusable('AlloΓ', preferred_aliases=['latin']) |
| 71 | False |
| 72 | >>> confusables.is_confusable('ρττ', preferred_aliases=['greek']) |
| 73 | False |
| 74 | >>> confusables.is_confusable('ρτ.τ', preferred_aliases=['greek', 'common']) |
| 75 | False |
| 76 | >>> confusables.is_confusable('ρττp') |
| 77 | [{'homoglyphs': [{'c': 'p', 'n': 'LATIN SMALL LETTER P'}], 'alias': 'GREEK', 'character': 'ρ'}] |
| 78 | |
| 79 | :param string: A unicode string |
| 80 | :type string: str |
| 81 | :param greedy: Don't stop on finding one confusable character - find all of them. |
| 82 | :type greedy: bool |
| 83 | :param preferred_aliases: Script blocks aliases which we don't want ``string``'s characters |
| 84 | to be confused with. |
| 85 | :type preferred_aliases: list(str) |
| 86 | :return: False if not confusable, all confusable characters and with what they are confusable |
| 87 | otherwise. |
| 88 | :rtype: bool or list |
| 89 | """ |
| 90 | preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(preferred_aliases) = [a.upper() for a in preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(preferred_aliases)] |
| 91 | outputs = [] |
| 92 | checked = setscip-python python python-stdlib 3.11 builtins/set#() |
| 93 | for char in stringscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(string): |
| 94 | if char in checked: |
| 95 | continue |
| 96 | checked.addscip-python python python-stdlib 3.11 builtins/set#add().(char) |
| 97 | char_alias = aliasscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.categories`/alias().(char) |
| 98 | if char_alias in preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(preferred_aliases): |
| 99 | # it's safe if the character might be confusable with homoglyphs from other |
| 100 | # categories than our preferred categories (=aliases) |
| 101 | continue |
| 102 | found = confusables_datascip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/confusables_data..get(char, False) |
| 103 | if found is False: |
| 104 | continue |
| 105 | # character λ is considered confusable if λ can be confused with a character from |
| 106 | # preferred_aliases, e.g. if 'LATIN', 'ρ' is confusable with 'p' from LATIN. |
| 107 | # if 'LATIN', 'Γ' is not confusable because in all the characters confusable with Γ, |
| 108 | # none of them is LATIN. |
| 109 | if preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(preferred_aliases): |
| 110 | potentially_confusable = [] |
| 111 | try: |
| 112 | for d in found: |
| 113 | aliases = [aliasscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.categories`/alias().(glyph) for glyph in d['c']] |
| 114 | for a in aliases: |
| 115 | if a in preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(preferred_aliases): |
| 116 | potentially_confusable = found |
| 117 | raise Foundscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/Found#() |
| 118 | except Foundscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/Found#: |
| 119 | pass |
| 120 | else: |
| 121 | potentially_confusable = found |
| 122 | if potentially_confusable: # we found homoglyphs |
| 123 | output = { |
| 124 | 'character': char, |
| 125 | 'alias': char_alias, |
| 126 | 'homoglyphs': potentially_confusable, |
| 127 | } |
| 128 | if not greedyscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(greedy): |
| 129 | return [output] |
| 130 | outputs.appendscip-python python python-stdlib 3.11 builtins/list#append().(output) |
| 131 | |
| 132 | return outputs or False |
| 133 | |
| 134 | |
| 135 | def is_dangerousscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_dangerous().(stringscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_dangerous().(string), preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_dangerous().(preferred_aliases)=[]): |
| 136 | """Checks if ``string`` can be dangerous, i.e. is it not only mixed-scripts |
| 137 | but also contains characters from other scripts than the ones in ``preferred_aliases`` |
| 138 | that might be confusable with characters from scripts in ``preferred_aliases`` |
| 139 | |
| 140 | For ``preferred_aliases`` examples, see ``is_confusable`` docstring. |
| 141 | |
| 142 | >>> bool(confusables.is_dangerous('Allo')) |
| 143 | False |
| 144 | >>> bool(confusables.is_dangerous('AlloΓ', preferred_aliases=['latin'])) |
| 145 | False |
| 146 | >>> bool(confusables.is_dangerous('Alloρ')) |
| 147 | True |
| 148 | >>> bool(confusables.is_dangerous('AlaskaJazz')) |
| 149 | False |
| 150 | >>> bool(confusables.is_dangerous('ΑlaskaJazz')) |
| 151 | True |
| 152 | |
| 153 | :param string: A unicode string |
| 154 | :type string: str |
| 155 | :param preferred_aliases: Script blocks aliases which we don't want ``string``'s characters |
| 156 | to be confused with. |
| 157 | :type preferred_aliases: list(str) |
| 158 | :return: Is it dangerous. |
| 159 | :rtype: bool |
| 160 | """ |
| 161 | return ( |
| 162 | is_mixed_scriptscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_mixed_script().(stringscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_dangerous().(string)) |
| 163 | and boolscip-python python python-stdlib 3.11 builtins/bool#(is_confusablescip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(stringscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_dangerous().(string), preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_confusable().(preferred_aliases)=preferred_aliasesscip-python python confusable_homoglyphs 3.3.1 `confusable_homoglyphs.confusables`/is_dangerous().(preferred_aliases))) |
| 164 | ) |