From 5c79fa8057519876d4d2726fbea2a800496fc740 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 6 Oct 2025 11:40:56 -0400 Subject: [PATCH] Use `platformdirs` for `.config.get_rt_dir()` Thanks to the `tox`-dev community for such a lovely pkg which seems to solves all the current cross-platform user-dir problems B) Also this, - now passes `platformdirs.user_runtime_dir(appname='tractor')` and allows caller to pass an optional `subdir` under `tractor/` if desired. - drops the `.config._rtdir: Path` mod var. - bumps the lock file with the new dep. --- pyproject.toml | 2 +- tractor/_state.py | 33 +++++++++++++++++++++------------ uv.lock | 13 ++++++++++++- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a0491598..ef94915b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,7 @@ dependencies = [ "msgspec>=0.19.0", "cffi>=1.17.1", "bidict>=0.23.1", + "platformdirs>=4.4.0", ] # ------ project ------ @@ -63,7 +64,6 @@ dev = [ "stackscope>=0.2.2,<0.3", # ^ requires this? "typing-extensions>=4.14.1", - "pyperclip>=1.9.0", "prompt-toolkit>=3.0.50", "xonsh>=0.19.2", diff --git a/tractor/_state.py b/tractor/_state.py index 2a47e548..4b214608 100644 --- a/tractor/_state.py +++ b/tractor/_state.py @@ -22,7 +22,6 @@ from __future__ import annotations from contextvars import ( ContextVar, ) -import os from pathlib import Path from typing import ( Any, @@ -30,6 +29,7 @@ from typing import ( TYPE_CHECKING, ) +import platformdirs from trio.lowlevel import current_task if TYPE_CHECKING: @@ -172,23 +172,32 @@ def current_ipc_ctx( return ctx -# std ODE (mutable) app state location -_rtdir: Path = Path(os.environ['XDG_RUNTIME_DIR']) - def get_rt_dir( - subdir: str = 'tractor' + subdir: str|Path|None = None, ) -> Path: ''' - Return the user "runtime dir" where most userspace apps stick - their IPC and cache related system util-files; we take hold - of a `'XDG_RUNTIME_DIR'/tractor/` subdir by default. + Return the user "runtime dir", the file-sys location where most + userspace apps stick their IPC and cache related system + util-files. + + On linux we take use a `'${XDG_RUNTIME_DIR}/tractor/` subdir by + default but equivalents are mapped for each platform using + the lovely `platformdirs`. ''' - rtdir: Path = _rtdir / subdir - if not rtdir.is_dir(): - rtdir.mkdir() - return rtdir + rt_dir: Path = Path( + platformdirs.user_runtime_dir( + appname='tractor', + ), + ) + if subdir: + rt_dir: Path = rt_dir / subdir + + if not rt_dir.is_dir(): + rt_dir.mkdir() + + return rt_dir def current_ipc_protos() -> list[str]: diff --git a/uv.lock b/uv.lock index 3c05dc2f..e4f88fa3 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 2 +revision = 3 requires-python = ">=3.11" [[package]] @@ -236,6 +236,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/9e/c3/059298687310d527a58bb01f3b1965787ee3b40dce76752eda8b44e9a2c5/pexpect-4.9.0-py2.py3-none-any.whl", hash = "sha256:7236d1e080e4936be2dc3e326cec0af72acf9212a7e1d060210e70a47e253523", size = 63772, upload-time = "2023-11-25T06:56:14.81Z" }, ] +[[package]] +name = "platformdirs" +version = "4.4.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/23/e8/21db9c9987b0e728855bd57bff6984f67952bea55d6f75e055c46b5383e8/platformdirs-4.4.0.tar.gz", hash = "sha256:ca753cf4d81dc309bc67b0ea38fd15dc97bc30ce419a7f58d13eb3bf14c4febf", size = 21634, upload-time = "2025-08-26T14:32:04.268Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/40/4b/2028861e724d3bd36227adfa20d3fd24c3fc6d52032f4a93c133be5d17ce/platformdirs-4.4.0-py3-none-any.whl", hash = "sha256:abd01743f24e5287cd7a5db3752faf1a2d65353f38ec26d98e25a6db65958c85", size = 18654, upload-time = "2025-08-26T14:32:02.735Z" }, +] + [[package]] name = "pluggy" version = "1.5.0" @@ -378,6 +387,7 @@ dependencies = [ { name = "colorlog" }, { name = "msgspec" }, { name = "pdbp" }, + { name = "platformdirs" }, { name = "tricycle" }, { name = "trio" }, { name = "wrapt" }, @@ -403,6 +413,7 @@ requires-dist = [ { name = "colorlog", specifier = ">=6.8.2,<7" }, { name = "msgspec", specifier = ">=0.19.0" }, { name = "pdbp", specifier = ">=1.6,<2" }, + { name = "platformdirs", specifier = ">=4.4.0" }, { name = "tricycle", specifier = ">=0.4.1,<0.5" }, { name = "trio", specifier = ">0.27" }, { name = "wrapt", specifier = ">=1.16.0,<2" },