KL-Py 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. KL_Py/KL_Py.py +4400 -0
  2. KL_Py/__init__.py +1 -0
  3. KL_Py/hindGui/__init__.py +16529 -0
  4. KL_Py/hindGui/_utils.py +140 -0
  5. KL_Py/hindGui/elements/base.py +1055 -0
  6. KL_Py/hindGui/elements/button.py +947 -0
  7. KL_Py/hindGui/elements/calendar.py +222 -0
  8. KL_Py/hindGui/elements/canvas.py +122 -0
  9. KL_Py/hindGui/elements/center.py +44 -0
  10. KL_Py/hindGui/elements/checkbox.py +240 -0
  11. KL_Py/hindGui/elements/column.py +443 -0
  12. KL_Py/hindGui/elements/combo.py +329 -0
  13. KL_Py/hindGui/elements/error.py +47 -0
  14. KL_Py/hindGui/elements/frame.py +273 -0
  15. KL_Py/hindGui/elements/graph.py +817 -0
  16. KL_Py/hindGui/elements/helpers.py +202 -0
  17. KL_Py/hindGui/elements/image.py +324 -0
  18. KL_Py/hindGui/elements/input.py +310 -0
  19. KL_Py/hindGui/elements/list_box.py +392 -0
  20. KL_Py/hindGui/elements/menu.py +192 -0
  21. KL_Py/hindGui/elements/multiline.py +685 -0
  22. KL_Py/hindGui/elements/option_menu.py +168 -0
  23. KL_Py/hindGui/elements/pane.py +127 -0
  24. KL_Py/hindGui/elements/progress_bar.py +304 -0
  25. KL_Py/hindGui/elements/radio.py +252 -0
  26. KL_Py/hindGui/elements/separator.py +64 -0
  27. KL_Py/hindGui/elements/sizegrip.py +34 -0
  28. KL_Py/hindGui/elements/slider.py +199 -0
  29. KL_Py/hindGui/elements/spin.py +252 -0
  30. KL_Py/hindGui/elements/status_bar.py +165 -0
  31. KL_Py/hindGui/elements/stretch.py +26 -0
  32. KL_Py/hindGui/elements/tab.py +697 -0
  33. KL_Py/hindGui/elements/table.py +463 -0
  34. KL_Py/hindGui/elements/text.py +413 -0
  35. KL_Py/hindGui/elements/tree.py +493 -0
  36. KL_Py/hindGui/replace.py +45 -0
  37. KL_Py/hindGui/tray.py +358 -0
  38. KL_Py/hindGui/window.py +2906 -0
  39. KL_Py/requests/__init__.py +184 -0
  40. KL_Py/requests/__version__.py +14 -0
  41. KL_Py/requests/_internal_utils.py +50 -0
  42. KL_Py/requests/adapters.py +719 -0
  43. KL_Py/requests/api.py +157 -0
  44. KL_Py/requests/auth.py +314 -0
  45. KL_Py/requests/certifi/__init__.py +4 -0
  46. KL_Py/requests/certifi/__main__.py +12 -0
  47. KL_Py/requests/certifi/core.py +83 -0
  48. KL_Py/requests/certifi/py.typed +0 -0
  49. KL_Py/requests/certs.py +17 -0
  50. KL_Py/requests/charset_normalizer/__init__.py +48 -0
  51. KL_Py/requests/charset_normalizer/__main__.py +6 -0
  52. KL_Py/requests/charset_normalizer/api.py +671 -0
  53. KL_Py/requests/charset_normalizer/cd.py +421 -0
  54. KL_Py/requests/charset_normalizer/cli/__init__.py +8 -0
  55. KL_Py/requests/charset_normalizer/cli/__main__.py +363 -0
  56. KL_Py/requests/charset_normalizer/constant.py +2031 -0
  57. KL_Py/requests/charset_normalizer/legacy.py +80 -0
  58. KL_Py/requests/charset_normalizer/md.py +744 -0
  59. KL_Py/requests/charset_normalizer/models.py +359 -0
  60. KL_Py/requests/charset_normalizer/py.typed +0 -0
  61. KL_Py/requests/charset_normalizer/utils.py +420 -0
  62. KL_Py/requests/charset_normalizer/version.py +8 -0
  63. KL_Py/requests/compat.py +106 -0
  64. KL_Py/requests/cookies.py +561 -0
  65. KL_Py/requests/exceptions.py +151 -0
  66. KL_Py/requests/help.py +134 -0
  67. KL_Py/requests/hooks.py +33 -0
  68. KL_Py/requests/idna/__init__.py +45 -0
  69. KL_Py/requests/idna/codec.py +122 -0
  70. KL_Py/requests/idna/compat.py +15 -0
  71. KL_Py/requests/idna/core.py +437 -0
  72. KL_Py/requests/idna/idnadata.py +4309 -0
  73. KL_Py/requests/idna/intranges.py +57 -0
  74. KL_Py/requests/idna/package_data.py +1 -0
  75. KL_Py/requests/idna/py.typed +0 -0
  76. KL_Py/requests/idna/uts46data.py +8841 -0
  77. KL_Py/requests/models.py +1039 -0
  78. KL_Py/requests/packages.py +23 -0
  79. KL_Py/requests/sessions.py +831 -0
  80. KL_Py/requests/status_codes.py +128 -0
  81. KL_Py/requests/structures.py +99 -0
  82. KL_Py/requests/urllib3/__init__.py +211 -0
  83. KL_Py/requests/urllib3/_base_connection.py +165 -0
  84. KL_Py/requests/urllib3/_collections.py +487 -0
  85. KL_Py/requests/urllib3/_request_methods.py +278 -0
  86. KL_Py/requests/urllib3/_version.py +34 -0
  87. KL_Py/requests/urllib3/connection.py +1099 -0
  88. KL_Py/requests/urllib3/connectionpool.py +1178 -0
  89. KL_Py/requests/urllib3/contrib/__init__.py +0 -0
  90. KL_Py/requests/urllib3/contrib/emscripten/__init__.py +17 -0
  91. KL_Py/requests/urllib3/contrib/emscripten/connection.py +260 -0
  92. KL_Py/requests/urllib3/contrib/emscripten/fetch.py +726 -0
  93. KL_Py/requests/urllib3/contrib/emscripten/request.py +22 -0
  94. KL_Py/requests/urllib3/contrib/emscripten/response.py +277 -0
  95. KL_Py/requests/urllib3/contrib/pyopenssl.py +564 -0
  96. KL_Py/requests/urllib3/contrib/socks.py +228 -0
  97. KL_Py/requests/urllib3/exceptions.py +335 -0
  98. KL_Py/requests/urllib3/fields.py +341 -0
  99. KL_Py/requests/urllib3/filepost.py +89 -0
  100. KL_Py/requests/urllib3/http2/__init__.py +53 -0
  101. KL_Py/requests/urllib3/http2/connection.py +356 -0
  102. KL_Py/requests/urllib3/http2/probe.py +87 -0
  103. KL_Py/requests/urllib3/poolmanager.py +651 -0
  104. KL_Py/requests/urllib3/py.typed +2 -0
  105. KL_Py/requests/urllib3/response.py +1480 -0
  106. KL_Py/requests/urllib3/util/__init__.py +42 -0
  107. KL_Py/requests/urllib3/util/connection.py +137 -0
  108. KL_Py/requests/urllib3/util/proxy.py +43 -0
  109. KL_Py/requests/urllib3/util/request.py +263 -0
  110. KL_Py/requests/urllib3/util/response.py +101 -0
  111. KL_Py/requests/urllib3/util/retry.py +549 -0
  112. KL_Py/requests/urllib3/util/ssl_.py +527 -0
  113. KL_Py/requests/urllib3/util/ssl_match_hostname.py +159 -0
  114. KL_Py/requests/urllib3/util/ssltransport.py +271 -0
  115. KL_Py/requests/urllib3/util/timeout.py +275 -0
  116. KL_Py/requests/urllib3/util/url.py +469 -0
  117. KL_Py/requests/urllib3/util/util.py +42 -0
  118. KL_Py/requests/urllib3/util/wait.py +124 -0
  119. KL_Py/requests/utils.py +1086 -0
  120. KL_Py/when.py +144 -0
  121. kl_py-0.0.1.dist-info/METADATA +15 -0
  122. kl_py-0.0.1.dist-info/RECORD +125 -0
  123. kl_py-0.0.1.dist-info/WHEEL +5 -0
  124. kl_py-0.0.1.dist-info/licenses/LICENSE +21 -0
  125. kl_py-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,421 @@
