@@ -388,8 +388,79 @@ python early in mas_utils:
388388
389389 mas_log = init_mas_log()
390390
391+ # Keep all warnings
392+ _deprecation_warnings = set ()
393+
394+ def report_deprecation (
395+ deprecated ,
396+ use_instead = " " ,
397+ should_raise = False ,
398+ deprecated_msg_fmt = " {deprecated} is deprecated." ,
399+ use_instead_msg_fmt = " Use '{use_instead} ' instead."
400+ ):
401+ """
402+ A unified function to report deprecation
403+
404+ IN:
405+ deprecated - the object that was deprecated or a str explaining what
406+ was deprecated
407+ Examples:
408+ report_deprecation("parameter 'mode'")
409+ report_deprecation(some_func)
410+ report_deprecation(DeprecatedClassExample)
411+ use_instead - str, the name of the function/class to use instead
412+ or just a message
413+ should_raise - bool, whether we raise an exception or just log the error
414+ deprecated_msg_fmt - str, formatter string used to report the deprecated object
415+ NOTE: MUST accept the 'deprecated' keyword and only it
416+ use_instead_msg_fmt - str, formatter string used to report the object to use instead
417+ of the deprecated one
418+ NOTE: MUST accept the 'use_instead' keyword and only it
419+ Examples:
420+ report_deprecation(
421+ "using 'int's",
422+ use_instead="floats"
423+ deprecated_msg_fmt="{deprecated} is no longer supported",
424+ use_instead_msg_fmt="The function now accepts {use_instead} ."
425+ )
426+
427+ RAISES:
428+ DeprecationWarning - if should_raise is True
429+ """
430+ if isinstance (deprecated, basestring ):
431+ deprecated = deprecated.capitalize()
432+
433+ else :
434+ module = getattr (deprecated, " __module__" , " " )
435+ if module:
436+ module += " ."
437+
438+ name = getattr (deprecated, " __name__" , None )
439+ if not name:
440+ name = str (deprecated)
441+
442+ deprecated = " '{}{} '" .format(module, name)
443+
444+ msg_start = deprecated_msg_fmt.format(deprecated = deprecated)
445+
446+ if use_instead:
447+ msg_end = " " + use_instead_msg_fmt.format(use_instead = use_instead)
448+
449+ else :
450+ msg_end = " "
451+
452+ msg = msg_start + msg_end
391453
392- def deprecated (use_instead = None , should_raise = False ):
454+ _deprecation_warnings.add(msg)
455+
456+ if should_raise:
457+ raise DeprecationWarning (msg)
458+
459+ else :
460+ print (" [WARNING] : " + msg, file= sys.stderr)
461+ mas_log.warning(msg)
462+
463+ def deprecated (**report_kws ):
393464 """
394465 Decorator that marks functions and classes as deprecated
395466
@@ -399,11 +470,16 @@ python early in mas_utils:
399470 will be reported in the main log (mas_log.txt) and stderr
400471 NOTE: if we were allowed to raise, we RAISE a DeprecationWarning intead
401472
402- You can access all the reports via __all_warnings__
473+ You can access all the reports via mas_utils._deprecation_warnings
403474
404475 IN:
405- use_instead - string with the name of the function/class to use instead
406- should_raise - whether we raise an exception or just log the error
476+ use_instead - string, the name of the function/class to use instead
477+ should_raise - bool, whether we raise an exception or just log the error
478+ deprecated_msg_fmt - string, a custom formater for the message (see report_deprecation)
479+ use_instead_msg_fmt - string, a custom formater for the message (see report_deprecation)
480+
481+ RAISES:
482+ DeprecationWarning - if should_raise is True
407483 """
408484 def decorator (callable_ ):
409485 """
@@ -421,43 +497,15 @@ python early in mas_utils:
421497 """
422498 Wrapper around the deprecated function/class
423499 """
424- msg = " '{module}{name} ' is deprecated.{use_instead_text} "
425-
426- if hasattr (callable_, " __module__" ) and callable_.__module__:
427- module = callable_.__module__ + " ."
428- else :
429- module = " "
430-
431- name = callable_.__name__
432-
433- if not use_instead:
434- use_instead_text = " "
435- else :
436- use_instead_text = " Use '{0} ' instead." .format(use_instead)
437-
438- msg = msg.format(
439- module = module,
440- name = name,
441- use_instead_text = use_instead_text
442- )
443-
444- deprecated.__all_warnings__.add(msg)
445-
446- if should_raise:
447- raise DeprecationWarning (msg)
448-
449- else :
450- print (" [WARNING] : " + msg, file= sys.stderr)
451- mas_log.warning(msg)
500+ report_deprecation(callable_, ** report_kws)
452501
453502 return callable_(* args, ** kwargs)
454503
455504 return wrapper
456505
457506 return decorator
458507
459- # Keep all warnings
460- deprecated.__all_warnings__ = set ()
508+ deprecated.__all_warnings__ = _deprecation_warnings
461509
462510 @ deprecated (use_instead = " mas_utils.mas_log.info" )
463511 def writelog (msg ):
0 commit comments