@@ -372,7 +372,7 @@ python early in mas_utils:
372372
373373 from store import mas_logging
374374
375-
375+
376376 def init_mas_log ():
377377 """
378378 Initializes the MAS log, or gets it if its already init.
@@ -390,8 +390,79 @@ python early in mas_utils:
390390
391391 mas_log = init_mas_log()
392392
393+ # Keep all warnings
394+ _deprecation_warnings = set ()
393395
394- def deprecated (use_instead = None , should_raise = False ):
396+ def report_deprecation (
397+ deprecated ,
398+ use_instead = " " ,
399+ should_raise = False ,
400+ deprecated_msg_fmt = " {deprecated} is deprecated." ,
401+ use_instead_msg_fmt = " Use '{use_instead} ' instead."
402+ ):
403+ """
404+ A unified function to report deprecation
405+
406+ IN:
407+ deprecated - the object that was deprecated or a str explaining what
408+ was deprecated
409+ Examples:
410+ report_deprecation("parameter 'mode'")
411+ report_deprecation(some_func)
412+ report_deprecation(DeprecatedClassExample)
413+ use_instead - str, the name of the function/class to use instead
414+ or just a message
415+ should_raise - bool, whether we raise an exception or just log the error
416+ deprecated_msg_fmt - str, formatter string used to report the deprecated object
417+ NOTE: MUST accept the 'deprecated' keyword and only it
418+ use_instead_msg_fmt - str, formatter string used to report the object to use instead
419+ of the deprecated one
420+ NOTE: MUST accept the 'use_instead' keyword and only it
421+ Examples:
422+ report_deprecation(
423+ "using 'int's",
424+ use_instead="floats"
425+ deprecated_msg_fmt="{deprecated} is no longer supported",
426+ use_instead_msg_fmt="The function now accepts {use_instead} ."
427+ )
428+
429+ RAISES:
430+ DeprecationWarning - if should_raise is True
431+ """
432+ if isinstance (deprecated, basestring ):
433+ deprecated = deprecated.capitalize()
434+
435+ else :
436+ module = getattr (deprecated, " __module__" , " " )
437+ if module:
438+ module += " ."
439+
440+ name = getattr (deprecated, " __name__" , None )
441+ if not name:
442+ name = str (deprecated)
443+
444+ deprecated = " '{}{} '" .format(module, name)
445+
446+ msg_start = deprecated_msg_fmt.format(deprecated = deprecated)
447+
448+ if use_instead:
449+ msg_end = " " + use_instead_msg_fmt.format(use_instead = use_instead)
450+
451+ else :
452+ msg_end = " "
453+
454+ msg = msg_start + msg_end
455+
456+ _deprecation_warnings.add(msg)
457+
458+ if should_raise:
459+ raise DeprecationWarning (msg)
460+
461+ else :
462+ print (" [WARNING] : " + msg, file= sys.stderr)
463+ mas_log.warning(msg)
464+
465+ def deprecated (**report_kws ):
395466 """
396467 Decorator that marks functions and classes as deprecated
397468
@@ -401,11 +472,16 @@ python early in mas_utils:
401472 will be reported in the main log (mas_log.txt) and stderr
402473 NOTE: if we were allowed to raise, we RAISE a DeprecationWarning intead
403474
404- You can access all the reports via __all_warnings__
475+ You can access all the reports via mas_utils._deprecation_warnings
405476
406477 IN:
407- use_instead - string with the name of the function/class to use instead
408- should_raise - whether we raise an exception or just log the error
478+ use_instead - string, the name of the function/class to use instead
479+ should_raise - bool, whether we raise an exception or just log the error
480+ deprecated_msg_fmt - string, a custom formater for the message (see report_deprecation)
481+ use_instead_msg_fmt - string, a custom formater for the message (see report_deprecation)
482+
483+ RAISES:
484+ DeprecationWarning - if should_raise is True
409485 """
410486 def decorator (callable_ ):
411487 """
@@ -423,43 +499,15 @@ python early in mas_utils:
423499 """
424500 Wrapper around the deprecated function/class
425501 """
426- msg = " '{module}{name} ' is deprecated.{use_instead_text} "
427-
428- if hasattr (callable_, " __module__" ) and callable_.__module__:
429- module = callable_.__module__ + " ."
430- else :
431- module = " "
432-
433- name = callable_.__name__
434-
435- if not use_instead:
436- use_instead_text = " "
437- else :
438- use_instead_text = " Use '{0} ' instead." .format(use_instead)
439-
440- msg = msg.format(
441- module = module,
442- name = name,
443- use_instead_text = use_instead_text
444- )
445-
446- deprecated.__all_warnings__.add(msg)
447-
448- if should_raise:
449- raise DeprecationWarning (msg)
450-
451- else :
452- print (" [WARNING] : " + msg, file= sys.stderr)
453- mas_log.warning(msg)
502+ report_deprecation(callable_, ** report_kws)
454503
455504 return callable_(* args, ** kwargs)
456505
457506 return wrapper
458507
459508 return decorator
460509
461- # Keep all warnings
462- deprecated.__all_warnings__ = set ()
510+ deprecated.__all_warnings__ = _deprecation_warnings
463511
464512 # mac logging
465513 class MASMacLog (renpy.renpy.log.LogFile ):
@@ -682,7 +730,7 @@ python early in mas_utils:
682730 class IsolatedFlexProp (object ):
683731 """
684732 class that supports flexible attributes.
685- all attributes that are set are stored in a
733+ all attributes that are set are stored in a
686734 separate internal structure. Supports a few additional behaviors
687735 because of this.
688736
@@ -936,4 +984,3 @@ python early in mas_utils:
936984 return int (value )
937985 except :
938986 return default
939-
0 commit comments