This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
forked from FichteFoll/discordrp-mpris
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f7bb6c
commit 907949b
Showing
5 changed files
with
167 additions
and
10 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ name = "pypi" | |
[packages] | ||
dbussy = "*" | ||
pytoml = "*" | ||
requests = "*" | ||
requests_file = "*" | ||
|
||
[dev-packages] | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import json | ||
import logging | ||
import os | ||
from pathlib import Path | ||
from typing import Any, Dict, Optional, Tuple | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Cache: | ||
def __init__(self, raw_cache: Dict[str, Any], cache_file: str) -> None: | ||
self.raw_cache = raw_cache | ||
self.cache_file = cache_file | ||
|
||
def get(self, key: str, default: Any = None) -> Any: | ||
if key in self.raw_cache: | ||
return self.raw_cache.get(key) | ||
else: | ||
return default | ||
|
||
def set(self, key: str, value: Any) -> None: | ||
self.raw_cache[key] = value | ||
try: | ||
with open(self.cache_file, "w", encoding = "UTF-8") as file: | ||
json.dump(self.raw_cache, file, separators = (",", ":")) | ||
except: | ||
logger.exception("Failed to write to the application's cache file.") | ||
|
||
@classmethod | ||
def load(cls) -> Optional['Cache']: | ||
# TODO create an empty JSON file if not exist | ||
user_cache, user_cache_file = cls._load_user_cache() | ||
if user_cache != None: | ||
return Cache(user_cache, user_cache_file) | ||
|
||
return None | ||
|
||
@staticmethod | ||
def _load_user_cache() -> Tuple[Optional[Dict[str, Any]], Optional[str]]: | ||
user_patterns = ("$XDG_CONFIG_HOME", "$HOME/.config") | ||
user_file = None | ||
|
||
for pattern in user_patterns: | ||
parent = Path(os.path.expandvars(pattern)) | ||
if parent.is_dir(): | ||
user_file = parent / "discordrp-mpris" / "cache.json" | ||
if user_file.is_file(): | ||
logging.debug(f"Loading user cache: {user_file!s}") | ||
with user_file.open() as f: | ||
return json.load(f), user_file | ||
|
||
return None, None |
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