use msgspec to parse message
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s

This commit is contained in:
Thomas Peetz
2026-07-31 02:40:12 +02:00
parent fdb9da358a
commit 1fa64bf5ce
2 changed files with 53 additions and 26 deletions
+43
View File
@@ -0,0 +1,43 @@
import logging.config
def get_logger(level: int, name: str) -> logging.Logger:
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S',
},
},
'handlers': {
'console': {
'class': logging.StreamHandler,
'level': logging.DEBUG,
'formatter': 'simple',
'stream': 'ext://sys.stdout'
},
},
'loggers': {
'urllib3.connectionpool': {
'level': 'WARNING',
'propagate': False,
},
'root': {
'level': 'DEBUG',
'handlers': ['console'],
},
},
})
logger = logging.getLogger(name)
if level is not None:
match level:
case 0:
logger.setLevel(logging.WARNING)
case 1:
logger.setLevel(logging.INFO)
case 2:
logger.setLevel(logging.DEBUG)
case _:
logger.setLevel(logging.CRITICAL)
return logger
+10 -26
View File
@@ -8,42 +8,23 @@ import msgspec
from pathlib import Path from pathlib import Path
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from platformdirs import PlatformDirs from platformdirs import PlatformDirs
from log import get_logger
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("--config", "-c", default="kontor-docker")
parser.add_argument('--verbose', '-v', action='count', default=0) parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument("--server", "-s", default="127.0.0.1") parser.add_argument("--server", "-s", default="127.0.0.1")
parser.add_argument("--port", "-p", default="61616")
args = parser.parse_args() args = parser.parse_args()
logger = logging.getLogger(__name__)
def get_logger(level: int, config: str):
"""
create Logger with configuration from config file
"""
dirs = PlatformDirs(config)
logging_config = Path(dirs.user_config_dir, "logging-config.yaml")
with open(logging_config, "rt", encoding="UTF-8") as f:
config_dict = yaml.safe_load(f.read())
logging.config.dictConfig(config_dict)
log = logging.getLogger("development")
if level is not None:
match level:
case 0:
log.setLevel(logging.INFO)
case 1:
log.setLevel(logging.DEBUG)
case _:
log.setLevel(logging.CRITICAL)
return log
class Link(msgspec.Struct): class Link(msgspec.Struct):
url: str url: str
class MyListener(stomp.ConnectionListener): class MyListener(stomp.ConnectionListener):
def __init__(self, log): def __init__(self, log, conn):
self.log = log self.log = log
self.conn = conn
pass pass
def on_error(self, frame): def on_error(self, frame):
@@ -53,14 +34,17 @@ class MyListener(stomp.ConnectionListener):
self.log.info("received a message %s", frame.body) self.log.info("received a message %s", frame.body)
link = msgspec.json.decode(frame.body, type=Link) link = msgspec.json.decode(frame.body, type=Link)
self.log.info("found link: %s", link.url) self.log.info("found link: %s", link.url)
json_bytes = msgspec.json.encode(link)
self.conn.send(body=json_bytes, destination="add_link_accepted")
self.conn.send(body=json_bytes, destination="update_title")
if __name__ == '__main__': if __name__ == '__main__':
logger = get_logger(args.verbose, args.config) logger = get_logger(args.verbose, __file__)
logger.info("kontor.read_queue started") logger.info("kontor.read_queue started")
host = [(args.server, 61616)] host = [(args.server, args.port)]
conn = stomp.Connection(host_and_ports=host) conn = stomp.Connection(host_and_ports=host)
conn.set_listener('', MyListener(logger)) conn.set_listener('', MyListener(logger, conn))
conn.connect(username='artemis', passcode='artemis', wait=True) conn.connect(username='artemis', passcode='artemis', wait=True)
conn.subscribe(destination='add_link', id=1, ack='auto', headers={}) conn.subscribe(destination='add_link', id=1, ack='auto', headers={})
time.sleep(5) time.sleep(5)