Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate bad function plugin arg #9736

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 52 additions & 16 deletions Monika After Story/game/zz_submods.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ init -991 python in mas_submod_utils:
init -980 python in mas_submod_utils:
import inspect
import store
from store import mas_utils

#Store the current label for use elsewhere
current_label = None
Expand All @@ -355,7 +356,7 @@ init -980 python in mas_submod_utils:
PRIORITY_SORT_KEY = lambda x: x[1][2]

#START: Decorator Function
def functionplugin(_label, _args=[], auto_error_handling=True, priority=0):
def functionplugin(_label, _args=None, auto_error_handling=True, priority=0):
"""
Decorator function to register a plugin

Expand Down Expand Up @@ -396,31 +397,31 @@ init -980 python in mas_submod_utils:
for _action, data_tuple in sorted_plugins:
if data_tuple[1]:
try:
store.__run(_action, getArgs(key, _action))
store.__run(_action, __getArgs(key, _action))
except Exception as ex:
store.mas_utils.mas_log.error("function {0} failed because {1}".format(_action.__name__, ex))

else:
store.__run(_action, getArgs(key, _action))
store.__run(_action, __getArgs(key, _action))

def registerFunction(key, _function, args=[], auto_error_handling=True, priority=DEF_PRIORITY):
def registerFunction(key, _function, args=None, auto_error_handling=True, priority=DEF_PRIORITY):
"""
Registers a function to the function_plugins dict

NOTE: Does NOT allow overwriting of existing functions in the dict
NOTE: Function must be callable
NOTE: Functions run when a label matching the key for the function is:
called, jumped, or fallen through to.
Or if plugged into a function, when a function by the name of the key calls getAndRunFunctions
called, jumped, or fallen through to.
Or if plugged into a function, when a function by the name of the key calls getAndRunFunctions
NOTE: If you need to provide args/kwargs to the function,
wrap it into functools.partial

IN:
key - key to add the function to.
NOTE: The key is either a label, or a function name
NOTE: Function names only work if the function contains a getAndRunFunctions call.
Without it, it does nothing.
_function - function to register
args - list of args (must be in order) to pass to the function
(Default: [])
auto_error_handling - whether or function plugins should ignore errors in functions
(Set this to False for functions which call or jump)
priority - Order priority to run functions
Expand All @@ -438,10 +439,20 @@ init -980 python in mas_submod_utils:
store.mas_utils.mas_log.error("{0} is not callable".format(_function.__name__))
return False

#Too many args
elif len(args) > len(inspect.getargspec(_function).args):
store.mas_utils.mas_log.error("Too many args provided for function {0}".format(_function.__name__))
return False
# TODO: remove args entirely in r8
if args is None:
args = ()

else:
mas_utils.report_deprecation(
"parameter 'args' in 'registerFunction'",
use_instead="functools.partial",
use_instead_msg_fmt="Wrap your callable in '{use_instead}' to provide it args/kwargs."
)
#Too many args
if len(args) > len(inspect.getargspec(_function).args):
store.mas_utils.mas_log.error("Too many args provided for function {0}".format(_function.__name__))
return False

#Check for overrides
key = __getOverrideLabel(key)
Expand All @@ -457,8 +468,9 @@ init -980 python in mas_submod_utils:
function_plugins[key][_function] = (args, auto_error_handling, priority)
return True

def getArgs(key, _function):
def __getArgs(key, _function):
"""
TODO: remove this with r8
Gets args for the given function at the given key

IN:
Expand All @@ -478,14 +490,35 @@ init -980 python in mas_submod_utils:

return func_dict.get(_function)[0]

def setArgs(key, _function, args=[]):
@mas_utils.deprecated(
use_instead="functools.partial",
use_instead_msg_fmt="Wrap your callable in '{use_instead}' to provide it args/kwargs."
)
def getArgs(key, _function):
"""
Gets args for the given function at the given key

IN:
key - key to retrieve the function from
_function - function to retrieve args from

OUT:
list of args if the function is present
If function is not present, None is returned
"""
return __getArgs(key, _function)

@mas_utils.deprecated(
use_instead="functools.partial",
use_instead_msg_fmt="Wrap your callable in '{use_instead}' to provide it args/kwargs."
)
def setArgs(key, _function, args=None):
multimokia marked this conversation as resolved.
Show resolved Hide resolved
"""
Sets args for the given function at the key

IN:
key - key that the function's function dict is stored in
_function - function to set the args
args - list of args (must be in order) to pass to the function (Default: [])

OUT:
boolean:
Expand All @@ -501,9 +534,12 @@ init -980 python in mas_submod_utils:
return False

#Function not in dict
elif _function not in func_dict:
if _function not in func_dict:
return False

if args is None:
args = ()

#Too many args provided
elif len(args) > len(inspect.getargspec(_function).args):
store.mas_utils.mas_log.error("Too many args provided for function {0}".format(_function.__name__))
Expand Down