From a38d9497128e104183653a41d3bf78062f72f4fc Mon Sep 17 00:00:00 2001 From: andrei Date: Thu, 14 Nov 2024 00:56:31 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D0=BC=D0=BE=D1=82=D1=80=D0=B8=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B4=D0=B8=D1=80=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D0=B8=20=D1=81=20=D0=B3=D1=80=D0=B0=D1=84=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.json | 2 +- requirements.txt | 1 + src/cfg/schema.py | 2 +- src/controller/base.py | 53 ++++++++++++++++++++++++++++++++++++++ src/controller/mediator.py | 13 ++++++++++ src/controller/monitor.py | 24 +++++++++++++++++ src/main.py | 5 ++++ 7 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/controller/base.py create mode 100644 src/controller/mediator.py create mode 100644 src/controller/monitor.py diff --git a/config/config.json b/config/config.json index 05d6529..c55445c 100644 --- a/config/config.json +++ b/config/config.json @@ -1,4 +1,4 @@ { "trace_storage_path": "/home/andrei/Desktop/bla", - "monitor_update_period": 100 + "monitor_update_period": 0.1 } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 308cea6..d0fe4f7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +loguru==0.7.2 numpy==2.1.3 pandas==2.2.3 PyQt5==5.15.11 diff --git a/src/cfg/schema.py b/src/cfg/schema.py index 0584e89..5261f54 100644 --- a/src/cfg/schema.py +++ b/src/cfg/schema.py @@ -3,4 +3,4 @@ from typing import NamedTuple class ConfigSchema(NamedTuple): trace_storage_path: str - monitor_update_period: int + monitor_update_period: int | float diff --git a/src/controller/base.py b/src/controller/base.py new file mode 100644 index 0000000..b036a39 --- /dev/null +++ b/src/controller/base.py @@ -0,0 +1,53 @@ +from __future__ import annotations + +import os +from typing import Optional + +from PyQt5.QtCore import QThread + + +class BaseMediator: + def __init__(self, monitor: BaseDirectoryMonitor): + self._monitor = monitor + self._monitor.mediator = self + + def notify(self, files: list[str]): + ... + + +class BaseDirectoryMonitor(QThread): + def __init__(self, + directory_path: str, + update_time: int, + mediator: Optional[BaseMediator] = None): + super().__init__() + + self._directory_path = directory_path + self._update_time = update_time + self._mediator = mediator + + self._files: list[str] = [] + + @property + def directory_path(self) -> str: + return self._directory_path + + @property + def update_time(self) -> int: + return self._update_time + + @property + def files(self) -> list[str]: + return self._files + + @property + def mediator(self) -> BaseMediator: + return self._mediator + + @mediator.setter + def mediator(self, mediator: BaseMediator) -> None: + self._mediator = mediator + + def _init_state(self): + files = os.listdir(self._directory_path) + self._files = files diff --git a/src/controller/mediator.py b/src/controller/mediator.py new file mode 100644 index 0000000..90061fe --- /dev/null +++ b/src/controller/mediator.py @@ -0,0 +1,13 @@ +from controller.base import BaseMediator + +from loguru import logger + + +class Mediator(BaseMediator): + + def notify(self, files: list[str]): + for file in files: + # TODO: create dataframe + # TODO: create plot widget + # TODO: send plot widget to controller + logger.info(f"File handled: {file}") diff --git a/src/controller/monitor.py b/src/controller/monitor.py new file mode 100644 index 0000000..e1d210b --- /dev/null +++ b/src/controller/monitor.py @@ -0,0 +1,24 @@ +from time import sleep +import os + +from loguru import logger + +from controller.base import BaseDirectoryMonitor + + +class DirectoryMonitor(BaseDirectoryMonitor): + + def run(self): + self._init_state() + logger.info("Monitor initiated") + + while True: + files = os.listdir(self._directory_path) + new_files = sorted(list(filter(lambda x: x not in self._files, files))) + if new_files: + logger.info(f"New files detected: {new_files}") + self._mediator.notify(new_files) + self._files = files + if not files: + self._files = [] + sleep(self._update_time) diff --git a/src/main.py b/src/main.py index 8a66be3..2e946c4 100644 --- a/src/main.py +++ b/src/main.py @@ -5,6 +5,8 @@ from os import path from gui.mainGui import MainWindow from cfg.schema import ConfigSchema +from controller.monitor import DirectoryMonitor +from controller.mediator import Mediator def read_json(filepath: str) -> dict: @@ -19,6 +21,9 @@ def read_json(filepath: str) -> dict: def main(): app = QtWidgets.QApplication(sys.argv) config = ConfigSchema(**read_json("config/config.json")) + monitor = DirectoryMonitor(config.trace_storage_path, config.monitor_update_period) + mediator = Mediator(monitor) + monitor.start() window = MainWindow() window.show()