#!/usr/bin/env python3 """ AntiDrift """ import logging import shutil import sys import signal from pathlib import Path import antidrift.client as client from antidrift.config import Config from antidrift.daemon import AntiDriftDaemon from dbus.mainloop.glib import DBusGMainLoop DBusGMainLoop(set_as_default=True) signal.signal(signal.SIGINT, signal.SIG_DFL) def init_logging(log_file: Path, mode: str): class DuplicateFilter(logging.Filter): def filter(self, record) -> bool: current_log = (record.module, record.levelno, record.msg) if current_log != getattr(self, "last_log", None): self.last_log = current_log return True return False format_str = f'%(asctime)s | %(levelname)-8s | {mode} | %(message)s' logging.basicConfig(filename=log_file, format=format_str, datefmt='%H:%M:%S', encoding='utf-8', level=logging.DEBUG) logger = logging.getLogger() logger.addFilter(DuplicateFilter()) def check_for_xdotool(): """ Check if xdotool is in path and exit if not """ result = shutil.which("xdotool") if not result: logging.critical("Please install xdotool") sys.exit(1) def tailf(config): import time from rich import print with open(config.log_file, 'r') as f: f.seek(0, 2) while True: line = f.readline() if not line: time.sleep(0.1) else: print(line.strip()) def main() -> None: """ Main routine that dispatches to client or daemon """ config = Config.load("config.yaml") if len(sys.argv) > 1 and sys.argv[1] == "tailf": tailf(config) return check_for_xdotool() if client.antidrift_daemon_is_running(): init_logging(config.log_file, "[blue]client[/blue]") client.client_mode(config) else: init_logging(config.log_file, "[red]server[/red]") add = AntiDriftDaemon(config) add.run() if __name__ == "__main__": main()