-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzzy_searcher.py
More file actions
84 lines (67 loc) · 2.38 KB
/
fuzzy_searcher.py
File metadata and controls
84 lines (67 loc) · 2.38 KB
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
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QListWidgetItem
import os
from pathlib import Path
import re
class SearchItem(QListWidgetItem):
def __init__(self, name, full_path, lineno, end, line):
self.name = name
self.full_path = full_path
self.lineno = lineno
self.end = end
self.line = line
self.formatted = f'{self.name}:{self.lineno}:{self.end} - {self.line} ...'
super().__init__(self.formatted)
def __str__(self):
return self.formatted
def __repr__(self):
return self.formatted
class SearchWorker(QThread):
finished = pyqtSignal(list)
def __init__(self):
super(SearchWorker, self).__init__(None)
self.items = []
self.search_file: str = None
self.search_text: str = None
def search(self):
debug = False
self.items = []
exclude_files = set([".svg", ".png", ".exe", ".pyc", ".qm"])
if not os.path.isfile(self.search_file):
return
file_ = os.path.basename(self.search_file)
full_path = os.path.abspath(self.search_file)
if Path(file_).suffix in exclude_files:
return
try:
with open(full_path, 'r', encoding='utf8') as f:
try:
reg = re.compile(self.search_text, re.IGNORECASE)
for i, line in enumerate(f):
if m := reg.search(line):
fd = SearchItem(
file_,
full_path,
i,
m.end(),
line[m.start():].strip()[:50], # limiting to 50 chars
)
self.items.append(fd)
except re.error as e:
if debug:
print(e)
except UnicodeDecodeError as e:
if debug:
print(e)
return
self.finished.emit(self.items)
def run(self):
self.search()
def update(self, pattern, file_path, full_path):
self.search_text = pattern
self.search_file = file_path
self.full_path= full_path
print(self.search_text)
print(self.search_file)
print(f"full path {full_path}")
self.start()