-
-
Notifications
You must be signed in to change notification settings - Fork 465
Refactor video_player.py (Fix #270)
#274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f41e7d0
Refactor video_player.py
CyanVoxel cd3709a
Add basic ResourceManager, use in video_player.py
CyanVoxel a99476a
Revert tagstudio.spec changes
CyanVoxel cadbceb
Change tuple usage to dicts
CyanVoxel 5037530
Move ResourceManager initialization steps
CyanVoxel 369d212
Fix errant list notation
CyanVoxel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.