Files
2025-06-27 02:53:43 +08:00

71 lines
2.1 KiB
Python

"""
Copyright (C) 2025 283375
This file is part of "arcaea-apk-assets" (stated as "this program" below).
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
"""
import zipfile
from typing import Any, ClassVar, Optional
from .assets import (
ArcaeaApkAsset,
ArcaeaApkAssetJacket,
ArcaeaApkAssetList,
ArcaeaApkAssetPartnerIcon,
)
__all__ = ["ArcaeaApkAssetsProcessor"]
class ArcaeaApkAssetsProcessor:
ASSET_TYPES: ClassVar[list[type[ArcaeaApkAsset]]] = [
ArcaeaApkAssetJacket,
ArcaeaApkAssetPartnerIcon,
ArcaeaApkAssetList,
]
@staticmethod
def _check_zipfile(value: Any):
if not isinstance(value, zipfile.ZipFile):
msg = f"{value!r} is not a ZipFile!"
raise TypeError(msg)
@classmethod
def get_asset(cls, asset_zip_filename: str) -> Optional[ArcaeaApkAsset]:
asset = None
for asset_type in cls.ASSET_TYPES:
if asset_ := asset_type.from_zip_filename(asset_zip_filename):
asset = asset_
break
return asset
@classmethod
def is_asset(cls, asset_zip_filename: str) -> bool:
return cls.get_asset(asset_zip_filename) is not None
@classmethod
def get_assets(cls, zf: zipfile.ZipFile) -> dict[zipfile.ZipInfo, ArcaeaApkAsset]:
cls._check_zipfile(zf)
matches = {}
for info in zf.infolist():
filename = info.filename
asset = cls.get_asset(filename)
if asset is not None:
matches[info] = asset
return matches