StarRailCopilot/module/base/decorator.py
2020-05-25 23:01:00 +08:00

70 lines
1.9 KiB
Python

from functools import wraps
import numpy as np
from module.logger import logger
class Config:
"""
Decorator that calls different function with a same name according to config.
func_list likes:
func_list = {
'func1': [
{'options': {'ENABLE': True}, 'func': 1},
{'options': {'ENABLE': False}, 'func': 1}
]
}
"""
func_list = {}
@classmethod
def when(cls, **kwargs):
"""
Args:
**kwargs: Any option in AzurLaneConfig.
Examples:
@Config.when(USE_ONE_CLICK_RETIREMENT=True)
def retire_ships(self, amount=None, rarity=None):
pass
@Config.when(USE_ONE_CLICK_RETIREMENT=False)
def retire_ships(self, amount=None, rarity=None):
pass
"""
options = kwargs
def decorate(func):
name = func.__name__
data = {'options': options, 'func': func}
if name not in cls.func_list:
cls.func_list[name] = [data]
else:
cls.func_list[name].append(data)
@wraps(func)
def wrapper(self, *args, **kwargs):
"""
Args:
self: ModuleBase instance.
*args:
**kwargs:
"""
for record in cls.func_list[name]:
flag = [value is None or self.config.__getattribute__(key) == value
for key, value in record['options'].items()]
if not np.all(flag):
continue
return record['func'](self, *args, **kwargs)
logger.warning(f'No option fits for {name}, using the last define func.')
return func(self, *args, **kwargs)
return wrapper
return decorate