@@ -337,6 +337,7 @@ init -991 python in mas_submod_utils:
337337init - 980 python in mas_submod_utils:
338338 import inspect
339339 import store
340+ from store import mas_utils
340341
341342 # Store the current label for use elsewhere
342343 current_label = None
@@ -355,12 +356,13 @@ init -980 python in mas_submod_utils:
355356 PRIORITY_SORT_KEY = lambda x : x[1 ][2 ]
356357
357358 # START: Decorator Function
358- def functionplugin (_label , _args = [] , auto_error_handling = True , priority = 0 ):
359+ def functionplugin (_label , _args = None , auto_error_handling = True , priority = 0 ):
359360 """
360361 Decorator function to register a plugin
361362
362363 The same as registerFunction. See its doc for parameter details
363364 """
365+ # TODO: functools.wraps
364366 def wrap (_function ):
365367 registerFunction(
366368 _label,
@@ -392,35 +394,36 @@ init -980 python in mas_submod_utils:
392394 return
393395
394396 # Firstly, let's get our sorted list
397+ # TODO: use insort instead of sorting every time we run things
395398 sorted_plugins = __prioritySort(key )
396399 for _action, data_tuple in sorted_plugins:
397400 if data_tuple[1 ]:
398401 try :
399- store.__run(_action, getArgs (key , _action))
402+ store.__run(_action, __getArgs (key , _action))
400403 except Exception as ex:
401404 store.mas_utils.mas_log.error(" function {0} failed because {1} " .format(_action.__name__ , ex))
402405
403406 else :
404- store.__run(_action, getArgs (key , _action))
407+ store.__run(_action, __getArgs (key , _action))
405408
406- def registerFunction (key , _function , args = [] , auto_error_handling = True , priority = DEF_PRIORITY):
409+ def registerFunction (key , _function , args = None , auto_error_handling = True , priority = DEF_PRIORITY):
407410 """
408411 Registers a function to the function_plugins dict
409412
410413 NOTE: Does NOT allow overwriting of existing functions in the dict
411414 NOTE: Function must be callable
412415 NOTE: Functions run when a label matching the key for the function is:
413- called, jumped, or fallen through to.
414- Or if plugged into a function, when a function by the name of the key calls getAndRunFunctions
416+ called, jumped, or fallen through to.
417+ Or if plugged into a function, when a function by the name of the key calls getAndRunFunctions
418+ NOTE: If you need to provide args/kwargs to the function,
419+ wrap it into functools.partial
415420
416421 IN:
417422 key - key to add the function to.
418423 NOTE: The key is either a label, or a function name
419424 NOTE: Function names only work if the function contains a getAndRunFunctions call.
420425 Without it, it does nothing.
421426 _function - function to register
422- args - list of args (must be in order) to pass to the function
423- (Default: [] )
424427 auto_error_handling - whether or function plugins should ignore errors in functions
425428 (Set this to False for functions which call or jump)
426429 priority - Order priority to run functions
@@ -438,10 +441,20 @@ init -980 python in mas_submod_utils:
438441 store.mas_utils.mas_log.error(" {0} is not callable" .format(_function.__name__ ))
439442 return False
440443
441- # Too many args
442- elif len (args) > len (inspect.getargspec(_function).args):
443- store.mas_utils.mas_log.error(" Too many args provided for function {0} " .format(_function.__name__ ))
444- return False
444+ # TODO: remove args entirely in r8
445+ if args is None :
446+ args = ()
447+
448+ else :
449+ mas_utils.report_deprecation(
450+ " parameter 'args' in 'registerFunction'" ,
451+ use_instead = " functools.partial" ,
452+ use_instead_msg_fmt = " Wrap your callable in '{use_instead} ' to provide it args/kwargs."
453+ )
454+ # Too many args
455+ if len (args) > len (inspect.getargspec(_function).args):
456+ store.mas_utils.mas_log.error(" Too many args provided for function {0} " .format(_function.__name__ ))
457+ return False
445458
446459 # Check for overrides
447460 key = __getOverrideLabel(key )
@@ -457,8 +470,9 @@ init -980 python in mas_submod_utils:
457470 function_plugins[key ][_function] = (args, auto_error_handling, priority)
458471 return True
459472
460- def getArgs (key , _function ):
473+ def __getArgs (key , _function ):
461474 """
475+ TODO: remove this with r8
462476 Gets args for the given function at the given key
463477
464478 IN:
@@ -471,21 +485,44 @@ init -980 python in mas_submod_utils:
471485 """
472486 global function_plugins
473487
474- func_dict = function_plugins.get(key )
488+ try :
489+ return function_plugins[key ][_function][0 ]
475490
476- if not func_dict:
477- return
491+ except KeyError :
492+ # Unknown key/function
493+ # We do not handle index error as that shouldn't be possible
494+ # and means there's a bug in the system
495+ return None
496+
497+ @ mas_utils.deprecated (
498+ use_instead = " functools.partial" ,
499+ use_instead_msg_fmt = " Wrap your callable in '{use_instead} ' to provide it args/kwargs."
500+ )
501+ def getArgs (key , _function ):
502+ """
503+ Gets args for the given function at the given key
504+
505+ IN:
506+ key - key to retrieve the function from
507+ _function - function to retrieve args from
478508
479- return func_dict.get(_function)[0 ]
509+ OUT:
510+ list of args if the function is present
511+ If function is not present, None is returned
512+ """
513+ return __getArgs(key , _function)
480514
481- def setArgs (key , _function , args = []):
515+ @ mas_utils.deprecated (
516+ use_instead = " functools.partial" ,
517+ use_instead_msg_fmt = " Wrap your callable in '{use_instead} ' to provide it args/kwargs."
518+ )
519+ def setArgs (key , _function , args = None ):
482520 """
483521 Sets args for the given function at the key
484522
485523 IN:
486524 key - key that the function's function dict is stored in
487525 _function - function to set the args
488- args - list of args (must be in order) to pass to the function (Default: [] )
489526
490527 OUT:
491528 boolean:
@@ -501,9 +538,12 @@ init -980 python in mas_submod_utils:
501538 return False
502539
503540 # Function not in dict
504- elif _function not in func_dict:
541+ if _function not in func_dict:
505542 return False
506543
544+ if args is None :
545+ args = ()
546+
507547 # Too many args provided
508548 elif len (args) > len (inspect.getargspec(_function).args):
509549 store.mas_utils.mas_log.error(" Too many args provided for function {0} " .format(_function.__name__ ))
0 commit comments