Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use platformdirs for cpuinfo cache #360

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading