Created
July 16, 2023 05:10
-
-
Save Gouvernathor/02d4d93c2ec484470c3df915d276863b to your computer and use it in GitHub Desktop.
Class decorator to register statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import renpy # type: ignore | |
"""renpy | |
python early: | |
""" | |
import inspect | |
__register_params = frozenset(inspect.signature(renpy.register_statement).parameters) - {"name", "parse", "execute"} | |
global register_decorator | |
def register_decorator(cls): | |
"""A class decorator which registers a new Creator-defined statement. | |
The "parse" parameter of renpy.register_statement should be either the | |
class constructor itself or a method named "parse" (likely a classmethod | |
or a staticmethod) returning an instance of the class. The name of the | |
statement will be the class name unless a "name" class attribute is | |
present, which should be a string. Instead of the "execute" parameter, | |
Ren'Py will look for a method named "execute", or in its absence, the | |
class's `__call__` method to call the object like a function. | |
All other parameters to the register_statement function can be set as | |
class attributes named the same way. | |
""" | |
name = getattr(cls, "name", cls.__name__) | |
parse = getattr(cls, "parse", cls) | |
execute = getattr(cls, "execute", None) or cls.__call__ | |
renpy.register_statement( | |
name, | |
parse=parse, | |
execute=execute, | |
**{k:getattr(cls, k) for k in __register_params.intersection(vars(cls))} | |
) | |
return cls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment