Skip to content

Commit

Permalink
🐛 Use site_data_dir(multipath=True)
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Jul 1, 2024
1 parent c2247eb commit 0d54279
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ repos:
hooks:
- id: update-pyproject.toml
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.9
rev: v0.4.10
hooks:
- id: ruff
- id: ruff-format
- repo: https://github.com/kumaraditya303/mirrors-pyright
rev: v1.1.367
rev: v1.1.368
hooks:
- id: pyright

Expand Down
56 changes: 30 additions & 26 deletions src/lsp_tree_sitter/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
"""

from gzip import decompress
from itertools import chain
from pathlib import Path
from subprocess import check_output # nosec: B404
from typing import Literal
from urllib import request

from bs4 import BeautifulSoup, FeatureNotFound
from markdown_it import MarkdownIt
from markdown_it.token import Token
from platformdirs import site_data_path, user_data_path
from platformdirs import site_data_dir, user_data_dir
from pygls.uris import uri_scheme
from pypandoc import convert_text


def get_data_paths(appname: str) -> list[Path]:
return [
Path(d)
for d in site_data_dir(appname, multipath=True).split(":")
+ [user_data_dir(appname)]
]


def get_man(filename: str) -> str:
r"""Get man.
Expand All @@ -30,20 +38,18 @@ def get_man(filename: str) -> str:
number = int(filename.split(".")[-1])
filename += "*"
text = b""
path = ""
for path in chain(
(user_data_path("man") / f"man{number}").glob(filename),
(site_data_path("man") / f"man{number}").glob(filename),
):
try:
with open(path, "rb") as f:
text = f.read()
break
except Exception: # nosec: B112
continue
file = ""
for path in get_data_paths("man"):
for file in (path / f"man{number}").glob(filename):
try:
with open(file, "rb") as f:
text = f.read()
break
except Exception: # nosec: B112
continue
if text == b"":
raise FileNotFoundError
_, _, ext = str(path).rpartition(".")
_, _, ext = str(file).rpartition(".")
if ext != str(number):
text = decompress(text)
return text.decode()
Expand All @@ -58,20 +64,18 @@ def get_info(filename: str) -> str:
"""
filename += "*"
text = b""
path = ""
for path in chain(
user_data_path("info").glob(filename),
site_data_path("info").glob(filename),
):
try:
with open(path, "rb") as f:
text = f.read()
break
except Exception: # nosec: B112
continue
file = ""
for path in get_data_paths("info"):
for file in path.glob(filename):
try:
with open(file, "rb") as f:
text = f.read()
break
except Exception: # nosec: B112
continue
if text == b"":
raise FileNotFoundError
_, _, ext = str(path).rpartition(".")
_, _, ext = str(file).rpartition(".")
if not ext.startswith("info"):
text = decompress(text)
return text.decode()
Expand Down

0 comments on commit 0d54279

Please sign in to comment.