2022-10-04 03:14:18 +00:00
|
|
|
# Python imports
|
2023-02-21 20:23:39 +00:00
|
|
|
import time
|
2022-11-29 04:34:13 +00:00
|
|
|
import threading
|
|
|
|
import subprocess
|
|
|
|
import signal
|
|
|
|
import json
|
|
|
|
import shlex
|
2022-11-30 04:34:25 +00:00
|
|
|
from datetime import datetime
|
2022-10-04 03:14:18 +00:00
|
|
|
|
|
|
|
# Lib imports
|
2022-10-10 01:59:44 +00:00
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2022-11-29 04:34:13 +00:00
|
|
|
from gi.repository import Gtk
|
|
|
|
from gi.repository import GLib
|
2022-10-04 03:14:18 +00:00
|
|
|
|
|
|
|
# Application imports
|
|
|
|
from ..widgets.grep_preview_widget import GrepPreviewWidget
|
|
|
|
|
|
|
|
|
|
|
|
# NOTE: Threads WILL NOT die with parent's destruction.
|
|
|
|
def threaded(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=False).start()
|
2022-10-07 01:48:44 +00:00
|
|
|
|
2022-10-04 03:14:18 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
# NOTE: Threads WILL die with parent's destruction.
|
|
|
|
def daemon_threaded(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
|
2022-10-07 01:48:44 +00:00
|
|
|
|
2022-10-04 03:14:18 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
class GrepSearchMixin:
|
|
|
|
def _run_grep_query(self, widget=None, eve=None):
|
2023-02-21 19:16:53 +00:00
|
|
|
self._queue_grep = True
|
|
|
|
|
|
|
|
if not self._grep_watcher_running:
|
|
|
|
self._grep_watcher_running = True
|
|
|
|
|
|
|
|
self._stop_grep_query()
|
|
|
|
self.reset_grep_box()
|
|
|
|
self.run_grep_watcher(query=widget)
|
2022-10-04 03:14:18 +00:00
|
|
|
|
|
|
|
@daemon_threaded
|
2023-02-21 19:16:53 +00:00
|
|
|
def run_grep_watcher(self, query):
|
|
|
|
while True:
|
|
|
|
if self._queue_grep:
|
|
|
|
self._queue_grep = False
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# NOTE: Hold call to translate if we're still typing/updating...
|
|
|
|
if self._queue_grep:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# NOTE: If query create new process and do all new loop.
|
|
|
|
if query:
|
|
|
|
self.pause_fifo_update = False
|
|
|
|
GLib.idle_add(self._exec_grep_query, query)
|
|
|
|
|
|
|
|
self._grep_watcher_running = False
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
def _stop_grep_query(self, widget=None, eve=None):
|
2023-03-05 03:45:29 +00:00
|
|
|
self._spinner.stop()
|
|
|
|
|
2022-10-04 03:14:18 +00:00
|
|
|
# NOTE: Freeze IPC consumption
|
|
|
|
self.pause_fifo_update = True
|
2022-10-05 03:58:27 +00:00
|
|
|
self.grep_query = ""
|
2022-11-30 04:34:25 +00:00
|
|
|
dt = datetime.now()
|
|
|
|
self.grep_time_stamp = datetime.timestamp(dt) # NOTE: Get timestamp
|
2022-10-04 03:14:18 +00:00
|
|
|
|
|
|
|
# NOTE: Kill the former process
|
|
|
|
if self._grep_proc:
|
2022-10-07 01:48:44 +00:00
|
|
|
if self._grep_proc.poll() == None:
|
|
|
|
self._grep_proc.terminate()
|
|
|
|
while self._grep_proc.poll() == None:
|
|
|
|
...
|
2022-10-04 03:14:18 +00:00
|
|
|
|
2022-10-07 01:48:44 +00:00
|
|
|
self._grep_proc = None
|
2022-10-04 03:14:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _exec_grep_query(self, widget=None, eve=None):
|
|
|
|
query = widget.get_text()
|
2023-01-29 06:10:09 +00:00
|
|
|
|
2022-10-10 01:59:44 +00:00
|
|
|
if not query.strip() in ("", None):
|
2022-10-05 03:58:27 +00:00
|
|
|
self.grep_query = query
|
|
|
|
|
2022-10-04 03:14:18 +00:00
|
|
|
target_dir = shlex.quote( self._fm_state.tab.get_current_directory() )
|
|
|
|
command = ["python", f"{self.path}/utils/search.py", "-t", "grep_search", "-d", f"{target_dir}", "-q", f"{query}"]
|
2023-03-05 03:45:29 +00:00
|
|
|
self._spinner.start()
|
2022-10-04 03:14:18 +00:00
|
|
|
self._grep_proc = subprocess.Popen(command, cwd=self.path, stdin=None, stdout=None, stderr=None)
|
|
|
|
|
|
|
|
def _load_grep_ui(self, data):
|
2022-10-10 01:59:44 +00:00
|
|
|
Gtk.main_iteration()
|
|
|
|
|
2022-10-05 03:58:27 +00:00
|
|
|
if not data in ("", None):
|
2022-10-04 03:14:18 +00:00
|
|
|
jdata = json.loads( data )
|
|
|
|
jkeys = jdata.keys()
|
|
|
|
for key in jkeys:
|
|
|
|
sub_keys = jdata[key].keys()
|
|
|
|
grep_result = jdata[key]
|
|
|
|
|
2022-10-05 03:58:27 +00:00
|
|
|
widget = GrepPreviewWidget(key, sub_keys, grep_result, self.grep_query)
|
|
|
|
self._grep_list.add(widget)
|