-
Notifications
You must be signed in to change notification settings - Fork 27
/
util.py
167 lines (144 loc) · 4.99 KB
/
util.py
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
from threading import Timer
from pathlib import Path
import os
import psutil
import platform
import base64
import cv2
import eel
from tools import bundle_dir
try:
from winreg import HKEY_CURRENT_USER, OpenKey, QueryValueEx
except ImportError:
if platform.system() == 'Windows':
print('failed to import winreg')
class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
self.start()
def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)
def start(self):
if not self.is_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False
def reset(self):
if self.is_running:
self.stop()
self.start()
def create_directory_if_not_exists(filename):
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
@eel.expose
def open_folder_by_relative_path(relative_path):
platform_name = platform.system()
if platform_name == 'Windows':
path = os.path.realpath(str(Path(bundle_dir, relative_path)))
os.startfile(path)
def base64_to_image(base64string, path):
image_path = base64_to_image_path(base64string, path)
img = cv2.imread(image_path)
return img
# Saves base64 image string and returns path
def base64_to_image_path(base64string, path):
with open(path, "wb") as fh:
fh.write(base64.b64decode(base64string))
return path
def get_default_browser_name():
platform_name = platform.system()
if platform_name == 'Windows':
with OpenKey(HKEY_CURRENT_USER, r"Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice") as key:
browser = QueryValueEx(key, 'Progid')[0]
browser_map = {
'ChromeHTML': 'chrome',
'FirefoxURL': 'chromium',
'IE.HTTP': 'edge'
}
if browser in browser_map:
return browser_map[browser]
else:
return 'chromium'
return 'chrome'
def get_PID_list():
if platform.system() == 'Darwin': # Not available for Mac
return []
processes = [proc.name() + ' ' + str(proc.pid) for proc in psutil.process_iter()]
processes.sort()
pids = []
for process in processes:
name = process.split(' ')[0]
pid = process.split(' ')[1]
if len(pids) == 0:
pids.append({'name': name, 'pids':[pid]})
elif name != pids[-1]['name']:
pids.append({'name': name, 'pids':[pid]})
else:
pids[-1]['pids'].append(pid)
return pids
def remove_spaces(s):
return "".join(s.split())
def remove_duplicate_characters(sentence):
chars = list(sentence)
prev = None
k = 0
for c in sentence:
if prev != c:
chars[k] = c
prev = c
k = k + 1
return ''.join(chars[:k])
def quick_remove_repeated_phrases(s):
prefix_array=[]
for i in range(len(s)):
prefix_array.append(s[:i])
#stop at 1st element to avoid checking for the ' ' char
for i in prefix_array[:1:-1]:
if s.count(i) > 1 :
#find where the next repetition starts
offset = s[len(i):].find(i)
return s[:len(i)+offset]
break
return s
def brute_remove_repeated_phrases(sentence):
head = 1
while 1:
scan_sentence = sentence[head:len(sentence)]
prefix = sentence[0:head]
if prefix in scan_sentence:
sentence = sentence.replace(prefix, '', 1)
head = 0
pass
head += 1
if head == len(sentence):
break
return sentence
def quick_and_brute_remove_repeated_phrases(sentence):
return brute_remove_repeated_phrases(quick_remove_repeated_phrases(sentence))
def format_output(output_objects, remove_repeat_mode, is_remove_duplicates, is_remove_spaces):
remove_repeat_dict = {
'quick': quick_remove_repeated_phrases,
'brute force': brute_remove_repeated_phrases,
'quick + brute force': quick_and_brute_remove_repeated_phrases
}
for output in output_objects:
output['text'] = output['text'].strip()
if remove_repeat_mode in remove_repeat_dict:
remove_repeat_dict[remove_repeat_mode](output['text'])
output['text'] = remove_duplicate_characters(output['text']) if is_remove_duplicates else output['text']
output['text'] = remove_spaces(output['text']) if is_remove_spaces else output['text']
return output_objects