Skip to content

Commit 45fde3f

Browse files
Merge pull request #360 from jonaslb/cache-dir
Use platformdirs for cpuinfo cache
2 parents 007fe79 + 87060df commit 45fde3f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies = [
3737
"numexpr",
3838
"py-cpuinfo",
3939
"httpx",
40+
"platformdirs",
4041
]
4142
version = "3.0.1.dev"
4243

src/blosc2/core.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
import sys
2222
from dataclasses import asdict
2323
from functools import lru_cache
24-
from typing import TYPE_CHECKING
24+
from typing import TYPE_CHECKING, Any
2525

2626
import cpuinfo
2727
import numpy as np
28+
import platformdirs
2829

2930
import blosc2
3031
from blosc2 import blosc2_ext
@@ -36,6 +37,12 @@
3637
import torch
3738

3839

40+
_USER_CACHE_DIR: pathlib.Path = platformdirs.user_cache_path(
41+
appname="python-blosc2",
42+
appauthor="blosc",
43+
)
44+
45+
3946
def _check_typesize(typesize):
4047
if not 1 <= typesize <= blosc2_ext.MAX_TYPESIZE:
4148
raise ValueError(f"typesize can only be in the 1-{blosc2_ext.MAX_TYPESIZE} range.")
@@ -1170,14 +1177,15 @@ def _get_cpu_info():
11701177
return cpu_info
11711178

11721179

1173-
def write_cached_cpu_info(cpu_info_dict: dict[str, any]) -> None:
1174-
with open(pathlib.Path.home() / ".blosc2-cpuinfo.json", "w") as f:
1180+
def write_cached_cpu_info(cpu_info_dict: dict[str, Any]) -> None:
1181+
_USER_CACHE_DIR.mkdir(parents=True, exist_ok=True)
1182+
with (_USER_CACHE_DIR / "cpuinfo.json").open("w") as f:
11751183
json.dump(cpu_info_dict, f, indent=4)
11761184

11771185

1178-
def read_cached_cpu_info() -> dict:
1186+
def read_cached_cpu_info() -> dict[str, Any]:
11791187
try:
1180-
with open(pathlib.Path.home() / ".blosc2-cpuinfo.json") as f:
1188+
with (_USER_CACHE_DIR / "cpuinfo.json").open() as f:
11811189
return json.load(f)
11821190
except (FileNotFoundError, json.JSONDecodeError):
11831191
return {}

0 commit comments

Comments
 (0)