From d7dc531c9b29496614e45cc9c3b5a148e0384e80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Ro=C3=9F?= Date: Sun, 4 Aug 2024 15:24:14 +0000 Subject: [PATCH] Add support for keyworded arguments in the Decorator API --- pyframebuffer/__init__.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/pyframebuffer/__init__.py b/pyframebuffer/__init__.py index f58eb59..6726069 100644 --- a/pyframebuffer/__init__.py +++ b/pyframebuffer/__init__.py @@ -1,11 +1,12 @@ """ Core Python sources of the pyframebuffer module. """ -import functools from pyframebuffer.color import getColorValue - import _pyfb as fb # type: ignore +import functools +import inspect + __all__ = ["openfb", "MAX_FRAMEBUFFERS", "fbuser"] MAX_FRAMEBUFFERS = fb.MAX_FRAMEBUFFERS @@ -218,13 +219,29 @@ def fbuser(fn): """ @functools.wraps(fn) def wrapper(*args, **kwargs): - argl = list(args) - fbnum = argl[0] + # first check the function signature + signature = inspect.signature(fn) + first_param = next(iter(signature.parameters), None) + + # declare the return value variable ret_val = None - with openfb(fbnum) as fb_obj: - argl[0] = fb_obj - args = tuple(argl) - ret_val = fn(*args, **kwargs) + + if first_param not in kwargs: + # got here because target argument is not used keyworded + # so manipulate the args tuple + argl = list(args) + fbnum = argl[0] + with openfb(fbnum) as fb_obj: + argl[0] = fb_obj + args = tuple(argl) + ret_val = fn(*args, **kwargs) + else: + # got here because target argument is keyworded + # so modify the dict + fbnum = int(kwargs[first_param]) + with openfb(fbnum) as fb_obj: + kwargs[first_param] = fb_obj + ret_val = fn(*args, **kwargs) return ret_val