diff --git a/kontor-scripts/api.py b/kontor-scripts/api.py index 87dc3c3..a49b861 100644 --- a/kontor-scripts/api.py +++ b/kontor-scripts/api.py @@ -224,38 +224,6 @@ class ApiConfig: return found_server -def get_logger(level, config: str): - """ - get Logger according to given log level by verbosity. - """ - dirs = PlatformDirs(config) - logging_config = Path(dirs.user_config_dir, "logging-config.yaml") - log_config = None - with open(logging_config, "rt", encoding="utf-8") as f: - log_config = yaml.safe_load(f.read()) - logging.config.dictConfig(log_config) - logger = logging.getLogger("development") - if level is not None: - match level: - case 0: - logger.setLevel(logging.CRITICAL) - logging.getLogger("requests").setLevel(logging.WARNING) - logging.getLogger("urllib3").setLevel(logging.WARNING) - case 1: - logging.getLogger("requests").setLevel(logging.INFO) - logging.getLogger("urllib3").setLevel(logging.INFO) - logger.setLevel(logging.INFO) - case 2: - logger.setLevel(logging.DEBUG) - logging.getLogger("requests").setLevel(logging.DEBUG) - logging.getLogger("urllib3").setLevel(logging.DEBUG) - case _: - logger.setLevel(logging.INFO) - logging.getLogger("requests").setLevel(logging.INFO) - logging.getLogger("urllib3").setLevel(logging.INFO) - return logger - - def get_api_config(log: Logger, config: str) -> ApiConfig: """ Load configuration from file. diff --git a/kontor-scripts/check_kontor.py b/kontor-scripts/check_kontor.py index 8d04371..9cf4d07 100644 --- a/kontor-scripts/check_kontor.py +++ b/kontor-scripts/check_kontor.py @@ -10,8 +10,8 @@ from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter from urllib.parse import urlparse from simple_term_menu import TerminalMenu -from api import Server, get_api_config, get_logger - +from api import Server, get_api_config +from log import get_logger parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument("--verbose", "-v", action="count", default=0) diff --git a/kontor-scripts/log.py b/kontor-scripts/log.py index d634937..74510c1 100644 --- a/kontor-scripts/log.py +++ b/kontor-scripts/log.py @@ -33,11 +33,19 @@ def get_logger(level: int, name: str) -> logging.Logger: if level is not None: match level: case 0: - logger.setLevel(logging.WARNING) + logger.setLevel(logging.CRITICAL) + logging.getLogger("requests").setLevel(logging.WARNING) + logging.getLogger("urllib3").setLevel(logging.WARNING) case 1: + logging.getLogger("requests").setLevel(logging.INFO) + logging.getLogger("urllib3").setLevel(logging.INFO) logger.setLevel(logging.INFO) case 2: logger.setLevel(logging.DEBUG) + logging.getLogger("requests").setLevel(logging.DEBUG) + logging.getLogger("urllib3").setLevel(logging.DEBUG) case _: - logger.setLevel(logging.CRITICAL) + logger.setLevel(logging.INFO) + logging.getLogger("requests").setLevel(logging.INFO) + logging.getLogger("urllib3").setLevel(logging.INFO) return logger diff --git a/kontor-scripts/read_queue.py b/kontor-scripts/read_queue.py index 2e57bd9..3f40a9c 100644 --- a/kontor-scripts/read_queue.py +++ b/kontor-scripts/read_queue.py @@ -21,11 +21,10 @@ args = parser.parse_args() class Link(msgspec.Struct): url: str -class MyListener(stomp.ConnectionListener): +class AddLinkListener(stomp.ConnectionListener): def __init__(self, log, conn): self.log = log self.conn = conn - pass def on_error(self, frame): self.log.info("received an error %s", frame.body) @@ -38,15 +37,43 @@ class MyListener(stomp.ConnectionListener): self.conn.send(body=json_bytes, destination="add_link_accepted") self.conn.send(body=json_bytes, destination="update_title") +class UpdateTitleListener(stomp.ConnectionListener): + def __init__(self, log, conn): + self.log = log + self.conn = conn + + def on_error(self, frame): + self.log.info("received an error %s", frame.body) + + def on_message(self, frame): + self.log.info("received a message %s", frame.body) + link = msgspec.json.decode(frame.body, type=Link) + self.log.info("found link: %s", link.url) + json_bytes = msgspec.json.encode(link) + self.conn.send(body=json_bytes, destination="update_title_accepted") + if __name__ == '__main__': logger = get_logger(args.verbose, __file__) logger.info("kontor.read_queue started") + APICONFIG = get_api_config(logger, args.config) + first_server: Optional[Server] = APICONFIG.get_server(args.server) + if not first_server: + sys.exit(2) + data = server.request(log=log, table="media_file") host = [(args.server, args.port)] - conn = stomp.Connection(host_and_ports=host) - conn.set_listener('', MyListener(logger, conn)) - conn.connect(username='artemis', passcode='artemis', wait=True) - conn.subscribe(destination='add_link', id=1, ack='auto', headers={}) + conn_add = stomp.Connection(host_and_ports=host) + conn_add.set_listener('', AddLinkListener(logger, conn_add)) + conn_add.start() + conn_add.connect(username='artemis', passcode='artemis', wait=True) + conn_add.subscribe(destination='add_link', id=1, ack='auto', headers={}) + + conn_update = stomp.Connection(host_and_ports=host) + conn_update.set_listener('', UpdateTitleListener(logger, conn_update)) + conn_update.start() + conn_update.connect(username='artemis', passcode='artemis', wait=True) + conn_update.subscribe(destination='update_title', id=1, ack='auto', headers={}) time.sleep(5) - conn.disconnect() + conn_add.disconnect() + conn_update.disconnect() logger.info("kontor.read_queue finished")