This commit is contained in:
Ari Archer 2021-11-29 16:33:18 +02:00
parent df85f57ba7
commit 410621da7d
Signed by untrusted user who does not match committer: ari
GPG key ID: A50D5B4B599AF8A2
2 changed files with 36 additions and 5 deletions

View file

@ -25,6 +25,8 @@ from arigram.utils import (
is_yes,
notify,
suspend,
rename_function,
copy_func,
)
from arigram.views import View
@ -946,18 +948,23 @@ class Controller:
handler = binding_info["handler"]
@bind(handler, [binding])
def handle(*args) -> None: # type: ignore
@rename_function(function_name)
def handle(*args, fn: Callable = function) -> None: # type: ignore
try:
function(self, *args)
fn(self, *args)
except Exception as e:
self.present_error(
f"{function_name}(): {e.__class__.__name__}: {e}"
)
log.info(
f"{function_name} = {function.__name__}() --> {binding}"
)
setattr(
Controller,
self,
function_name,
handle,
copy_func(handle),
)

View file

@ -12,12 +12,14 @@ import struct
import subprocess
import sys
import unicodedata
import types
import functools
from datetime import datetime
from functools import lru_cache
from logging.handlers import RotatingFileHandler
from subprocess import CompletedProcess
from types import TracebackType
from typing import Any, Dict, Optional, Tuple, Type
from typing import Any, Dict, Optional, Tuple, Type, Callable
from arigram import config
@ -37,6 +39,28 @@ class LogWriter:
pass
def copy_func(f):
"""Based on https://stackoverflow.com/a/6528148/190597 (Glenn Maynard)"""
g = types.FunctionType(
f.__code__,
f.__globals__,
name=f.__name__,
argdefs=f.__defaults__,
closure=f.__closure__,
)
g = functools.update_wrapper(g, f)
g.__kwdefaults__ = f.__kwdefaults__
return g
def rename_function(new_name: str) -> Callable:
def decorator(fun: Callable):
fun.__name__ = new_name
return fun
return decorator
def setup_log() -> None:
os.makedirs(config.LOG_PATH, exist_ok=True)