2020-04-14 04:26:57 +00:00
|
|
|
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]:
|
|
|
|
|
2020-05-25 15:01:00 +00:00
|
|
|
flag = [value is None or self.config.__getattribute__(key) == value
|
|
|
|
for key, value in record['options'].items()]
|
2020-04-14 04:26:57 +00:00
|
|
|
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
|