forked from clan/clan-core
matrix-bot: Add argparse
add mautrix
This commit is contained in:
parent
881196188c
commit
ef9b733631
12
pkgs/matrix-bot/bin/bot
Executable file
12
pkgs/matrix-bot/bin/bot
Executable file
@ -0,0 +1,12 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
sys.path.insert(
|
||||||
|
0, os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
|
||||||
|
)
|
||||||
|
|
||||||
|
from matrix_bot import main # NOQA
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -1,10 +1,10 @@
|
|||||||
{ python3, setuptools, ... }:
|
{ python3, setuptools, mautrix, ... }:
|
||||||
|
|
||||||
|
|
||||||
let
|
let
|
||||||
|
|
||||||
pythonDependencies = [
|
pythonDependencies = [
|
||||||
|
mautrix
|
||||||
];
|
];
|
||||||
|
|
||||||
runtimeDependencies = [ ];
|
runtimeDependencies = [ ];
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
devShells.matrix-bot = pkgs.callPackage ./shell.nix { inherit (self'.packages) matrix-bot; };
|
devShells.matrix-bot = pkgs.callPackage ./shell.nix { inherit (self'.packages) matrix-bot; };
|
||||||
packages = {
|
packages = {
|
||||||
matrix-bot = pkgs.python3.pkgs.callPackage ./default.nix { };
|
matrix-bot = pkgs.python3.pkgs.callPackage ./default.nix { };
|
||||||
default = self'.packages.matrix-bot;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
checks = { };
|
checks = { };
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
from .main import main
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -0,0 +1,4 @@
|
|||||||
|
from .main import main
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
97
pkgs/matrix-bot/matrix_bot/custom_logger.py
Normal file
97
pkgs/matrix-bot/matrix_bot/custom_logger.py
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
import inspect
|
||||||
|
import logging
|
||||||
|
from collections.abc import Callable
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
grey = "\x1b[38;20m"
|
||||||
|
yellow = "\x1b[33;20m"
|
||||||
|
red = "\x1b[31;20m"
|
||||||
|
bold_red = "\x1b[31;1m"
|
||||||
|
green = "\u001b[32m"
|
||||||
|
blue = "\u001b[34m"
|
||||||
|
|
||||||
|
|
||||||
|
def get_formatter(color: str) -> Callable[[logging.LogRecord, bool], logging.Formatter]:
|
||||||
|
def myformatter(
|
||||||
|
record: logging.LogRecord, with_location: bool
|
||||||
|
) -> logging.Formatter:
|
||||||
|
reset = "\x1b[0m"
|
||||||
|
|
||||||
|
try:
|
||||||
|
filepath = Path(record.pathname).resolve()
|
||||||
|
filepath = Path("~", filepath.relative_to(Path.home()))
|
||||||
|
except Exception:
|
||||||
|
filepath = Path(record.pathname)
|
||||||
|
|
||||||
|
if not with_location:
|
||||||
|
return logging.Formatter(f"{color}%(levelname)s{reset}: %(message)s")
|
||||||
|
|
||||||
|
return logging.Formatter(
|
||||||
|
f"{color}%(levelname)s{reset}: %(message)s\nLocation: {filepath}:%(lineno)d::%(funcName)s\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
return myformatter
|
||||||
|
|
||||||
|
|
||||||
|
FORMATTER = {
|
||||||
|
logging.DEBUG: get_formatter(blue),
|
||||||
|
logging.INFO: get_formatter(green),
|
||||||
|
logging.WARNING: get_formatter(yellow),
|
||||||
|
logging.ERROR: get_formatter(red),
|
||||||
|
logging.CRITICAL: get_formatter(bold_red),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class CustomFormatter(logging.Formatter):
|
||||||
|
def __init__(self, log_locations: bool) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.log_locations = log_locations
|
||||||
|
|
||||||
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
|
return FORMATTER[record.levelno](record, self.log_locations).format(record)
|
||||||
|
|
||||||
|
|
||||||
|
class ThreadFormatter(logging.Formatter):
|
||||||
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
|
return FORMATTER[record.levelno](record, False).format(record)
|
||||||
|
|
||||||
|
|
||||||
|
def get_caller() -> str:
|
||||||
|
frame = inspect.currentframe()
|
||||||
|
if frame is None:
|
||||||
|
return "unknown"
|
||||||
|
caller_frame = frame.f_back
|
||||||
|
if caller_frame is None:
|
||||||
|
return "unknown"
|
||||||
|
caller_frame = caller_frame.f_back
|
||||||
|
if caller_frame is None:
|
||||||
|
return "unknown"
|
||||||
|
frame_info = inspect.getframeinfo(caller_frame)
|
||||||
|
|
||||||
|
try:
|
||||||
|
filepath = Path(frame_info.filename).resolve()
|
||||||
|
filepath = Path("~", filepath.relative_to(Path.home()))
|
||||||
|
except Exception:
|
||||||
|
filepath = Path(frame_info.filename)
|
||||||
|
|
||||||
|
ret = f"{filepath}:{frame_info.lineno}::{frame_info.function}"
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logging(level: Any, root_log_name: str = __name__.split(".")[0]) -> None:
|
||||||
|
# Get the root logger and set its level
|
||||||
|
main_logger = logging.getLogger(root_log_name)
|
||||||
|
main_logger.setLevel(level)
|
||||||
|
|
||||||
|
# Create and add the default handler
|
||||||
|
default_handler = logging.StreamHandler()
|
||||||
|
|
||||||
|
# Create and add your custom handler
|
||||||
|
default_handler.setLevel(level)
|
||||||
|
default_handler.setFormatter(CustomFormatter(str(level) == str(logging.DEBUG)))
|
||||||
|
main_logger.addHandler(default_handler)
|
||||||
|
|
||||||
|
# Set logging level for other modules used by this module
|
||||||
|
logging.getLogger("asyncio").setLevel(logging.INFO)
|
||||||
|
logging.getLogger("httpx").setLevel(level=logging.WARNING)
|
@ -1,3 +1,36 @@
|
|||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from matrix_bot.custom_logger import setup_logging
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def create_parser(prog: str | None = None) -> argparse.ArgumentParser:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
prog=prog,
|
||||||
|
description="A gitea bot for matrix",
|
||||||
|
formatter_class=argparse.RawTextHelpFormatter,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--debug",
|
||||||
|
help="Enable debug logging",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
return parser
|
||||||
def main():
|
def main():
|
||||||
print("Hello, world!")
|
parser = create_parser()
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
parser.print_help()
|
||||||
|
|
||||||
|
if args.debug:
|
||||||
|
setup_logging(logging.DEBUG, root_log_name=__name__.split(".")[0])
|
||||||
|
log.debug("Debug log activated")
|
||||||
|
else:
|
||||||
|
setup_logging(logging.INFO, root_log_name=__name__.split(".")[0])
|
Loading…
Reference in New Issue
Block a user