-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Refactor video_player.py - Move icons files to qt/images folder, some being renamed - Reduce icon loading to single initial import - Tweak icon dimensions and animation timings - Remove unnecessary commented code - Remove unused/duplicate imports - Add license info to file * Add basic ResourceManager, use in video_player.py * Revert tagstudio.spec changes * Change tuple usage to dicts * Move ResourceManager initialization steps * Fix errant list notation
- Loading branch information
Showing
8 changed files
with
129 additions
and
65 deletions.
There are no files selected for viewing
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright (C) 2024 Travis Abendshien (CyanVoxel). | ||
# Licensed under the GPL-3.0 License. | ||
# Created for TagStudio: https://github.com/CyanVoxel/TagStudio | ||
|
||
import logging | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
import ujson | ||
|
||
logging.basicConfig(format="%(message)s", level=logging.INFO) | ||
|
||
|
||
class ResourceManager: | ||
"""A resource manager for retrieving resources.""" | ||
|
||
_map: dict = {} | ||
_cache: dict[str, Any] = {} | ||
_initialized: bool = False | ||
|
||
def __init__(self) -> None: | ||
# Load JSON resource map | ||
if not ResourceManager._initialized: | ||
with open( | ||
Path(__file__).parent / "resources.json", mode="r", encoding="utf-8" | ||
) as f: | ||
ResourceManager._map = ujson.load(f) | ||
logging.info( | ||
f"[ResourceManager] {len(ResourceManager._map.items())} resources registered" | ||
) | ||
ResourceManager._initialized = True | ||
|
||
def get(self, id: str) -> Any: | ||
"""Get a resource from the ResourceManager. | ||
This can include resources inside and outside of QResources, and will return | ||
theme-respecting variations of resources if available. | ||
Args: | ||
id (str): The name of the resource. | ||
Returns: | ||
Any: The resource if found, else None. | ||
""" | ||
cached_res = ResourceManager._cache.get(id) | ||
if cached_res: | ||
return cached_res | ||
else: | ||
res: dict = ResourceManager._map.get(id) | ||
if res.get("mode") in ["r", "rb"]: | ||
with open( | ||
(Path(__file__).parents[2] / "resources" / res.get("path")), | ||
res.get("mode"), | ||
) as f: | ||
data = f.read() | ||
if res.get("mode") == "rb": | ||
data = bytes(data) | ||
ResourceManager._cache[id] = data | ||
return data | ||
elif res.get("mode") in ["qt"]: | ||
# TODO: Qt resource loading logic | ||
pass | ||
|
||
def __getattr__(self, __name: str) -> Any: | ||
attr = self.get(__name) | ||
if attr: | ||
return attr | ||
raise AttributeError(f"Attribute {id} not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"play_icon": { | ||
"path": "qt/images/play.svg", | ||
"mode": "rb" | ||
}, | ||
"pause_icon": { | ||
"path": "qt/images/pause.svg", | ||
"mode": "rb" | ||
}, | ||
"volume_icon": { | ||
"path": "qt/images/volume.svg", | ||
"mode": "rb" | ||
}, | ||
"volume_mute_icon": { | ||
"path": "qt/images/volume_mute.svg", | ||
"mode": "rb" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters