@hot的处理由loadres移至path中

This commit is contained in:
zhbaor 2024-08-09 10:45:55 +08:00
parent 40a0c230a8
commit 946fd529b4
3 changed files with 59 additions and 9 deletions

View file

@ -0,0 +1,41 @@
import unittest
from arknights_mower.utils import path
from arknights_mower.utils.image import res2path
from arknights_mower.utils.path import get_path
internal_dir = get_path("@internal")
install_dir = get_path("@internal")
class TestLogicExpression(unittest.TestCase):
def test_single(self):
res = res2path("infra_overview")
relative_path = str(res.relative_to(internal_dir))
self.assertEqual(relative_path, "arknights_mower/resources/infra_overview.png")
def test_single_hot(self):
res = res2path("@hot/inudi/banner")
relative_path = str(res.relative_to(install_dir))
self.assertEqual(relative_path, "tmp/hot_update/inudi/banner.png")
def test_single_jpg(self):
res = res2path("@hot/inudi/banner.jpg")
relative_path = str(res.relative_to(install_dir))
self.assertEqual(relative_path, "tmp/hot_update/inudi/banner.jpg")
def test_multi(self):
path.global_space = "/test/path"
res = res2path("infra_overview")
relative_path = str(res.relative_to(internal_dir))
self.assertEqual(relative_path, "arknights_mower/resources/infra_overview.png")
def test_multi_hot(self):
path.global_space = "/test/path"
res = res2path("@hot/inudi/banner")
relative_path = str(res.relative_to(install_dir))
self.assertEqual(relative_path, "tmp/hot_update/inudi/banner.png")
if __name__ == "__main__":
unittest.main()

View file

@ -1,10 +1,10 @@
from functools import lru_cache
from pathlib import Path
from typing import Union
import cv2
import numpy as np
from arknights_mower import __rootdir__
from arknights_mower.utils import typealias as tp
from arknights_mower.utils.log import logger, save_screenshot
from arknights_mower.utils.path import get_path
@ -30,15 +30,16 @@ def img2bytes(img: tp.Image) -> bytes:
)[1]
def loadres(res: tp.Res, gray: bool = False) -> Union[tp.Image, tp.GrayImage]:
if res.startswith("@hot"):
res_name = res.replace("@hot", "@install/tmp/hot_update", 1)
else:
res_name = f"{__rootdir__}/resources/{res}"
def res2path(res: tp.Res) -> Path:
if not res.startswith("@"):
res = f"@internal/arknights_mower/resources/{res}"
if not res.endswith(".jpg"):
res_name += ".png"
filename = get_path(res_name, "")
return loadimg(filename, gray)
res += ".png"
return get_path(res)
def loadres(res: tp.Res, gray: bool = False) -> Union[tp.Image, tp.GrayImage]:
return loadimg(res2path(res), gray)
@lru_cache(maxsize=128)

View file

@ -27,6 +27,7 @@ else:
_app_dir = Path(os.getcwd()).resolve()
_internal_dir = _app_dir
_install_dir = _internal_dir
_hot_update_dir = _install_dir / "tmp" / "hot_update"
def _get_path(base_path, path, space) -> Path:
@ -51,12 +52,17 @@ def get_install_path(path) -> Path:
return _get_path(_install_dir, path, None)
def get_hot_update_path(path) -> Path:
return _get_path(_hot_update_dir, path, None)
def get_path(path: str, space=None) -> Path:
"""
使用 '@xxx/' 来表示一些特别的目录
@app: mower数据文件夹, 例如 get_path('@app/logs/runtime.log')
@internal: mower内部文件夹, 在开发时为 .git 所在目录, 打包时为 @app/_internal
@install: mower 安装文件夹在开发时与 @internal 相同打包时为 mower 的安装目录
@hot: 热更新文件夹@install/tmp/hot_update
指定space来区分配置文件空间如space为None(默认值)则使用global_space
@ -78,6 +84,8 @@ def get_path(path: str, space=None) -> Path:
return get_internal_path(relative_path)
elif special_dir_name == "install":
return get_install_path(relative_path)
elif special_dir_name == "hot":
return get_hot_update_path(relative_path)
else:
raise ValueError(
"{}: {} 不是一个有效的特殊目录别名".format(path, special_dir_name)