Skip to content

Commit

Permalink
Switch to platformdirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatsh committed Oct 30, 2024
1 parent 20d2536 commit 2bfcfc0
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 73 deletions.
1 change: 0 additions & 1 deletion .rtfd-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 0 additions & 10 deletions .stubs/xdg/BaseDirectory.pyi

This file was deleted.

1 change: 0 additions & 1 deletion .stubs/xdg/__init__.pyi

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ paren
parseable
pipx
plabel
platformdirs
popen
postprocessors
prss
Expand All @@ -103,7 +104,6 @@ pylint
pyright
pytest
pythonwarnings
pyxdg
ratelimit
ratelimit
rcfile
Expand Down
18 changes: 9 additions & 9 deletions open_in_mpv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -16,27 +16,27 @@
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'

SYSTEM_HOSTS_DIRS: Final[tuple[str, str, str]] = ('/etc/chromium/native-messaging-hosts',
'/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).',
Expand Down
42 changes: 5 additions & 37 deletions open_in_mpv/open_in_mpv.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]:
Expand Down
15 changes: 2 additions & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2bfcfc0

Please sign in to comment.