2024-11-25 14:01:09 +03:00
|
|
|
import os
|
2025-01-20 14:38:41 +03:00
|
|
|
|
2024-11-25 14:01:09 +03:00
|
|
|
from loguru import logger
|
|
|
|
|
|
2025-01-20 17:26:34 +03:00
|
|
|
from base.base import BaseDirectoryMonitor, BaseFileManager
|
2024-11-25 14:01:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DirectoryMonitor(BaseDirectoryMonitor):
|
|
|
|
|
|
2025-01-20 17:26:34 +03:00
|
|
|
def init_state(self):
|
|
|
|
|
if not os.path.exists(self._directory_path):
|
|
|
|
|
logger.error(f"Путь {self._directory_path} не существует.")
|
|
|
|
|
raise FileNotFoundError(f"Путь {self._directory_path} не существует.")
|
|
|
|
|
|
2024-12-23 14:18:54 +03:00
|
|
|
self._files = [
|
|
|
|
|
os.path.join(self._directory_path, file)
|
|
|
|
|
for file in os.listdir(self._directory_path)
|
|
|
|
|
if file.lower().endswith('.csv')
|
|
|
|
|
]
|
2024-11-25 14:01:09 +03:00
|
|
|
|
|
|
|
|
self.update_timer.timeout.connect(self._monitor)
|
|
|
|
|
logger.info("Monitor initiated!")
|
|
|
|
|
|
|
|
|
|
def _monitor(self):
|
2024-12-23 14:18:54 +03:00
|
|
|
current_files = [
|
|
|
|
|
os.path.join(self._directory_path, file)
|
|
|
|
|
for file in os.listdir(self._directory_path)
|
|
|
|
|
if file.lower().endswith('.csv')
|
|
|
|
|
]
|
|
|
|
|
new_files = sorted(list(filter(lambda x: x not in self._files, current_files)))
|
2024-11-25 14:01:09 +03:00
|
|
|
if new_files:
|
|
|
|
|
logger.info(f"New files detected: {new_files}")
|
2025-01-20 17:26:34 +03:00
|
|
|
self._file_manager.add_new_paths(new_files)
|
2024-12-23 14:18:54 +03:00
|
|
|
self._files = current_files
|
|
|
|
|
if not current_files:
|
2024-11-25 14:01:09 +03:00
|
|
|
self._files = []
|
|
|
|
|
|
2025-01-20 17:26:34 +03:00
|
|
|
|
|
|
|
|
class FileManager(BaseFileManager):
|
|
|
|
|
|
2025-01-21 16:45:53 +03:00
|
|
|
def replot_all(self) -> None:
|
2025-01-20 17:26:34 +03:00
|
|
|
if self._paths_library is not None:
|
|
|
|
|
self._mediator.notify(self, list(self._paths_library))
|
|
|
|
|
|
2025-01-21 16:45:53 +03:00
|
|
|
def open_custom_file(self, path:str) -> None:
|
2025-01-20 17:26:34 +03:00
|
|
|
self._paths_library.add(path)
|
|
|
|
|
self._mediator.notify(self, [path if path else ''])
|
|
|
|
|
|
2025-01-21 16:45:53 +03:00
|
|
|
def set_mode(self, num:int) -> None:
|
|
|
|
|
match num:
|
|
|
|
|
case 1: # Режим создания отчета
|
|
|
|
|
self._monitor.stop()
|
|
|
|
|
self._paths_library.clear()
|
|
|
|
|
self._paths_library.add('')
|
|
|
|
|
self._mediator.notify(self, list(self._paths_library))
|
|
|
|
|
|
|
|
|
|
case 2: # Режим онлайн-мониторинга папки
|
|
|
|
|
self._monitor.init_state()
|
|
|
|
|
self._monitor.start()
|
|
|
|
|
|
2025-01-20 17:26:34 +03:00
|
|
|
def update_monitor_settings(self, settings:list[dict]) -> None:
|
|
|
|
|
_, system_params = settings
|
|
|
|
|
directory_path = system_params['trace_storage_path'][0]
|
|
|
|
|
update_time = system_params['monitor_update_period'][0]
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(directory_path):
|
|
|
|
|
logger.error(f"Путь {directory_path} не существует.")
|
|
|
|
|
raise FileNotFoundError(f"Путь {directory_path} не существует.")
|
|
|
|
|
|
|
|
|
|
if update_time <= 0.01:
|
|
|
|
|
logger.error(f"Путь {directory_path} не существует.")
|
|
|
|
|
|
|
|
|
|
if self._monitor.isActive: self._monitor.pause()
|
|
|
|
|
self._monitor.directory_path = directory_path
|
|
|
|
|
self._monitor.update_time = update_time
|
|
|
|
|
if self._monitor.isActive: self._monitor.start()
|
2024-12-18 19:43:46 +03:00
|
|
|
|
2025-01-20 17:26:34 +03:00
|
|
|
def add_new_paths(self, paths):
|
|
|
|
|
paths_set = set(paths)
|
|
|
|
|
new = self._paths_library.difference(paths_set)
|
|
|
|
|
self._paths_library.update(new)
|
|
|
|
|
self._mediator.notify(list(new))
|
|
|
|
|
|
2024-11-25 17:20:00 +03:00
|
|
|
|