2023-07-06 09:45:50 +08:00
|
|
|
import os
|
|
|
|
import urllib.parse
|
|
|
|
|
2023-10-10 01:54:45 +08:00
|
|
|
DEFAULT_CACHE_DIR = os.getenv("CACHE_ROOT", os.path.expanduser("~/.commacache"))
|
2023-07-06 09:45:50 +08:00
|
|
|
|
2023-10-10 01:54:45 +08:00
|
|
|
def cache_path_for_file_path(fn, cache_dir=DEFAULT_CACHE_DIR):
|
|
|
|
dir_ = os.path.join(cache_dir, "local")
|
2023-12-24 12:18:48 +08:00
|
|
|
os.makedirs(dir_, exist_ok=True)
|
2023-07-06 09:45:50 +08:00
|
|
|
fn_parsed = urllib.parse.urlparse(fn)
|
|
|
|
if fn_parsed.scheme == '':
|
|
|
|
cache_fn = os.path.abspath(fn).replace("/", "_")
|
|
|
|
else:
|
|
|
|
cache_fn = f'{fn_parsed.hostname}_{fn_parsed.path.replace("/", "_")}'
|
|
|
|
return os.path.join(dir_, cache_fn)
|