StarRailCopilot/tasks/map/route/loader.py

72 lines
2.3 KiB
Python
Raw Normal View History

2023-09-23 11:19:53 +00:00
import importlib
import os
2023-09-23 22:52:52 +00:00
from module.base.decorator import del_cached_property
from module.exception import ScriptError
2023-09-23 11:19:53 +00:00
from module.logger import logger
from tasks.base.ui import UI
2023-09-23 22:52:52 +00:00
from tasks.map.route.base import RouteBase, RouteData
2023-09-23 11:19:53 +00:00
class RouteLoader(UI):
2023-09-23 22:52:52 +00:00
route_module: str = ''
route_func: str = ''
route_obj: RouteBase
2023-09-23 11:19:53 +00:00
2023-09-23 22:52:52 +00:00
def route_delete(self):
del_cached_property(self, 'route_obj')
self.route_module = ''
self.route_func = ''
def route_run(self, route: RouteData | str):
2023-09-23 11:19:53 +00:00
"""
Args:
2023-09-23 22:52:52 +00:00
route: .py module path such as `route.daily.ForgottenHallStage1:route`
which will load `./route/daily/ForgottenHallStage1.py` and run `Route.route()`
2023-09-23 11:19:53 +00:00
"""
2023-09-23 22:52:52 +00:00
logger.hr('Route run', level=1)
if isinstance(route, RouteData):
route = route.route
logger.attr('Route', route)
try:
module, func = route.split(':')
except ValueError:
logger.critical(f'Route invalid: {route}')
raise ScriptError
path = f'./{module.replace(".", "/")}.py'
# Import route file
2023-09-23 11:19:53 +00:00
try:
2023-09-23 22:52:52 +00:00
module_obj = importlib.import_module(f'{module}')
2023-09-23 11:19:53 +00:00
except ModuleNotFoundError:
2023-09-23 22:52:52 +00:00
logger.critical(f'Route file not found: {module} ({path})')
2023-09-23 11:19:53 +00:00
if not os.path.exists(path):
logger.critical(f'Route file not exists: {path}')
2023-09-23 22:52:52 +00:00
raise ScriptError
2023-09-23 11:19:53 +00:00
2023-09-23 22:52:52 +00:00
# Create route object
# Reuse the previous one
if self.route_module != module:
# config = copy.deepcopy(self.config).merge(module.Config())
config = self.config
device = self.device
try:
self.route_obj = module_obj.Route(config=config, device=device)
except AttributeError as e:
logger.critical(e)
logger.critical(f'Route file {route} ({path}) must define class Route')
raise ScriptError
self.route_module = module
# Get route func
2023-09-23 11:19:53 +00:00
try:
2023-09-23 22:52:52 +00:00
func_obj = self.route_obj.__getattribute__(func)
2023-09-23 11:19:53 +00:00
except AttributeError as e:
logger.critical(e)
2023-09-23 22:52:52 +00:00
logger.critical(f'Route class in {route} ({path}) does not have method {func}')
raise ScriptError
self.route_func = func
# Run
func_obj()