2024-11-13 10:22:53 +03:00
|
|
|
|
import sys
|
2024-11-12 19:26:38 +03:00
|
|
|
|
import os
|
|
|
|
|
|
import time
|
2024-11-13 10:22:53 +03:00
|
|
|
|
from PyQt5.QtWidgets import (
|
|
|
|
|
|
QApplication,
|
|
|
|
|
|
QMainWindow,
|
|
|
|
|
|
QTabWidget,
|
|
|
|
|
|
QWidget,
|
|
|
|
|
|
QVBoxLayout,
|
|
|
|
|
|
QLabel,
|
|
|
|
|
|
)
|
|
|
|
|
|
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QObject, QFileSystemWatcher
|
|
|
|
|
|
from PyQt5.QtGui import QIcon
|
2024-11-12 19:26:38 +03:00
|
|
|
|
|
2024-11-13 10:22:53 +03:00
|
|
|
|
# Импортируйте ваш класс `app` здесь
|
|
|
|
|
|
# Предполагается, что класс `app` предоставляет интерфейс для отображения данных
|
|
|
|
|
|
# Если `app` не предоставляет виджет, возможно, потребуется его модифицировать
|
|
|
|
|
|
from src.gui.app import graphWindow
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DirectoryWatcher(QObject):
|
|
|
|
|
|
|
|
|
|
|
|
file_created = pyqtSignal(str)
|
2024-11-12 19:26:38 +03:00
|
|
|
|
|
|
|
|
|
|
def __init__(self, directory_path):
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
self.directory_path = directory_path
|
2024-11-13 10:22:53 +03:00
|
|
|
|
self.watcher = QFileSystemWatcher()
|
|
|
|
|
|
self.watcher.addPath(self.directory_path)
|
|
|
|
|
|
self.existing_files = set(os.listdir(self.directory_path))
|
|
|
|
|
|
self.watcher.directoryChanged.connect(self.on_directory_changed)
|
|
|
|
|
|
|
|
|
|
|
|
def on_directory_changed(self, _):
|
|
|
|
|
|
try:
|
|
|
|
|
|
current_files = set(os.listdir(self.directory_path))
|
|
|
|
|
|
new_files = current_files - self.existing_files
|
|
|
|
|
|
self.existing_files = current_files
|
|
|
|
|
|
for file in new_files:
|
|
|
|
|
|
if file.lower().endswith(".csv"):
|
|
|
|
|
|
full_path = os.path.join(self.directory_path, file)
|
|
|
|
|
|
self.file_created.emit(full_path)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
print(f"Ошибка при обработке изменений директории: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MainWindow(QMainWindow):
|
|
|
|
|
|
def __init__(self, directory_to_watch):
|
2024-11-12 19:26:38 +03:00
|
|
|
|
super().__init__()
|
2024-11-13 10:22:53 +03:00
|
|
|
|
self.setWindowTitle("Мониторинг CSV-файлов")
|
|
|
|
|
|
self.setGeometry(100, 100, 800, 600)
|
2024-11-12 19:26:38 +03:00
|
|
|
|
|
2024-11-13 10:22:53 +03:00
|
|
|
|
self.tabs = QTabWidget()
|
|
|
|
|
|
self.setCentralWidget(self.tabs)
|
2024-11-12 19:26:38 +03:00
|
|
|
|
|
2024-11-13 10:22:53 +03:00
|
|
|
|
# self.setWindowIcon(QIcon("path_to_icon.png"))
|
|
|
|
|
|
self.start_directory_watcher(directory_to_watch)
|
|
|
|
|
|
|
|
|
|
|
|
def start_directory_watcher(self, directory_path):
|
|
|
|
|
|
self.watcher_thread = QThread()
|
|
|
|
|
|
self.watcher = DirectoryWatcher(directory_path)
|
|
|
|
|
|
self.watcher.moveToThread(self.watcher_thread)
|
|
|
|
|
|
|
|
|
|
|
|
self.watcher.file_created.connect(self.handle_new_file)
|
|
|
|
|
|
self.watcher_thread.started.connect(self.watcher.on_directory_changed)
|
|
|
|
|
|
|
|
|
|
|
|
self.watcher_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
def handle_new_file(self, file_path):
|
|
|
|
|
|
file_name = os.path.basename(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
app_instance = graphWindow(path=file_path)
|
|
|
|
|
|
|
|
|
|
|
|
tab = QWidget()
|
|
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
|
|
app_widget = app_instance.get_widget()
|
|
|
|
|
|
layout.addWidget(app_widget)
|
|
|
|
|
|
|
|
|
|
|
|
label = QLabel(f"{file_name}")
|
|
|
|
|
|
label.setAlignment(Qt.AlignCenter)
|
|
|
|
|
|
layout.addWidget(label)
|
|
|
|
|
|
tab.setLayout(layout)
|
|
|
|
|
|
|
|
|
|
|
|
self.tabs.addTab(tab, file_name)
|
|
|
|
|
|
|
|
|
|
|
|
def closeEvent(self, event):
|
|
|
|
|
|
self.watcher_thread.quit()
|
|
|
|
|
|
self.watcher_thread.wait()
|
|
|
|
|
|
event.accept()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
directory_to_watch = "D:/downloads/a22"
|
|
|
|
|
|
|
|
|
|
|
|
if not os.path.isdir(directory_to_watch):
|
|
|
|
|
|
print(f"Директория не найдена: {directory_to_watch}")
|
|
|
|
|
|
sys.exit(1)
|
2024-11-12 19:26:38 +03:00
|
|
|
|
|
2024-11-13 10:22:53 +03:00
|
|
|
|
app_instance = QApplication(sys.argv)
|
|
|
|
|
|
window = MainWindow(directory_to_watch)
|
|
|
|
|
|
window.show()
|
|
|
|
|
|
sys.exit(app_instance.exec_())
|
2024-11-08 10:50:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
2024-11-13 10:22:53 +03:00
|
|
|
|
if __name__ == "__main__":
|
2024-11-08 10:50:18 +03:00
|
|
|
|
main()
|