Skip to content

Commit e8a8caa

Browse files
authored
Merge pull request #155 from dylanljones/key-cache
Key cache
2 parents 692d679 + 833e80d commit e8a8caa

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
- **db:** **allow commiting if playlist is not found in XML ([#149](https://github.com/dylanljones/pyrekordbox/issues/149))**
1010
This fixes an issue where the changes to the DB can not be commited if the playlist is missing
1111
in the playlist-xml. A warning is now shown instead.
12+
- **config:** **move pyrekordbox cache file to appdir**
13+
The cache file is now stored in the appdata directory of the user.
14+
This avoids writing to the pyrekordbox package directory.
1215

1316

1417
<a name="v0.4.0"></a>

pyrekordbox/config.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
# Cache file for pyrekordbox data
3232
_cache_file_version = 2
33-
_cache_file = Path(__file__).parent / "rb.cache"
33+
_cache_file_name = "rb.cache"
3434

3535
# Define empty pyrekordbox configuration
3636
__config__ = {
@@ -48,6 +48,25 @@ class InvalidApplicationDirname(Exception):
4848
pass
4949

5050

51+
def get_appdata_dir() -> Path:
52+
"""Returns the path of the application data directory.
53+
54+
On Windows, the application data is stored in `/Users/user/AppData/Roaming`.
55+
On macOS the application data is stored in `~/Libary/Application Support`.
56+
"""
57+
if sys.platform == "win32":
58+
# Windows: located in /Users/user/AppData/Roaming/
59+
app_data = Path(os.environ["AppData"])
60+
elif sys.platform == "darwin":
61+
# MacOS: located in ~/Library/Application Support/
62+
app_data = Path("~").expanduser() / "Library" / "Application Support"
63+
else:
64+
# Linux: not supported
65+
logger.warning(f"OS {sys.platform} not supported!")
66+
return Path("~").expanduser() / ".local" / "share"
67+
return app_data
68+
69+
5170
def get_pioneer_install_dir(path: Union[str, Path] = None) -> Path: # pragma: no cover
5271
"""Returns the path of the Pioneer program installation directory.
5372
@@ -426,12 +445,16 @@ def run(self):
426445

427446

428447
def write_db6_key_cache(key: str) -> None: # pragma: no cover
429-
"""Writes the decrypted Rekordbox6 database key to the cache file.
448+
r"""Writes the decrypted Rekordbox6 database key to the cache file.
430449
431450
This method can also be used to manually cache the database key, provided
432451
the user has found the key somewhere else. The key can be, for example,
433452
found in some other projects that hard-coded it.
434453
454+
The cache file is stored in the application data directory of pyrekordbox:
455+
Windows: `C:\Users\<user>\AppData\Roaming\pyrekordbox`
456+
macOS: `~/Library/Application Support/pyrekordbox`
457+
435458
Parameters
436459
----------
437460
key : str
@@ -453,7 +476,12 @@ def write_db6_key_cache(key: str) -> None: # pragma: no cover
453476
lines.append(f"version: {_cache_file_version}")
454477
lines.append("dp: " + key)
455478
text = "\n".join(lines)
456-
with open(_cache_file, "w") as fh:
479+
480+
cache_file = get_appdata_dir() / "pyrekordbox" / _cache_file_name
481+
if not cache_file.parent.exists():
482+
cache_file.parent.mkdir()
483+
484+
with open(cache_file, "w") as fh:
457485
fh.write(text)
458486
# Set the config key to make sure the key is present after calling method
459487
if __config__["rekordbox6"]:
@@ -465,10 +493,13 @@ def write_db6_key_cache(key: str) -> None: # pragma: no cover
465493
def _update_sqlite_key(opts, conf):
466494
cache_version = 0
467495
pw, dp = "", ""
468-
if _cache_file.exists(): # pragma: no cover
469-
logger.debug("Found cache file %s", _cache_file)
496+
497+
cache_file = get_appdata_dir() / "pyrekordbox" / _cache_file_name
498+
499+
if cache_file.exists(): # pragma: no cover
500+
logger.debug("Found cache file %s", cache_file)
470501
# Read cache file
471-
with open(_cache_file, "r") as fh:
502+
with open(cache_file, "r") as fh:
472503
text = fh.read()
473504
lines = text.splitlines()
474505
if lines[0].startswith("version:"):

0 commit comments

Comments
 (0)