From 5b9db22f392eaca6426a84f700d58110a13f8a26 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Fri, 29 Oct 2021 11:02:38 +0200 Subject: [PATCH] open files by drag&drop --- main.py | 13 +++++++++++++ scribble.py | 49 +++++++------------------------------------------ urlutils.py | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 42 deletions(-) create mode 100644 urlutils.py diff --git a/main.py b/main.py index e2a615b..ce70d33 100644 --- a/main.py +++ b/main.py @@ -8,11 +8,13 @@ from PyQt6.QtCore import * from PyQt6.QtGui import * import sys +import urlutils from aboutdialog import AboutDialog from ravenui import RavenUI from settings import Settings from settingsstore import SettingsStore from tabs import Tabs +from urlutils import url_is_file MAX_LINE_LENGTH = 4096 @@ -28,6 +30,7 @@ class MainWindow(QMainWindow): self.setWindowTitle(self.tr("RavenLog")) self.setGeometry(0, 0, 640, 480) self.setDockNestingEnabled(True) + self.setAcceptDrops(True) self.tabs = Tabs(self.settings) self.tabs.create_tab("/home/andi/ws/performanceDb/data/production/logs_2018-09-06_2018-09-06.csv") @@ -121,6 +124,16 @@ class MainWindow(QMainWindow): self.settings.set_session('general', 'recent_files', recent_files) self._update_recent_files_menu() + def dragEnterEvent(self, e: QDragEnterEvent): + if e.mimeData().hasFormat('text/plain') and url_is_file(e.mimeData().text()): + e.accept() + else: + e.ignore() + + def dropEvent(self, e): + file = urlutils.url_to_path(e.mimeData().text()) + self._open_file(file) + def closeEvent(self, event): self.destruct() diff --git a/scribble.py b/scribble.py index 5d9e892..c16c6b1 100644 --- a/scribble.py +++ b/scribble.py @@ -1,44 +1,9 @@ +import os +import urllib +from urllib.parse import urlparse -import multiprocessing -import time - -from multiprocessing import Pool - -file = "/tmp/tmp123123" - -from concurrent.futures import ThreadPoolExecutor -from time import sleep - -def writer(): - with open(file, "w+b") as f: - for i in range(0, 10): - f.write(("%s"%(i)).encode("utf8")) - time.sleep(0.5) - print("writing ", time.time()) - -def reader(): - with open(file, "rb") as f: - for i in range(0,10): - - lines = f.readlines() - time.sleep(0.5) - print("read ", time.time()) - -pool = Pool(5) - -with pool as p: - p.apply_async(writer) - p.apply_async(reader) - -pool.join() - - -executor = ThreadPoolExecutor(2) -future1 = executor.submit(writer) -future2 = executor.submit(reader) - -print(future1.result()) -print(future2.result()) - - +url = "file:///home/andi/.local/share/ravenlog/session.ini" +p = urlparse(url) +final_path = os.path.abspath(os.path.join(p.netloc, p.path)) +print(final_path) diff --git a/urlutils.py b/urlutils.py new file mode 100644 index 0000000..d8cc47a --- /dev/null +++ b/urlutils.py @@ -0,0 +1,14 @@ +import os +from urllib.parse import urlparse + + +def url_to_path(url: str) -> str: + p = urlparse(url) + return os.path.abspath(os.path.join(p.netloc, p.path)) + + +def url_is_file(url: str) -> bool: + if url.startswith("file://"): + path = url_to_path(url) + return os.path.isfile(path) + return False