From 168d7cc1f87509c3dac02522e1d9eeec5b28e9ae Mon Sep 17 00:00:00 2001 From: erjemin Date: Tue, 19 Dec 2023 18:35:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=BE=D0=B2=D0=B8=D1=82=20=D1=81=D0=BE?= =?UTF-8?q?=D0=B1=D1=8B=D1=82=D0=B8=D1=8F=20"=D0=BC=D0=BE=D0=B4=D0=B8?= =?UTF-8?q?=D1=84=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D1=8F=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=B0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 81525c8..e62f760 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- +import platform import time import sys import signal import toml +import os from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from pathlib import Path @@ -33,8 +35,28 @@ def read_toml(file_path): sys.exit(1) +class LogFileHandler(FileSystemEventHandler): + def __init__(self, log_filename): + self.log_filename = log_filename + + def on_modified(self, event): + if event.is_directory: + return + print(event) + # elif event.event_type == 'modified' and event.src_path == self.log_filename: + # # Обработка изменений в лог-файле + # with open(event.src_path, 'r') as file: + # new_lines = file.readlines() + # # Обработка новых строк + # for line in new_lines: + # print(f"Изменение в {self.log_filename}:: {line}", end='') + + # Press the green button in the gutter to run the script. if __name__ == '__main__': + # Получение информации о системе + # system_info = platform.system() + # print(f"System: {system_info}") # Установка обработчика сигнала signal.signal(signal.SIGTERM, handler_stop) # Считывание имени config-файла их аргументов командной строки @@ -49,8 +71,36 @@ if __name__ == '__main__': interval = config['interval'] if 'interval' in config else 1 # получаем все группы в конфиге group_keys = [key for key, value in config.items() if isinstance(value, dict)] + count_valid_groups = 0 + observers = [] for group in group_keys: - print(group) + if 'log_in' not in config[group]: + print(f"Config '{config_file}' not have key 'log_in' in group '{group}' (see config '{config_file}').") + continue + if not os.path.isfile(config[group]['log_in']): + print(f"File '{config[group]['log_in']}' not exist (see config '{config_file}').") + continue + if 'outdir' not in config[group]: + print(f"Config '{config_file}' not have key 'outdir' in group '{group}' (see config '{config_file}').") + continue + if not os.path.isdir(config[group]['outdir']): + print(f"Directory '{config[group]['outdir']}' not exist (see config '{config_file}').") + continue + observers.append(Observer()) + observers[count_valid_groups].schedule(LogFileHandler(config[group]['log_in']), + path=os.path.dirname(config[group]['log_in']), + recursive=False) + observers[count_valid_groups].start() + count_valid_groups += 1 + + # observer = Observer() + # event_handler = LogFileHandler(config[group]['log_in']) + # observer.schedule(event_handler, path=os.path.dirname(config[group]['log_in']), recursive=False) + + if count_valid_groups == 0: + # если нет ни одной валидной группы, то выходим + print(f"Config '{config_file}' not have valid groups (see config '{config_file}').") + sys.exit(1) # все обработчики в этом цикле try: @@ -59,6 +109,8 @@ if __name__ == '__main__': print_hi('PyCharm') time.sleep(interval) except KeyboardInterrupt: + for observer in observers: + observer.stop() print('Terminate by `KeyboardInterrupt`.') sys.exit(0) except ValueError: