-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuiev.pyx
10921 lines (9461 loc) · 377 KB
/
uiev.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
cimport cython
cimport numpy as np
from ast import literal_eval
from collections import defaultdict
from collections.abc import Iterable
from contextlib import suppress as contextlib_suppress
from functools import lru_cache
from functools import reduce
from io import StringIO,BytesIO
from itertools import takewhile
from libc.stdint cimport int64_t,uint8_t
from libc.stdio cimport fputc,fclose,fprintf,fopen,FILE
from libcpp.string cimport string,npos
from libcpp.unordered_map cimport unordered_map
from libcpp.utility cimport pair
from libcpp.vector cimport vector
from operator import getitem as operator_getitem
from operator import itemgetter as operator_itemgetter
from os import environ as os_environ
from pandas import isna as pdisna
from pandas import read_csv
from pandas.core.base import PandasObject
from pandas.core.frame import DataFrame, Series, Index
from platform import platform
from random import randint as random_randint
from string import printable as string_printable
from struct import Struct
from struct import pack as structpack
from subprocess import PIPE, DEVNULL
from subprocess import Popen as subprocess_Popen
from subprocess import run as subprocess_run
from tempfile import NamedTemporaryFile
from time import sleep as timesleep
from types import GeneratorType
from unicodedata import name as unicodedata_name
from zlib import compress as zlib_compress
from zlib import crc32 as zlib_crc32
from random import uniform
from base64 import b64encode
import collections
import ctypes
import cython
import numpy as np
import os
import pandas as pd
import re as repy
import regex as re
import requests
import shutil
import subprocess
import sys, subprocess
import typing
import warnings
import zipfile
import traceback
from pandas import DataFrame as pd_DataFrame
from threading import Timer
re.cache_all(True)
warnings.simplefilter(action='ignore', category=pd.errors.PerformanceWarning)
ctypedef struct color_rgb_with_coords_and_count:
Py_ssize_t x
Py_ssize_t y
Py_ssize_t count
uint8_t r
uint8_t g
uint8_t b
ctypedef vector[color_rgb_with_coords_and_count] vec_rgbxycount
cdef:
object opened_adb_log
dict[str,bytes] latin_keycombination = {
# ascii
"!":b"input text '!'",
'"':b"""input text '"'""",
"#":b"input text '#'",
"$":b"input text '$'",
"%":b"input text '%'",
"&":b"input text '&'",
"'":b'''input text "'"''',
"(":b"input text '('",
")":b"input text ')'",
"*":b"input text '*'",
"+":b"input text '+'",
",":b"input text ','",
"-":b"input text '-'",
".":b"input text '.'",
"/":b"input text '/'",
"0":b"input text '0'",
"1":b"input text '1'",
"2":b"input text '2'",
"3":b"input text '3'",
"4":b"input text '4'",
"5":b"input text '5'",
"6":b"input text '6'",
"7":b"input text '7'",
"8":b"input text '8'",
"9":b"input text '9'",
":":b"input text ':'",
";":b"input text ';'",
"<":b"input text '<'",
"=":b"input text '='",
">":b"input text '>'",
"?":b"input text '?'",
"@":b"input text '@'",
"A":b"input text 'A'",
"B":b"input text 'B'",
"C":b"input text 'C'",
"D":b"input text 'D'",
"E":b"input text 'E'",
"F":b"input text 'F'",
"G":b"input text 'G'",
"H":b"input text 'H'",
"I":b"input text 'I'",
"J":b"input text 'J'",
"K":b"input text 'K'",
"L":b"input text 'L'",
"M":b"input text 'M'",
"N":b"input text 'N'",
"O":b"input text 'O'",
"P":b"input text 'P'",
"Q":b"input text 'Q'",
"R":b"input text 'R'",
"S":b"input text 'S'",
"T":b"input text 'T'",
"U":b"input text 'U'",
"V":b"input text 'V'",
"W":b"input text 'W'",
"X":b"input text 'X'",
"Y":b"input text 'Y'",
"Z":b"input text 'Z'",
"[":b"input text '['",
"\\":b"input text '\\'",
"]":b"input text ']'",
"^":b"input text '^'",
"_":b"input text '_'",
"`":b"input text '`'",
"a":b"input text 'a'",
"b":b"input text 'b'",
"c":b"input text 'c'",
"d":b"input text 'd'",
"e":b"input text 'e'",
"f":b"input text 'f'",
"g":b"input text 'g'",
"h":b"input text 'h'",
"i":b"input text 'i'",
"j":b"input text 'j'",
"k":b"input text 'k'",
"l":b"input text 'l'",
"m":b"input text 'm'",
"n":b"input text 'n'",
"o":b"input text 'o'",
"p":b"input text 'p'",
"q":b"input text 'q'",
"r":b"input text 'r'",
"s":b"input text 's'",
"t":b"input text 't'",
"u":b"input text 'u'",
"v":b"input text 'v'",
"w":b"input text 'w'",
"x":b"input text 'x'",
"y":b"input text 'y'",
"z":b"input text 'z'",
"{":b"input text '{'",
"|":b"input text '|'",
"}":b"input text '}'",
"~":b"input text '~'",
# https://www.ut.edu/academics/college-of-arts-and-letters/department-of-languages-and-linguistics/typing-accented-characters
# á, é, í, ó, ú, ý, Á, É, Í, Ó, Ú, Ý
"á":b"input keycombination 58 33;input text 'a'",
"é":b"input keycombination 58 33;input text 'e'",
"í":b"input keycombination 58 33;input text 'i'",
"ó":b"input keycombination 58 33;input text 'o'",
"ú":b"input keycombination 58 33;input text 'u'",
"ý":b"input keycombination 58 33;input text 'y'",
"Á":b"input keycombination 58 33;input text 'A'",
"É":b"input keycombination 58 33;input text 'E'",
"Í":b"input keycombination 58 33;input text 'I'",
"Ó":b"input keycombination 58 33;input text 'O'",
"Ú":b"input keycombination 58 33;input text 'U'",
"Ý":b"input keycombination 58 33;input text 'Y'",
# ç, Ç
"Ç" :b"input keycombination 59 57 31",
"ç" :b"input keycombination 57 31",
# â, ê, î, ô, û, Â, Ê, Î, Ô, Û
"â":b"input keycombination 57 37;input text 'a'",
"ê":b"input keycombination 57 37;input text 'e'",
"î":b"input keycombination 57 37;input text 'i'",
"ô":b"input keycombination 57 37;input text 'o'",
"û":b"input keycombination 57 37;input text 'u'",
"Â":b"input keycombination 57 37;input text 'A'",
"Ê":b"input keycombination 57 37;input text 'E'",
"Î":b"input keycombination 57 37;input text 'I'",
"Ô":b"input keycombination 57 37;input text 'O'",
"Û":b"input keycombination 57 37;input text 'U'",
# ã, ñ, õ, Ã, Ñ, Õ
"ã":b"input keycombination 57 42;input text 'a'",
"ñ":b"input keycombination 57 42;input text 'n'",
"õ":b"input keycombination 57 42;input text 'o'",
"Ã":b"input keycombination 57 42;input text 'A'",
"Ñ":b"input keycombination 57 42;input text 'N'",
"Õ":b"input keycombination 57 42;input text 'O'",
# ß, ẞ
"ß": b"input keycombination 57 47",
"ẞ": b"input keycombination 59 57 47",
# ä, ë, ï, ö, ü, ÿ, Ä, Ë, Ï, Ö, Ü, Ÿ
"ä":b"input keycombination 57 49;input text 'a'",
"ë":b"input keycombination 57 49;input text 'e'",
"ï":b"input keycombination 57 49;input text 'i'",
"ö":b"input keycombination 57 49;input text 'o'",
"ü":b"input keycombination 57 49;input text 'u'",
"ÿ":b"input keycombination 57 49;input text 'y'",
"Ä":b"input keycombination 57 49;input text 'A'",
"Ë":b"input keycombination 57 49;input text 'E'",
"Ï":b"input keycombination 57 49;input text 'I'",
"Ö":b"input keycombination 57 49;input text 'O'",
"Ü":b"input keycombination 57 49;input text 'U'",
"Ÿ":b"input keycombination 57 49;input text 'Y'",
# à, è, ì, ò, ù, À, È, Ì, Ò, Ù
"à":b"input keycombination 57 68;input text 'a'",
"è":b"input keycombination 57 68;input text 'e'",
"ì":b"input keycombination 57 68;input text 'i'",
"ò":b"input keycombination 57 68;input text 'o'",
"ù":b"input keycombination 57 68;input text 'u'",
"À":b"input keycombination 57 68;input text 'A'",
"È":b"input keycombination 57 68;input text 'E'",
"Ì":b"input keycombination 57 68;input text 'I'",
"Ò":b"input keycombination 57 68;input text 'O'",
"Ù":b"input keycombination 57 68;input text 'U'",
#todo
"å":b"input text 'a'",
"Å":b"input text 'a'",
"æ":b"input text 'ae'",
"Æ":b"input text 'Ae'",
"œ":b"input text 'oe'",
"Œ":b"input text 'Oe'",
"ð":b"input text 'd'",
"Ð":b"input text 'D'",
"ø":b"input text 'o'",
"Ø":b"input text 'O'",
"¿":b"input text '?'",
"¡":b"input text '!'",
}
string cpp_distance_metric=<string>b"Euclidean"
dict letter_lookup_dict = {}
string emptystring = <string>b""
vector[string] emptystringvec = [b""]
string ppm_header=<string>b"P6\n%d %d\n%d\n"
string write_binary=<string>b"wb"
int SIG_BOOLEAN = ord("Z")
int SIG_BYTE = ord("B")
int SIG_SHORT = ord("S")
int SIG_INT = ord("I")
int SIG_LONG = ord("J")
int SIG_FLOAT = ord("F")
int SIG_DOUBLE = ord("D")
int SIG_STRING = ord("R")
int SIG_MAP = ord("M")
int SIG_END_MAP = 0
str PYTHON_STRUCT_UNPACK_SIG_BOOLEAN = "?"
str PYTHON_STRUCT_UNPACK_SIG_BYTE = "b"
str PYTHON_STRUCT_UNPACK_SIG_SHORT = "h"
str PYTHON_STRUCT_UNPACK_SIG_INT = "i"
str PYTHON_STRUCT_UNPACK_SIG_LONG = "q"
str PYTHON_STRUCT_UNPACK_SIG_FLOAT = "f"
str PYTHON_STRUCT_UNPACK_SIG_DOUBLE = "d"
str PYTHON_STRUCT_UNPACK_SIG_STRING = "s"
str LITTLE_OR_BIG = ">"
object STRUCT_UNPACK_SIG_BOOLEAN = Struct(
f"{LITTLE_OR_BIG}{PYTHON_STRUCT_UNPACK_SIG_BOOLEAN}"
).unpack
object STRUCT_UNPACK_SIG_BYTE = Struct(
f"{LITTLE_OR_BIG}{PYTHON_STRUCT_UNPACK_SIG_BYTE}"
).unpack
object STRUCT_UNPACK_SIG_SHORT = Struct(
f"{LITTLE_OR_BIG}{PYTHON_STRUCT_UNPACK_SIG_SHORT}"
).unpack
object STRUCT_UNPACK_SIG_INT = Struct(
f"{LITTLE_OR_BIG}{PYTHON_STRUCT_UNPACK_SIG_INT}"
).unpack
object STRUCT_UNPACK_SIG_LONG = Struct(
f"{LITTLE_OR_BIG}{PYTHON_STRUCT_UNPACK_SIG_LONG}"
).unpack
object STRUCT_UNPACK_SIG_FLOAT = Struct(
f"{LITTLE_OR_BIG}{PYTHON_STRUCT_UNPACK_SIG_FLOAT}"
).unpack
object STRUCT_UNPACK_SIG_DOUBLE = Struct(
f"{LITTLE_OR_BIG}{PYTHON_STRUCT_UNPACK_SIG_DOUBLE}"
).unpack
object asciifunc = np.frompyfunc(ascii, 1, 1)
object reprfunc = np.frompyfunc(repr, 1, 1)
str ResetAll = "\033[0m"
str LightRed = "\033[91m"
str LightGreen = "\033[92m"
str LightYellow = "\033[93m"
str LightBlue = "\033[94m"
str LightMagenta = "\033[95m"
str LightCyan = "\033[96m"
str White = "\033[97m"
str this_folder = os.path.dirname(__file__)
bint iswindows = "win" in platform().lower()
str url_fragment_parser = "https://github.com/hansalemaos/android_fragment_parser/raw/refs/heads/main/fragmentdumper.cpp"
str cpp_file_pure_fragment = "fragmentdumper.cpp"
str cpp_file_fragment = os.path.join(this_folder, cpp_file_pure_fragment)
str exe_file_pure_fragment = "fragmentdumper.exe" if iswindows else "fragmentdumper_exe"
str exe_file_fragment = os.path.join(this_folder, exe_file_pure_fragment)
str url_ui2_parser = "https://github.com/hansalemaos/uiautomator2tocsv/raw/refs/heads/main/uiautomator2parser.cpp"
str cpp_file_pure_ui2 = "uiautomator2parser.cpp"
str cpp_file_ui2 = os.path.join(this_folder, cpp_file_pure_ui2)
str exe_file_pure_ui2 = "uiautomator2parser.exe" if iswindows else "uiautomator2parser_exe"
str exe_file_ui2 = os.path.join(this_folder, exe_file_pure_ui2)
str url_ui1_parser = "https://raw.githubusercontent.com/hansalemaos/uiautomator_dump_to_csv/refs/heads/main/uiautomatornolimit.cpp"
str cpp_file_pure_ui1 = "uiautomatornolimit.cpp"
str cpp_file_ui1 = os.path.join(this_folder, cpp_file_pure_ui1)
str exe_file_pure_ui1 = "uiautomatornolimit.exe" if iswindows else "uiautomatornolimit_exe"
str exe_file_ui1 = os.path.join(this_folder, exe_file_pure_ui1)
str url_tesser_parser = "https://github.com/hansalemaos/tesseract_hocr_to_csv/raw/refs/heads/main/hocr2csv.cpp"
str cpp_file_pure_tesser = "hocr2csv.cpp"
str cpp_file_tesser = os.path.join(this_folder, cpp_file_pure_tesser)
str exe_file_pure_tesser = "hocr2csv.exe" if iswindows else "hocr2csv_exe"
str exe_file_tesser = os.path.join(this_folder, exe_file_pure_tesser)
dict[str,object] invisibledict = {"shell":False, "env":os_environ}
dict cache_apply_literal_eval_to_tuple = {}
list[str] columns_ui2 = [
"aa_index",
"aa_indent",
"aa_text",
"aa_resource_id",
"aa_clazz",
"aa_package",
"aa_content_desc",
"aa_checkable",
"aa_checked",
"aa_clickable",
"aa_enabled",
"aa_focusable",
"aa_focused",
"aa_scrollable",
"aa_long_clickable",
"aa_password",
"aa_selected",
"aa_visible_to_user",
"aa_bounds",
"aa_drawing_order",
"aa_hint",
"aa_display_id",
"aa_line_index",
"aa_children",
"aa_parents",
"aa_start_x",
"aa_start_y",
"aa_end_x",
"aa_end_y",
"aa_center_x",
"aa_center_y",
"aa_width",
"aa_height",
"aa_area",
"aa_w_h_relation",
]
list[str] columns_fragments = [
"aa_my_id",
"aa_my_group_id",
"aa_my_element_id",
"aa_my_direct_parent_id",
"aa_my_parent_ids",
"aa_original_string",
"aa_center_x",
"aa_center_y",
"aa_area",
"aa_start_x",
"aa_start_y",
"aa_end_x",
"aa_end_y",
"aa_height",
"aa_width",
"aa_is_sqare",
"aa_rel_width_height",
"aa_hashcode_int",
"aa_mid_int",
"aa_spaces",
"aa_classname",
"aa_element_id",
"aa_hashcode",
"aa_mid",
"aa_start_x_relative",
"aa_end_x_relative",
"aa_start_y_relative",
"aa_end_y_relative",
"aa_clickable",
"aa_context_clickable",
"aa_drawn",
"aa_enabled",
"aa_focusable",
"aa_long_clickable",
"aa_pflag_activated",
"aa_pflag_dirty_mask",
"aa_pflag_focused",
"aa_pflag_hovered",
"aa_pflag_invalidated",
"aa_pflag_is_root_namespace",
"aa_pflag_prepressed",
"aa_pflag_selected",
"aa_scrollbars_horizontal",
"aa_scrollbars_vertical",
"aa_visibility",
]
object windll=None
object ntdll=None
object kernel32=None
object _GetShortPathNameW=None
str adb_connect_exe="adbconnect.exe" if iswindows else "adbconnect_exe"
str adb_connect_exe_full = os.path.join(this_folder, adb_connect_exe)
object regex_dev = re.compile(rb"(^.*?)\s{2,}device\s+(.*?)$")
if iswindows:
from ctypes import wintypes
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
creationflags = subprocess.CREATE_NO_WINDOW
invisibledict = {
"startupinfo": startupinfo,
"creationflags": creationflags,
"start_new_session": True,
}
windll = ctypes.LibraryLoader(ctypes.WinDLL)
ntdll = windll.ntdll
kernel32 = windll.kernel32
_GetShortPathNameW = kernel32.GetShortPathNameW
_GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
_GetShortPathNameW.restype = wintypes.DWORD
class JustColors:
def __init__(self):
self.BOLD = "\033[1m"
self.ITALIC = "\033[3m"
self.UNDERLINE = "\033[4m"
self.UNDERLINE_THICK = "\033[21m"
self.HIGHLIGHTED = "\033[7m"
self.HIGHLIGHTED_BLACK = "\033[40m"
self.HIGHLIGHTED_RED = "\033[41m"
self.HIGHLIGHTED_GREEN = "\033[42m"
self.HIGHLIGHTED_YELLOW = "\033[43m"
self.HIGHLIGHTED_BLUE = "\033[44m"
self.HIGHLIGHTED_PURPLE = "\033[45m"
self.HIGHLIGHTED_CYAN = "\033[46m"
self.HIGHLIGHTED_GREY = "\033[47m"
self.HIGHLIGHTED_GREY_LIGHT = "\033[100m"
self.HIGHLIGHTED_RED_LIGHT = "\033[101m"
self.HIGHLIGHTED_GREEN_LIGHT = "\033[102m"
self.HIGHLIGHTED_YELLOW_LIGHT = "\033[103m"
self.HIGHLIGHTED_BLUE_LIGHT = "\033[104m"
self.HIGHLIGHTED_PURPLE_LIGHT = "\033[105m"
self.HIGHLIGHTED_CYAN_LIGHT = "\033[106m"
self.HIGHLIGHTED_WHITE_LIGHT = "\033[107m"
self.STRIKE_THROUGH = "\033[9m"
self.MARGIN_1 = "\033[51m"
self.MARGIN_2 = "\033[52m"
self.BLACK = "\033[30m"
self.RED_DARK = "\033[31m"
self.GREEN_DARK = "\033[32m"
self.YELLOW_DARK = "\033[33m"
self.BLUE_DARK = "\033[34m"
self.PURPLE_DARK = "\033[35m"
self.CYAN_DARK = "\033[36m"
self.GREY_DARK = "\033[37m"
self.BLACK_LIGHT = "\033[90m"
self.RED = "\033[91m"
self.GREEN = "\033[92m"
self.YELLOW = "\033[93m"
self.BLUE = "\033[94m"
self.PURPLE = "\033[95m"
self.CYAN = "\033[96m"
self.WHITE = "\033[97m"
self.DEFAULT = "\033[0m"
mycolors = JustColors()
cpdef printincolor(values, color=None, print_to_stderr=False):
s1 = "GOT AN ERROR DURING PRINTING"
if color:
try:
s1 = "%s%s%s" % (color, values, mycolors.DEFAULT)
except Exception:
if isinstance(values, bytes):
s1 = "%s%s%s" % (
color,
values.decode("utf-8", "backslashreplace"),
mycolors.DEFAULT,
)
else:
s1 = "%s%s%s" % (color, repr(values), mycolors.DEFAULT)
if print_to_stderr:
sys.stderr.flush()
sys.stderr.write(f"{s1}\n")
sys.stderr.flush()
else:
print(s1)
else:
try:
s1 = "%s%s" % (values, mycolors.DEFAULT)
except Exception:
if isinstance(values, bytes):
s1 = "%s%s" % (
values.decode("utf-8", "backslashreplace"),
mycolors.DEFAULT,
)
else:
s1 = "%s%s" % (repr(values), mycolors.DEFAULT)
if print_to_stderr:
sys.stderr.flush()
sys.stderr.write(f"{s1}\n")
sys.stderr.flush()
else:
print(s1)
def errwrite(*args, **kwargs):
symbol_top = kwargs.pop("symbol_top", "╦")
symbol_bottom = kwargs.pop("symbol_bottom", "╩")
len_top = kwargs.pop("len_top", "60")
len_bottom = kwargs.pop("len_bottom", "60")
color_top = kwargs.pop("color_top", "YELLOW_DARK")
color_bottom = kwargs.pop("color_bottom", "RED_DARK")
print_to_stderr = kwargs.pop("print_to_stderr", False)
color_exception = kwargs.pop("color_exception", "CYAN")
color2print_top = None
color2print_bottom = None
color_exceptionmiddle = None
try:
color2print_top = mycolors.__dict__.get(
color_top, mycolors.__dict__.get("YELLOW_DARK")
)
color2print_bottom = mycolors.__dict__.get(
color_bottom, mycolors.__dict__.get("RED_DARK")
)
color_exceptionmiddle = mycolors.__dict__.get(
color_exception, mycolors.__dict__.get("CYAN")
)
except Exception as e:
print(e)
printincolor(
values="".join(symbol_top * int(len_top)),
color=color2print_top,
print_to_stderr=print_to_stderr,
)
etype, value, tb = sys.exc_info()
lines = traceback.format_exception(etype, value, tb)
try:
if print_to_stderr:
sys.stderr.flush()
sys.stderr.write("".join(lines))
sys.stderr.flush()
else:
printincolor(
"".join(lines),
color=color_exceptionmiddle,
print_to_stderr=print_to_stderr,
)
except Exception:
print("".join(lines))
printincolor(
"".join(symbol_bottom * int(len_bottom)),
color=color2print_bottom,
print_to_stderr=print_to_stderr,
)
cdef extern from "fuzzmatcher.h" nogil :
cdef cppclass StringMatcher:
StringMatcher(vector[string]&, vector[string]&)
void _load_vecs_for_cython(vector[string]*, vector[string]*)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_longest_common_substring_v1(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_longest_common_substring_v1(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_hemming_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_hemming_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_hemming_distance_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_hemming_distance_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_longest_common_substring_v0(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_longest_common_substring_v0(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_longest_common_subsequence_v0(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_longest_common_subsequence_v0(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_damerau_levenshtein_distance_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_damerau_levenshtein_distance_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_damerau_levenshtein_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_damerau_levenshtein_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_levenshtein_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_levenshtein_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_levenshtein_distance_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_levenshtein_distance_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_jaro_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_jaro_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_jaro_winkler_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_jaro_winkler_distance_1way(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_jaro_winkler_distance_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_jaro_winkler_distance_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_jaro_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_jaro_2ways(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_subsequence_v1(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_subsequence_v1(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ab_map_subsequence_v2(bint print_results, bint convert_to_csv, string file_path)
unordered_map[int64_t, unordered_map[int64_t, pair[double, int64_t]]] ba_map_subsequence_v2(bint print_results, bint convert_to_csv, string file_path)
StringMatcher& to_upper()
StringMatcher& to_lower()
StringMatcher& to_without_non_alphanumeric()
StringMatcher& to_without_non_printable()
StringMatcher& to_100_percent_copy()
StringMatcher& to_without_whitespaces()
StringMatcher& to_with_normalized_whitespaces()
void _str__for_cython();
cdef extern from "fuzzmatcher.h" namespace "stringhelpers" nogil :
vector[string] read_file_to_vector_lines(const string& filename)
void _repr__for_cython(vector[string]*v);
cdef void convert_to_stdvec(object stri, vector[string]& outvector):
cdef:
Py_ssize_t i
string converted_cpp
if isinstance(stri, (str, bytes)):
stri = [stri]
outvector.resize(len(stri))
outvector.clear()
for i in range(len(stri)):
converted_cpp=convert_python_object_to_cpp_string(stri[i])
outvector.emplace_back(converted_cpp)
@cython.final
cdef class PyStringMatcher:
cdef:
StringMatcher*sm
vector[string] stri1list
vector[string] stri2list
def __cinit__(self):
self.sm = new StringMatcher(emptystringvec,emptystringvec)
self.stri1list=[]
self.stri2list=[]
def __init__(self, object stri1, object stri2):
convert_to_stdvec(stri1,self.stri1list)
convert_to_stdvec(stri2,self.stri2list)
self.sm._load_vecs_for_cython(<vector[string]*>(&self.stri1list),<vector[string]*>(&self.stri2list))
def __dealloc__(self):
del self.sm
def _filter_results(self, dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r, bint ab = True, bint sort_reverse=False):
cdef:
dict[str,object] outdict={}
list sorteddict
Py_ssize_t index
bytes pystri1, pystri2
sorteddict=sorted([[tuple(x[1].values())[0][0],x] for x in r.items()], key=lambda y: y[0],reverse=sort_reverse)
for index in range(len(sorteddict)):
if ab:
pystri1=(self.stri1list[0][sorteddict[index][1][0]])
pystri2=(self.stri2list[0][tuple(sorteddict[index][1][1].keys())[0]])
outdict[index]={
"aa_match":sorteddict[index][0],
"aa_1_is_sub":tuple(sorteddict[index][1][1].values())[0][1],
"aa_index_1":sorteddict[index][1][0],
"aa_index_2":tuple(sorteddict[index][1][1].keys())[0],
"aa_str_1":pystri1.decode('utf-8','backslashreplace'),
"aa_str_2":pystri2.decode('utf-8','backslashreplace'),
}
else:
pystri1=(self.stri2list[0][sorteddict[index][1][0]])
pystri2=(self.stri1list[0][tuple(sorteddict[index][1][1].keys())[0]])
outdict[index]={
"aa_match":sorteddict[index][0],
"aa_2_is_sub":tuple(sorteddict[index][1][1].values())[0][1],
"aa_index_2":sorteddict[index][1][0],
"aa_index_1":tuple(sorteddict[index][1][1].keys())[0],
"aa_str_2":pystri1.decode('utf-8','backslashreplace'),
"aa_str_1":pystri2.decode('utf-8','backslashreplace'),
}
return outdict
cpdef ab_map_damerau_levenshtein_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_damerau_levenshtein_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=False)
cpdef ab_map_damerau_levenshtein_distance_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_damerau_levenshtein_distance_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=False)
cpdef ab_map_hemming_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_hemming_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=False)
cpdef ab_map_hemming_distance_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_hemming_distance_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=False)
cpdef ab_map_jaro_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_jaro_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ab_map_jaro_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_jaro_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ab_map_jaro_winkler_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_jaro_winkler_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ab_map_jaro_winkler_distance_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_jaro_winkler_distance_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ab_map_levenshtein_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_levenshtein_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=False)
cpdef ab_map_levenshtein_distance_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_levenshtein_distance_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=False)
cpdef ab_map_longest_common_subsequence_v0(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_longest_common_subsequence_v0(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ab_map_longest_common_substring_v0(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_longest_common_substring_v0(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ab_map_longest_common_substring_v1(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_longest_common_substring_v1(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ab_map_subsequence_v1(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_subsequence_v1(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ab_map_subsequence_v2(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ab_map_subsequence_v2(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=True,sort_reverse=True)
cpdef ba_map_damerau_levenshtein_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_damerau_levenshtein_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=False)
cpdef ba_map_damerau_levenshtein_distance_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_damerau_levenshtein_distance_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=False)
cpdef ba_map_hemming_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_hemming_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=False)
cpdef ba_map_hemming_distance_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_hemming_distance_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=False)
cpdef ba_map_jaro_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_jaro_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef ba_map_jaro_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_jaro_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef ba_map_jaro_winkler_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_jaro_winkler_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef ba_map_jaro_winkler_distance_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_jaro_winkler_distance_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef ba_map_levenshtein_distance_1way(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_levenshtein_distance_1way(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=False)
cpdef ba_map_levenshtein_distance_2ways(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_levenshtein_distance_2ways(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=False)
cpdef ba_map_longest_common_subsequence_v0(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_longest_common_subsequence_v0(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef ba_map_longest_common_substring_v0(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_longest_common_substring_v0(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef ba_map_longest_common_substring_v1(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_longest_common_substring_v1(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef ba_map_subsequence_v1(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_subsequence_v1(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef ba_map_subsequence_v2(self, bint print_cpp=False):
cdef:
dict[int64_t,dict[int64_t,tuple[double,int64_t]]] r
r =self.sm.ba_map_subsequence_v2(print_cpp,False,emptystring)
return self._filter_results(r=r,ab=False,sort_reverse=True)
cpdef cpp_data_to_upper(self):
self.sm.to_upper()
return self
cpdef cpp_data_to_lower(self):
self.sm.to_lower()
return self
cpdef cpp_data_to_without_non_alphanumeric(self):
self.sm.to_without_non_alphanumeric()
return self
cpdef cpp_data_to_without_non_printable(self):
self.sm.to_without_non_printable()
return self
cpdef cpp_data_to_100_percent_copy(self):
self.sm.to_100_percent_copy()
return self
cpdef cpp_data_to_without_whitespaces(self):
self.sm.to_without_whitespaces()
return self
cpdef cpp_data_to_with_normalized_whitespaces(self):
self.sm.to_with_normalized_whitespaces()
return self
cdef str get_tmpfile(str suffix):
cdef:
object tfp
str filename
tfp = NamedTemporaryFile(delete=False, suffix=suffix)
filename = tfp.name
filename = os.path.normpath(filename)
tfp.close()
return filename
cdef string convert_python_object_to_cpp_string(object shell_command):
cdef:
string cpp_shell_command
bytes tmp_bytes
if isinstance(shell_command,bytes):
cpp_shell_command=<string>shell_command
elif isinstance(shell_command,str):
tmp_bytes=shell_command.encode()
cpp_shell_command=<string>(tmp_bytes)
else:
tmp_bytes=str(shell_command).encode()
cpp_shell_command=<string>(tmp_bytes)
return cpp_shell_command
cpdef take_screenshot(list[str] cmd, int width, int height, dict kwargs):
try:
if iswindows:
return np.frombuffer(subprocess_run(cmd,**{**invisibledict,**kwargs, 'capture_output':True}).stdout.replace(b"\r\n",b"\n"), dtype=np.uint8,offset=16).reshape((height, width, 4))[...,[0,1,2]]
else:
return np.frombuffer(subprocess_run(cmd,**{**invisibledict,**kwargs, 'capture_output':True}).stdout, dtype=np.uint8,offset=16).reshape((height, width, 4))[...,[0,1,2]]
except Exception:
errwrite()
return np.array([],dtype=np.uint8)
################################################# START Pandas Printer ####################################################################
@cython.nonecheck(True)
cpdef pdp(
object df,
Py_ssize_t column_rep=70,
Py_ssize_t max_lines=0,
Py_ssize_t max_colwidth=300,
Py_ssize_t ljust_space=2,
str sep=" | ",
bint vtm_escape=True,
):
cdef:
dict[Py_ssize_t, np.ndarray] stringdict= {}
dict[Py_ssize_t, Py_ssize_t] stringlendict= {}
list[str] df_columns, allcolumns_as_string
list[str] colors2rotate=[
LightRed,
LightGreen,
LightYellow,
LightBlue,
LightMagenta,
LightCyan,
White,
]
Py_ssize_t i, len_a, len_df_columns, lenstr, counter, j, len_stringdict0, k, len_stringdict
str stringtoprint, dashes, dashesrep
np.ndarray a
if vtm_escape:
print('\033[12:2p')
if len(df) > max_lines and max_lines > 0:
a = df.iloc[:max_lines].reset_index(drop=False).T.__array__()
else:
a = df.iloc[:len(df)].reset_index(drop=False).T.__array__()
try:
df_columns = ["iloc"] + [str(x) for x in df.columns]
except Exception:
try:
df_columns = ["iloc",str(df.name)]
except Exception:
df_columns = ["iloc",str(0)]
len_a=len(a)
for i in range(len_a):
try:
stringdict[i] = reprfunc(a[i]).astype("U")
except Exception: