diff --git a/.rtfd-requirements.txt b/.rtfd-requirements.txt index a92c2e3..ad02bd0 100644 --- a/.rtfd-requirements.txt +++ b/.rtfd-requirements.txt @@ -26,7 +26,6 @@ pydantic==2.9.2 ; python_version >= "3.12" and python_version < "4" pygls==1.3.1 ; python_version >= "3.12" and python_version < "4" pygments==2.18.0 ; python_version >= "3.12" and python_version < "4" pyspellchecker==0.8.1 ; python_version >= "3.12" and python_version < "4" -pyxdg==0.28 ; python_version >= "3.12" and python_version < "4" requests==2.32.3 ; python_version >= "3.12" and python_version < "4" restructuredtext-lint==1.4.0 ; python_version >= "3.12" and python_version < "4" snowballstemmer==2.2.0 ; python_version >= "3.12" and python_version < "4" diff --git a/.stubs/xdg/BaseDirectory.pyi b/.stubs/xdg/BaseDirectory.pyi deleted file mode 100644 index 1ae1061..0000000 --- a/.stubs/xdg/BaseDirectory.pyi +++ /dev/null @@ -1,10 +0,0 @@ -# SPDX-License-Identifier: MIT -from typing import Final - -def save_state_path(*resource: str) -> str: - ... - -def get_runtime_dir(*, strict: bool = ...) -> str: - ... - -xdg_config_home: Final[str] = ... diff --git a/.stubs/xdg/__init__.pyi b/.stubs/xdg/__init__.pyi deleted file mode 100644 index 548d2d4..0000000 --- a/.stubs/xdg/__init__.pyi +++ /dev/null @@ -1 +0,0 @@ -# SPDX-License-Identifier: MIT diff --git a/.vscode/dictionary.txt b/.vscode/dictionary.txt index 281b99d..d438420 100644 --- a/.vscode/dictionary.txt +++ b/.vscode/dictionary.txt @@ -91,6 +91,7 @@ paren parseable pipx plabel +platformdirs popen postprocessors prss @@ -103,7 +104,6 @@ pylint pyright pytest pythonwarnings -pyxdg ratelimit ratelimit rcfile diff --git a/open_in_mpv/constants.py b/open_in_mpv/constants.py index 1257152..39d5a68 100644 --- a/open_in_mpv/constants.py +++ b/open_in_mpv/constants.py @@ -3,7 +3,7 @@ import os import platform -import xdg.BaseDirectory +from platformdirs import user_config_dir __all__ = ('HOST_DATA', 'HOST_DATA_FIREFOX', 'IS_LINUX', 'IS_MAC', 'IS_WIN', 'JSON_FILENAME', 'MAC_HOSTS_DIRS', 'SYSTEM_HOSTS_DIRS', 'USER_CHROME_HOSTS_REG_PATH_WIN', @@ -16,14 +16,14 @@ JSON_FILENAME: Final[str] = 'sh.tat.open_in_mpv.json' HOME: Final[str] = os.environ.get('HOME', '') -USER_CHROME_HOSTS_REG_PATH_WIN: Final[str] = 'HKCU:\\Software\\Google\\Chrome\\NativeMessagingHosts' +USER_CHROME_HOSTS_REG_PATH_WIN: Final[str] = r'HKCU:\Software\Google\Chrome\NativeMessagingHosts' MAC_HOSTS_DIRS: Final[tuple[str, ...]] = ( f'{HOME}/Library/Application Support/Chromium/NativeMessagingHosts', f'{HOME}/Library/Application Support/Google/Chrome Beta/NativeMessagingHosts', f'{HOME}/Library/Application Support/Google/Chrome Canary/NativeMessagingHosts', f'{HOME}/Library/Application Support/Google/Chrome/NativeMessagingHosts', - f'{HOME}/Library/Application Support/Mozilla/NativeMessagingHosts/') + f'{HOME}/Library/Application Support/Mozilla/NativeMessagingHosts') MACPORTS_BIN_PATH: Final[str] = '/opt/local/bin' @@ -31,12 +31,12 @@ '/etc/opt/chrome/native-messaging-hosts', '/etc/opt/edge/native-messaging-hosts') USER_HOSTS_DIRS: Final[tuple[str, ...]] = ( - f'{xdg.BaseDirectory.xdg_config_home}/BraveSoftware/Brave-Browser/NativeMessagingHosts', - f'{xdg.BaseDirectory.xdg_config_home}/chromium/NativeMessagingHosts', - f'{xdg.BaseDirectory.xdg_config_home}/google-chrome-beta/NativeMessagingHosts', - f'{xdg.BaseDirectory.xdg_config_home}/google-chrome-canary/NativeMessagingHosts', - f'{xdg.BaseDirectory.xdg_config_home}/google-chrome/NativeMessagingHosts', - f'{xdg.BaseDirectory.xdg_config_home}/.mozilla/native-messaging-hosts/') + f'{user_config_dir()}/BraveSoftware/Brave-Browser/NativeMessagingHosts', + f'{user_config_dir()}/chromium/NativeMessagingHosts', + f'{user_config_dir()}/google-chrome-beta/NativeMessagingHosts', + f'{user_config_dir()}/google-chrome-canary/NativeMessagingHosts', + f'{user_config_dir()}/google-chrome/NativeMessagingHosts', + f'{user_config_dir()}/.mozilla/native-messaging-hosts') COMMON_HOST_DATA: Final[dict[str, str | None]] = { 'description': 'Opens a URL in mpv (for use with extension).', diff --git a/open_in_mpv/open_in_mpv.py b/open_in_mpv/open_in_mpv.py index 86d4673..16b0571 100644 --- a/open_in_mpv/open_in_mpv.py +++ b/open_in_mpv/open_in_mpv.py @@ -1,7 +1,5 @@ # SPDX-License-Identifier: MIT from collections.abc import Callable, Mapping -from functools import lru_cache -from os.path import expandvars from pathlib import Path from typing import Any, BinaryIO, Final, TextIO, cast, override import json @@ -11,47 +9,17 @@ import struct import subprocess as sp import sys -import tempfile +from platformdirs import user_log_path, user_runtime_path import click -import xdg.BaseDirectory -from .constants import IS_MAC, IS_WIN, MACPORTS_BIN_PATH +from .constants import MACPORTS_BIN_PATH FALLBACKS: Final[dict[str, Any]] = {'log': None, 'socket': None} -logger = logging.getLogger(__name__) - - -@lru_cache -def get_log_path() -> Path: - if IS_MAC: - return Path('~/Library/Logs').expanduser() - if IS_WIN: - return Path(expandvars(r'%LOCALDATA%\open-in-mpv')) - try: - return Path(xdg.BaseDirectory.save_state_path('open-in-mpv')) - except KeyError: - FALLBACKS['log'] = tempfile.TemporaryDirectory(prefix='open-in-mpv') - return Path(FALLBACKS['log'].name) - - -@lru_cache -def get_socket_path() -> Path: - if IS_MAC: - return Path('~/Library/Caches/open-in-mpv.sock').expanduser() - if IS_WIN: - return Path(expandvars(r'\\.\pipe\open-in-mpv')) - try: - return Path(xdg.BaseDirectory.get_runtime_dir()) / 'open-in-mpv.sock' - except KeyError: - with tempfile.NamedTemporaryFile(prefix='open-in-mpv', suffix='.sock', delete=False) as tf: - FALLBACKS['socket'] = tf - return Path(FALLBACKS['socket'].name) - - -LOG_PATH = get_log_path() -MPV_SOCKET = get_socket_path() +LOG_PATH = user_log_path('open-in-mpv') +MPV_SOCKET = user_runtime_path('open-in-mpv') / 'open-in-mpv.sock' VERSION = 'v0.1.7' +logger = logging.getLogger(__name__) def environment(data_resp: dict[str, Any], *, debugging: bool) -> dict[str, Any]: diff --git a/poetry.lock b/poetry.lock index 45795bb..13597ce 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.4 and should not be changed by hand. [[package]] name = "alabaster" @@ -948,17 +948,6 @@ pytest = ">=6.2.5" [package.extras] dev = ["pre-commit", "pytest-asyncio", "tox"] -[[package]] -name = "pyxdg" -version = "0.28" -description = "PyXDG contains implementations of freedesktop.org standards in python." -optional = false -python-versions = "*" -files = [ - {file = "pyxdg-0.28-py2.py3-none-any.whl", hash = "sha256:bdaf595999a0178ecea4052b7f4195569c1ff4d344567bccdc12dfdf02d545ab"}, - {file = "pyxdg-0.28.tar.gz", hash = "sha256:3267bb3074e934df202af2ee0868575484108581e6f3cb006af1da35395e88b4"}, -] - [[package]] name = "requests" version = "2.32.3" @@ -1337,4 +1326,4 @@ type = ["pytest-mypy"] [metadata] lock-version = "2.0" python-versions = ">=3.12,<4" -content-hash = "96ea39bbe43ce897da7d5241daf01d5c2c77a36397b106eaa81b156262bc398d" +content-hash = "64214bc46784754a8ad9bc8fce3289e7d46a0e0f91ca963d36ef5c2fc27d73a3" diff --git a/pyproject.toml b/pyproject.toml index 8375efe..81c154a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,8 +21,8 @@ version = "0.1.0" [tool.poetry.dependencies] python = ">=3.12,<4" click = ">=8.0,<8.1.8" +platformdirs = "^4.3.6" psutil = ">=5.9.5,<7.0.0" -pyxdg = "^0.28" [tool.poetry.group.dev] optional = true