-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
executable file
·69 lines (55 loc) · 2.32 KB
/
cache.py
File metadata and controls
executable file
·69 lines (55 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
"""
Cross-Platform Cache Directory Utilities
Provides platform-specific cache directory paths with optional platformdirs integration.
Works on all platforms with or without platformdirs installed.
Usage:
from cache import user_cache_path
cache_dir = user_cache_path('g3xtools', 'g3xtools')
Dependencies:
- platformdirs (optional): Preferred for cache directory paths
- Falls back to built-in implementation if not available
"""
import pathlib
import sys
from typing import cast
# Public API
__all__ = [
'user_cache_path',
]
def user_cache_path(appname: str, appauthor: str = '', ensure_exists: bool = True) -> pathlib.Path:
"""
Get platform-specific user cache directory path.
Attempts to use platformdirs library for proper platform-specific paths.
Falls back to manual implementation if platformdirs is not installed.
Creates the directory if it doesn't exist (unless ensure_exists=False).
Args:
appname: Application name for cache directory
appauthor: Application author (used on Windows)
ensure_exists: If True, create directory if it doesn't exist (default: True)
Returns:
Path to cache directory as pathlib.Path
Examples:
>>> cache_dir = user_cache_path('g3xtools', 'g3xtools')
>>> # macOS: ~/Library/Caches/g3xtools
>>> # Linux: ~/.cache/g3xtools
>>> # Windows: ~\\AppData\\Local\\g3xtools\\g3xtools\\Cache
"""
try:
from platformdirs import user_cache_path as _platformdirs_cache
return cast(pathlib.Path, _platformdirs_cache(appname, appauthor, ensure_exists=ensure_exists))
except ImportError:
# Fallback implementation for manual platform detection
if sys.platform == 'darwin':
cache_path = pathlib.Path.home() / 'Library' / 'Caches' / appname
elif sys.platform == 'win32':
if appauthor:
cache_path = pathlib.Path.home() / 'AppData' / 'Local' / appauthor / appname / 'Cache'
else:
cache_path = pathlib.Path.home() / 'AppData' / 'Local' / appname / 'Cache'
else:
# Linux and other Unix-like systems
cache_path = pathlib.Path.home() / '.cache' / appname
if ensure_exists:
cache_path.mkdir(parents=True, exist_ok=True)
return cache_path