Skip to content

Commit

Permalink
Add support for keyworded arguments in the Decorator API
Browse files Browse the repository at this point in the history
  • Loading branch information
RossAdrian committed Aug 4, 2024
1 parent 61d7666 commit d7dc531
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions pyframebuffer/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d7dc531

Please sign in to comment.