feat(assets): support PyInstaller sys._MEIPASS frozen asset bundle path
This commit is contained in:
@@ -14,6 +14,7 @@ Assets managed:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import re
|
import re
|
||||||
import json
|
import json
|
||||||
import math
|
import math
|
||||||
@@ -48,9 +49,12 @@ def _download_file(url: str, dest_path: Path) -> bool:
|
|||||||
Downloads a file with custom User-Agent to avoid Cloudflare 403 blocks.
|
Downloads a file with custom User-Agent to avoid Cloudflare 403 blocks.
|
||||||
Returns True on success, False otherwise.
|
Returns True on success, False otherwise.
|
||||||
"""
|
"""
|
||||||
dest_path.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
if dest_path.exists() and dest_path.stat().st_size > 0:
|
if dest_path.exists() and dest_path.stat().st_size > 0:
|
||||||
return True
|
return True
|
||||||
|
try:
|
||||||
|
dest_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
|
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
|
||||||
@@ -84,7 +88,14 @@ class AssetsManager:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, assets_dir: Optional[Path] = None, cat_width: int = 144, config: Optional[Config] = None):
|
def __init__(self, assets_dir: Optional[Path] = None, cat_width: int = 144, config: Optional[Config] = None):
|
||||||
self.assets_dir = assets_dir or (Path(__file__).parent / "assets")
|
if assets_dir:
|
||||||
|
self.assets_dir = assets_dir
|
||||||
|
elif getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
|
||||||
|
mei_catser = Path(sys._MEIPASS) / "catser" / "assets"
|
||||||
|
self.assets_dir = mei_catser if mei_catser.exists() else (Path(sys._MEIPASS) / "assets")
|
||||||
|
else:
|
||||||
|
self.assets_dir = Path(__file__).parent / "assets"
|
||||||
|
|
||||||
self.config = config or Config(cat_width=cat_width)
|
self.config = config or Config(cat_width=cat_width)
|
||||||
self.cat_width = self.config.cat_width
|
self.cat_width = self.config.cat_width
|
||||||
self.cat_height = self.config.cat_height
|
self.cat_height = self.config.cat_height
|
||||||
@@ -106,7 +117,10 @@ class AssetsManager:
|
|||||||
|
|
||||||
def initialize(self) -> None:
|
def initialize(self) -> None:
|
||||||
"""Downloads all missing assets and prepares raw frames."""
|
"""Downloads all missing assets and prepares raw frames."""
|
||||||
|
try:
|
||||||
self.assets_dir.mkdir(parents=True, exist_ok=True)
|
self.assets_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
self._ensure_assets_downloaded()
|
self._ensure_assets_downloaded()
|
||||||
self._load_raw_assets()
|
self._load_raw_assets()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user