Skip to content

Commit 2087270

Browse files
authored
Merge pull request Monika-After-Story#9725 from Booplicate/better-probability
Improve greeting/fares probabilities code
2 parents e3a90e2 + 16fca1b commit 2087270

10 files changed

Lines changed: 197 additions & 111 deletions

Monika After Story/game/0utils.rpy

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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-

Monika After Story/game/event-handler.rpy

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,7 +2406,7 @@ init python:
24062406
# now this event has passsed checks, we can add it to the db
24072407
eventdb.setdefault(event.eventlabel, event)
24082408

2409-
@store.mas_utils.deprecated("mas_hideEVL", should_raise=True)
2409+
@store.mas_utils.deprecated(use_instead="mas_hideEVL", should_raise=True)
24102410
def hideEventLabel(
24112411
eventlabel,
24122412
lock=False,
@@ -2434,7 +2434,7 @@ init python:
24342434
# (DEfault: evhand.event_database)
24352435
mas_hideEventLabel(eventlabel, lock, derandom, depool, decond, eventdb)
24362436

2437-
@store.mas_utils.deprecated("mas_hideEvent")
2437+
@store.mas_utils.deprecated(use_instead="mas_hideEvent")
24382438
def hideEvent(
24392439
event,
24402440
lock=False,
@@ -2582,7 +2582,7 @@ init python:
25822582
"""
25832583
mas_showEvent(eventdb.get(ev_label, None), unlock, _random, _pool)
25842584

2585-
@store.mas_utils.deprecated("mas_lockEvent", should_raise=True)
2585+
@store.mas_utils.deprecated(use_instead="mas_lockEvent", should_raise=True)
25862586
def lockEvent(ev):
25872587
"""
25882588
NOTE: DEPRECATED
@@ -2593,7 +2593,7 @@ init python:
25932593
"""
25942594
mas_lockEvent(ev)
25952595

2596-
@store.mas_utils.deprecated("mas_lockEventLabel", should_raise=True)
2596+
@store.mas_utils.deprecated(use_instead="mas_lockEventLabel", should_raise=True)
25972597
def lockEventLabel(evlabel, eventdb=evhand.event_database):
25982598
"""
25992599
NOTE: DEPRECATED
@@ -2627,7 +2627,7 @@ init python:
26272627
evhand._lockEventLabel(evlabel, eventdb=eventdb)
26282628

26292629

2630-
@store.mas_utils.deprecated("MASEventList.push")
2630+
@store.mas_utils.deprecated(use_instead="MASEventList.push")
26312631
def pushEvent(event_label, skipeval=False, notify=False):
26322632
"""
26332633
This pushes high priority or time sensitive events onto the top of
@@ -2648,7 +2648,7 @@ init python:
26482648
MASEventList.push(event_label, skipeval, notify)
26492649

26502650

2651-
@store.mas_utils.deprecated("MASEventList.queue")
2651+
@store.mas_utils.deprecated(use_instead="MASEventList.queue")
26522652
def queueEvent(event_label, notify=False):
26532653
"""
26542654
This adds low priority or order-sensitive events onto the bottom of
@@ -2667,7 +2667,7 @@ init python:
26672667
MASEventList.queue(event_label, notify)
26682668

26692669

2670-
@store.mas_utils.deprecated("mas_unlockEvent", should_raise=True)
2670+
@store.mas_utils.deprecated(use_instead="mas_unlockEvent", should_raise=True)
26712671
def unlockEvent(ev):
26722672
"""
26732673
NOTE: DEPRECATED
@@ -2678,7 +2678,7 @@ init python:
26782678
"""
26792679
mas_unlockEvent(ev)
26802680

2681-
@store.mas_utils.deprecated("mas_unlockEventLabel")
2681+
@store.mas_utils.deprecated(use_instead="mas_unlockEventLabel")
26822682
def unlockEventLabel(evlabel, eventdb=evhand.event_database):
26832683
"""
26842684
NOTE: DEPRECATED
@@ -2761,7 +2761,7 @@ init python:
27612761
return evhand._isPresent(ev)
27622762

27632763

2764-
@store.mas_utils.deprecated("MASEventList.pop", should_raise=True)
2764+
@store.mas_utils.deprecated(use_instead="MASEventList.pop", should_raise=True)
27652765
def popEvent(remove=True):
27662766
"""
27672767
DO NOT USE.

0 commit comments

Comments
 (0)