1
+ from __future__ import annotations
2
+
3
+ import importlib
4
+ from codecs import IncrementalDecoder
5
+ from collections import Counter
6
+ from functools import lru_cache
7
+ from typing import Counter as TypeCounter
8
+
9
+ from .constant import (
10
+ FREQUENCIES,
11
+ KO_NAMES,
12
+ LANGUAGE_SUPPORTED_COUNT,
13
+ TOO_SMALL_SEQUENCE,
14
+ ZH_NAMES,
15
+ _FREQUENCIES_SET,
16
+ _FREQUENCIES_RANK,
17
+ )
18
+ from .md import is_suspiciously_successive_range
19
+ from .models import CoherenceMatches
20
+ from .utils import (
21
+ is_accentuated,
22
+ is_latin,
23
+ is_multi_byte_encoding,
24
+ is_unicode_range_secondary,
25
+ unicode_range,
26
+ )
27
+
28
+
29
+ def encoding_unicode_range(iana_name: str) -> list[str]:
30
+ """
31
+ Return associated unicode ranges in a single byte code page.
32
+ """
33
+ if is_multi_byte_encoding(iana_name):
34
+ raise OSError("Function not supported on multi-byte code page")
35
+
36
+ decoder = importlib.import_module(f"encodings.{iana_name}").IncrementalDecoder
37
+
38
+ p: IncrementalDecoder = decoder(errors="ignore")
39
+ seen_ranges: dict[str, int] = {}
40
+ character_count: int = 0
41
+
42
+ for i in range(0x40, 0xFF):
43
+ chunk: str = p.decode(bytes([i]))
44
+
45
+ if chunk:
46
+ character_range: str | None = unicode_range(chunk)
47
+
48
+ if character_range is None:
49
+ continue
50
+
51
+ if is_unicode_range_secondary(character_range) is False:
52
+ if character_range not in seen_ranges:
53
+ seen_ranges[character_range] = 0
54
+ seen_ranges[character_range] += 1
55
+ character_count += 1
56
+
57
+ return sorted(
58
+ [
59
+ character_range
60
+ for character_range in seen_ranges
61
+ if seen_ranges[character_range] / character_count >= 0.15
62
+ ]
63
+ )
64
+
65
+
66
+ def unicode_range_languages(primary_range: str) -> list[str]:
67
+ """
68
+ Return inferred languages used with a unicode range.
69
+ """
70
+ languages: list[str] = []
71
+
72
+ for language, characters in FREQUENCIES.items():
73
+ for character in characters:
74
+ if unicode_range(character) == primary_range:
75
+ languages.append(language)
76
+ break
77
+
78
+ return languages
79
+
80
+
81
+ @lru_cache()
82
+ def encoding_languages(iana_name: str) -> list[str]:
83
+ """
84
+ Single-byte encoding language association. Some code page are heavily linked to particular language(s).
85
+ This function does the correspondence.
86
+ """
87
+ unicode_ranges: list[str] = encoding_unicode_range(iana_name)
88
+ primary_range: str | None = None
89
+
90
+ for specified_range in unicode_ranges:
91
+ if "Latin" not in specified_range:
92
+ primary_range = specified_range
93
+ break
94
+
95
+ if primary_range is None:
96
+ return ["Latin Based"]
97
+
98
+ return unicode_range_languages(primary_range)
99
+
100
+
101
+ @lru_cache()
102
+ def mb_encoding_languages(iana_name: str) -> list[str]:
103
+ """
104
+ Multi-byte encoding language association. Some code page are heavily linked to particular language(s).
105
+ This function does the correspondence.
106
+ """
107
+ if (
108
+ iana_name.startswith("shift_")
109
+ or iana_name.startswith("iso2022_jp")
110
+ or iana_name.startswith("euc_j")
111
+ or iana_name == "cp932"
112
+ ):
113
+ return ["Japanese"]
114
+ if iana_name.startswith("gb") or iana_name in ZH_NAMES:
115
+ return ["Chinese"]
116
+ if iana_name.startswith("iso2022_kr") or iana_name in KO_NAMES:
117
+ return ["Korean"]
118
+
119
+ return []
120
+
121
+
122
+ @lru_cache(maxsize=LANGUAGE_SUPPORTED_COUNT)
123
+ def get_target_features(language: str) -> tuple[bool, bool]:
124
+ """
125
+ Determine main aspects from a supported language if it contains accents and if is pure Latin.
126
+ """
127
+ target_have_accents: bool = False
128
+ target_pure_latin: bool = True
129
+
130
+ for character in FREQUENCIES[language]:
131
+ if not target_have_accents and is_accentuated(character):
132
+ target_have_accents = True
133
+ if target_pure_latin and is_latin(character) is False:
134
+ target_pure_latin = False
135
+
136
+ return target_have_accents, target_pure_latin
137
+
138
+
139
+ def alphabet_languages(
140
+ characters: list[str], ignore_non_latin: bool = False
141
+ ) -> list[str]:
142
+ """
143
+ Return associated languages associated to given characters.
144
+ """
145
+ languages: list[tuple[str, float]] = []
146
+
147
+ characters_set: frozenset[str] = frozenset(characters)
148
+ source_have_accents = any(is_accentuated(character) for character in characters)
149
+
150
+ for language, language_characters in FREQUENCIES.items():
151
+ target_have_accents, target_pure_latin = get_target_features(language)
152
+
153
+ if ignore_non_latin and target_pure_latin is False:
154
+ continue
155
+
156
+ if target_have_accents is False and source_have_accents:
157
+ continue
158
+
159
+ character_count: int = len(language_characters)
160
+
161
+ character_match_count: int = len(_FREQUENCIES_SET[language] & characters_set)
162
+
163
+ ratio: float = character_match_count / character_count
164
+
165
+ if ratio >= 0.2:
166
+ languages.append((language, ratio))
167
+
168
+ languages = sorted(languages, key=lambda x: x[1], reverse=True)
169
+
170
+ return [compatible_language[0] for compatible_language in languages]
171
+
172
+
173
+ def characters_popularity_compare(
174
+ language: str, ordered_characters: list[str]
175
+ ) -> float:
176
+ """
177
+ Determine if a ordered characters list (by occurrence from most appearance to rarest) match a particular language.
178
+ The result is a ratio between 0. (absolutely no correspondence) and 1. (near perfect fit).
179
+ Beware that is function is not strict on the match in order to ease the detection. (Meaning close match is 1.)
180
+ """
181
+ if language not in FREQUENCIES:
182
+ raise ValueError(f"{language} not available")
183
+
184
+ character_approved_count: int = 0
185
+ frequencies_language_set: frozenset[str] = _FREQUENCIES_SET[language]
186
+ lang_rank: dict[str, int] = _FREQUENCIES_RANK[language]
187
+
188
+ ordered_characters_count: int = len(ordered_characters)
189
+ target_language_characters_count: int = len(FREQUENCIES[language])
190
+
191
+ large_alphabet: bool = target_language_characters_count > 26
192
+
193
+ expected_projection_ratio: float = (
194
+ target_language_characters_count / ordered_characters_count
195
+ )
196
+
197
+ # Pre-built rank dict for ordered_characters (avoids repeated list slicing).
198
+ ordered_rank: dict[str, int] = {
199
+ char: rank for rank, char in enumerate(ordered_characters)
200
+ }
201
+
202
+ # Pre-compute characters common to both orderings.
203
+ # Avoids repeated `c in ordered_rank` dict lookups in the inner counts.
204
+ common_chars: list[tuple[int, int]] = [
205
+ (lr, ordered_rank[c]) for c, lr in lang_rank.items() if c in ordered_rank
206
+ ]
207
+
208
+ for character, character_rank in zip(
209
+ ordered_characters, range(0, ordered_characters_count)
210
+ ):
211
+ if character not in frequencies_language_set:
212
+ continue
213
+
214
+ character_rank_in_language: int = lang_rank[character]
215
+ character_rank_projection: int = int(character_rank * expected_projection_ratio)
216
+
217
+ if (
218
+ large_alphabet is False
219
+ and abs(character_rank_projection - character_rank_in_language) > 4
220
+ ):
221
+ continue
222
+
223
+ if (
224
+ large_alphabet is True
225
+ and abs(character_rank_projection - character_rank_in_language)
226
+ < target_language_characters_count / 3
227
+ ):
228
+ character_approved_count += 1
229
+ continue
230
+
231
+ # Count how many characters appear "before" in both orderings,
232
+ # and how many appear "at or after" in both orderings.
233
+ before_match_count: int = sum(
234
+ 1
235
+ for lr, orr in common_chars
236
+ if lr < character_rank_in_language and orr < character_rank
237
+ )
238
+
239
+ after_len: int = target_language_characters_count - character_rank_in_language
240
+ after_match_count: int = sum(
241
+ 1
242
+ for lr, orr in common_chars
243
+ if lr >= character_rank_in_language and orr >= character_rank
244
+ )
245
+
246
+ if character_rank_in_language == 0 and before_match_count <= 4:
247
+ character_approved_count += 1
248
+ continue
249
+
250
+ if after_len == 0 and after_match_count <= 4:
251
+ character_approved_count += 1
252
+ continue
253
+
254
+ if (
255
+ character_rank_in_language > 0
256
+ and before_match_count / character_rank_in_language >= 0.4
257
+ ) or (after_len > 0 and after_match_count / after_len >= 0.4):
258
+ character_approved_count += 1
259
+ continue
260
+
261
+ return character_approved_count / len(ordered_characters)
262
+
263
+
264
+ def alpha_unicode_split(decoded_sequence: str) -> list[str]:
265
+ """
266
+ Given a decoded text sequence, return a list of str. Unicode range / alphabet separation.
267
+ Ex. a text containing English/Latin with a bit a Hebrew will return two items in the resulting list;
268
+ One containing the latin letters and the other hebrew.
269
+ """
270
+ layers: dict[str, list[str]] = {}
271
+
272
+ # Fast path: track single-layer key to skip dict iteration for single-script text.
273
+ single_layer_key: str | None = None
274
+ multi_layer: bool = False
275
+
276
+ for character in decoded_sequence:
277
+ if character.isalpha() is False:
278
+ continue
279
+
280
+ character_range: str | None = unicode_range(character)
281
+
282
+ if character_range is None:
283
+ continue
284
+
285
+ layer_target_range: str | None = None
286
+
287
+ if multi_layer:
288
+ for discovered_range in layers:
289
+ if (
290
+ is_suspiciously_successive_range(discovered_range, character_range)
291
+ is False
292
+ ):
293
+ layer_target_range = discovered_range
294
+ break
295
+ elif single_layer_key is not None:
296
+ if (
297
+ is_suspiciously_successive_range(single_layer_key, character_range)
298
+ is False
299
+ ):
300
+ layer_target_range = single_layer_key
301
+
302
+ if layer_target_range is None:
303
+ layer_target_range = character_range
304
+
305
+ if layer_target_range not in layers:
306
+ layers[layer_target_range] = []
307
+ if single_layer_key is None:
308
+ single_layer_key = layer_target_range
309
+ else:
310
+ multi_layer = True
311
+
312
+ layers[layer_target_range].append(character)
313
+
314
+ return ["".join(chars).lower() for chars in layers.values()]
315
+
316
+
317
+ def merge_coherence_ratios(results: list[CoherenceMatches]) -> CoherenceMatches:
318
+ """
319
+ This function merge results previously given by the function coherence_ratio.
320
+ The return type is the same as coherence_ratio.
321
+ """
322
+ per_language_ratios: dict[str, list[float]] = {}
323
+ for result in results:
324
+ for sub_result in result:
325
+ language, ratio = sub_result
326
+ if language not in per_language_ratios:
327
+ per_language_ratios[language] = [ratio]
328
+ continue
329
+ per_language_ratios[language].append(ratio)
330
+
331
+ merge = [
332
+ (
333
+ language,
334
+ round(
335
+ sum(per_language_ratios[language]) / len(per_language_ratios[language]),
336
+ 4,
337
+ ),
338
+ )
339
+ for language in per_language_ratios
340
+ ]
341
+
342
+ return sorted(merge, key=lambda x: x[1], reverse=True)
343
+
344
+
345
+ def filter_alt_coherence_matches(results: CoherenceMatches) -> CoherenceMatches:
346
+ """
347
+ We shall NOT return "English—" in CoherenceMatches because it is an alternative
348
+ of "English". This function only keeps the best match and remove the em-dash in it.
349
+ """
350
+ index_results: dict[str, list[float]] = dict()
351
+
352
+ for result in results:
353
+ language, ratio = result
354
+ no_em_name: str = language.replace("—", "")
355
+
356
+ if no_em_name not in index_results:
357
+ index_results[no_em_name] = []
358
+
359
+ index_results[no_em_name].append(ratio)
360
+
361
+ if any(len(index_results[e]) > 1 for e in index_results):
362
+ filtered_results: CoherenceMatches = []
363
+
364
+ for language in index_results:
365
+ filtered_results.append((language, max(index_results[language])))
366
+
367
+ return filtered_results
368
+
369
+ return results
370
+
371
+
372
+ @lru_cache(maxsize=2048)
373
+ def coherence_ratio(
374
+ decoded_sequence: str, threshold: float = 0.1, lg_inclusion: str | None = None
375
+ ) -> CoherenceMatches:
376
+ """
377
+ Detect ANY language that can be identified in given sequence. The sequence will be analysed by layers.
378
+ A layer = Character extraction by alphabets/ranges.
379
+ """
380
+
381
+ results: list[tuple[str, float]] = []
382
+ ignore_non_latin: bool = False
383
+
384
+ sufficient_match_count: int = 0
385
+
386
+ lg_inclusion_list = lg_inclusion.split(",") if lg_inclusion is not None else []
387
+ if "Latin Based" in lg_inclusion_list:
388
+ ignore_non_latin = True
389
+ lg_inclusion_list.remove("Latin Based")
390
+
391
+ for layer in alpha_unicode_split(decoded_sequence):
392
+ sequence_frequencies: TypeCounter[str] = Counter(layer)
393
+ most_common = sequence_frequencies.most_common()
394
+
395
+ character_count: int = len(layer)
396
+
397
+ if character_count <= TOO_SMALL_SEQUENCE:
398
+ continue
399
+
400
+ popular_character_ordered: list[str] = [c for c, o in most_common]
401
+
402
+ for language in lg_inclusion_list or alphabet_languages(
403
+ popular_character_ordered, ignore_non_latin
404
+ ):
405
+ ratio: float = characters_popularity_compare(
406
+ language, popular_character_ordered
407
+ )
408
+
409
+ if ratio < threshold:
410
+ continue
411
+ elif ratio >= 0.8:
412
+ sufficient_match_count += 1
413
+
414
+ results.append((language, round(ratio, 4)))
415
+
416
+ if sufficient_match_count >= 3:
417
+ break
418
+
419
+ return sorted(
420
+ filter_alt_coherence_matches(results), key=lambda x: x[1], reverse=True
421
+ )
@@ -0,0 +1,8 @@
1
+ from __future__ import annotations
2
+
3
+ from .__main__ import cli_detect, query_yes_no
4
+
5
+ __all__ = (
6
+ "cli_detect",
7
+ "query_yes_no",
8
+ )