Skip to content

Commit

Permalink
Use platformdirs for cpuinfo cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslb committed Feb 1, 2025
1 parent 007fe79 commit 87060df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies = [
"numexpr",
"py-cpuinfo",
"httpx",
"platformdirs",
]
version = "3.0.1.dev"

Expand Down
18 changes: 13 additions & 5 deletions src/blosc2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import sys
from dataclasses import asdict
from functools import lru_cache
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import cpuinfo
import numpy as np
import platformdirs

import blosc2
from blosc2 import blosc2_ext
Expand All @@ -36,6 +37,12 @@
import torch


_USER_CACHE_DIR: pathlib.Path = platformdirs.user_cache_path(
appname="python-blosc2",
appauthor="blosc",
)


def _check_typesize(typesize):
if not 1 <= typesize <= blosc2_ext.MAX_TYPESIZE:
raise ValueError(f"typesize can only be in the 1-{blosc2_ext.MAX_TYPESIZE} range.")
Expand Down Expand Up @@ -1170,14 +1177,15 @@ def _get_cpu_info():
return cpu_info


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


def read_cached_cpu_info() -> dict:
def read_cached_cpu_info() -> dict[str, Any]:
try:
with open(pathlib.Path.home() / ".blosc2-cpuinfo.json") as f:
with (_USER_CACHE_DIR / "cpuinfo.json").open() as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return {}
Expand Down

0 comments on commit 87060df

Please sign in to comment.