open files by drag&drop

This commit is contained in:
2021-10-29 11:02:38 +02:00
parent 6653f2ae89
commit 5b9db22f39
3 changed files with 34 additions and 42 deletions

13
main.py
View File

@@ -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()

View File

@@ -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)

14
urlutils.py Normal file
View File

@@ -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