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-23 18:13:44 +03:00
|
|
|
|
from base.base import BaseDirectoryMonitor, BaseFileManager, Settings
|
2024-11-25 14:01:09 +03:00
|
|
|
|
|
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:
|
2025-02-06 15:59:16 +03:00
|
|
|
|
if self._mode != 3: self._mediator.notify(self, list(self._paths_library))
|
|
|
|
|
|
else: self.open_raw_traces_dir(list(self._paths_library)[0])
|
2025-01-20 17:26:34 +03:00
|
|
|
|
|
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))
|
2025-02-06 15:59:16 +03:00
|
|
|
|
self._mode = 1
|
2025-01-21 16:45:53 +03:00
|
|
|
|
|
|
|
|
|
|
case 2: # Режим онлайн-мониторинга папки
|
|
|
|
|
|
self._monitor.init_state()
|
|
|
|
|
|
self._monitor.start()
|
2025-02-06 15:59:16 +03:00
|
|
|
|
self._mode = 2
|
2025-01-30 16:08:42 +03:00
|
|
|
|
|
|
|
|
|
|
case 3: # Режим работы с трейсами клиента
|
|
|
|
|
|
self._monitor.stop()
|
|
|
|
|
|
self._paths_library.clear()
|
2025-02-06 15:59:16 +03:00
|
|
|
|
self._mode = 3
|
2025-01-21 16:45:53 +03:00
|
|
|
|
|
2025-01-23 18:13:44 +03:00
|
|
|
|
def update_monitor_settings(self, settings:Settings) -> None:
|
|
|
|
|
|
directory_path = settings.system['trace_storage_path'][0]
|
|
|
|
|
|
update_time = settings.system['monitor_update_period'][0]
|
2025-01-20 17:26:34 +03:00
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(directory_path):
|
2025-02-06 14:16:40 +03:00
|
|
|
|
logger.warning(f"Путь {directory_path} не существует.")
|
2025-01-22 18:24:19 +03:00
|
|
|
|
#raise FileNotFoundError(f"Путь {directory_path} не существует.")
|
2025-01-20 17:26:34 +03:00
|
|
|
|
|
|
|
|
|
|
if update_time <= 0.01:
|
2025-02-06 14:16:40 +03:00
|
|
|
|
logger.warning(f"Интервал между проверками папки слишком мал: {update_time}")
|
2025-01-20 17:26:34 +03:00
|
|
|
|
|
|
|
|
|
|
if self._monitor.isActive: self._monitor.pause()
|
2025-01-22 18:24:19 +03:00
|
|
|
|
self._monitor._directory_path = directory_path
|
|
|
|
|
|
self._monitor._update_time = update_time
|
2025-01-20 17:26:34 +03:00
|
|
|
|
if self._monitor.isActive: self._monitor.start()
|
2024-12-18 19:43:46 +03:00
|
|
|
|
|
2025-01-30 16:08:42 +03:00
|
|
|
|
def add_new_paths(self, paths:list) -> None:
|
2025-01-20 17:26:34 +03:00
|
|
|
|
paths_set = set(paths)
|
|
|
|
|
|
new = self._paths_library.difference(paths_set)
|
|
|
|
|
|
self._paths_library.update(new)
|
|
|
|
|
|
self._mediator.notify(list(new))
|
|
|
|
|
|
|
2025-01-30 16:08:42 +03:00
|
|
|
|
def open_raw_traces_dir(self, path:str) -> None:
|
2025-02-06 15:59:16 +03:00
|
|
|
|
self._paths_library.clear()
|
|
|
|
|
|
self._paths_library.add(path)
|
2025-01-30 16:08:42 +03:00
|
|
|
|
dat_file, txt_file = None, None
|
|
|
|
|
|
for entry in os.listdir(path):
|
|
|
|
|
|
full_path = os.path.join(path, entry)
|
|
|
|
|
|
|
|
|
|
|
|
if os.path.isfile(full_path):
|
|
|
|
|
|
_, ext = os.path.splitext(entry)
|
|
|
|
|
|
ext = ext.lower()
|
|
|
|
|
|
if ext == '.dat' and dat_file is None:
|
|
|
|
|
|
dat_file = full_path
|
|
|
|
|
|
elif ext == '.txt' and txt_file is None:
|
|
|
|
|
|
txt_file = full_path
|
|
|
|
|
|
if dat_file and txt_file:
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if dat_file and txt_file:
|
2025-02-06 15:59:16 +03:00
|
|
|
|
self._mediator.prerender_TCW(self, [dat_file, txt_file])
|
2025-01-30 16:08:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-25 17:20:00 +03:00
|
|
|
|
|
2025-01-23 18:13:44 +03:00
|
|
|
|
class DirectoryMonitor(BaseDirectoryMonitor):
|
|
|
|
|
|
|
|
|
|
|
|
def init_state(self):
|
|
|
|
|
|
if not os.path.exists(self._directory_path):
|
|
|
|
|
|
logger.error(f"Путь {self._directory_path} не существует.")
|
|
|
|
|
|
raise FileNotFoundError(f"Путь {self._directory_path} не существует.")
|
|
|
|
|
|
|
|
|
|
|
|
self._files = [
|
|
|
|
|
|
os.path.join(self._directory_path, file)
|
|
|
|
|
|
for file in os.listdir(self._directory_path)
|
|
|
|
|
|
if file.lower().endswith('.csv')
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
self.update_timer.timeout.connect(self._monitor)
|
|
|
|
|
|
logger.info("Monitor initiated!")
|
|
|
|
|
|
|
|
|
|
|
|
def _monitor(self):
|
|
|
|
|
|
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)))
|
|
|
|
|
|
if new_files:
|
|
|
|
|
|
logger.info(f"New files detected: {new_files}")
|
|
|
|
|
|
self._file_manager.add_new_paths(new_files)
|
|
|
|
|
|
self._files = current_files
|
|
|
|
|
|
if not current_files:
|
|
|
|
|
|
self._files = []
|