wuenlp_tools.utils

 1from __future__ import annotations
 2
 3import os
 4import shutil
 5from pathlib import Path
 6
 7
 8class _UserPath(Path):
 9    """``Path`` whose ``repr`` uses ``~`` instead of the expanded home directory."""
10
11    def __repr__(self) -> str:
12        home = os.path.expanduser("~")
13        text = os.fspath(self)
14        if text == home or text.startswith(home + os.sep):
15            text = "~" + text[len(home):]
16        kind = "WindowsPath" if os.sep == "\\" else "PosixPath"
17        return f"{kind}({text!r})"
18
19
20# Shipped, read-only resources (inside the installed package).
21resource_dir = _UserPath(__file__).resolve().parent.parent / "resources"
22
23# Writable cache for downloads and generated files (like wuenlp extra typesystems).
24user_resource_dir = _UserPath.home() / ".local/share/wuenlp-tools/resources"
25
26example_txt = resource_dir / "example.txt"
27
28
29def resolve_resource_path(*parts: str) -> Path:
30    """Return an existing resource path (user cache first, then shipped package)."""
31    user_path = user_resource_dir.joinpath(*parts)
32    if user_path.is_file():
33        return user_path
34    builtin_path = resource_dir.joinpath(*parts)
35    if builtin_path.is_file():
36        return builtin_path
37    return user_path
38
39
40def ensure_resource_path(*parts: str) -> Path:
41    """Writable path under ~/.local/share/wuenlp-tools/resources for downloads/caches."""
42    path = user_resource_dir.joinpath(*parts)
43    path.parent.mkdir(parents=True, exist_ok=True)
44    return path
45
46
47def seed_resource_from_package(*parts: str) -> Path:
48    """Copy a shipped resource into the user cache if missing; return resolved path."""
49    dest = ensure_resource_path(*parts)
50    if dest.is_file():
51        return dest
52    src = resource_dir.joinpath(*parts)
53    if src.is_file():
54        shutil.copy2(src, dest)
55    return dest
resource_dir = PosixPath('~/PycharmProjects/wuenlp-tools/wuenlp_tools/resources')

Path whose repr uses ~ instead of the expanded home directory.

user_resource_dir = PosixPath('~/.local/share/wuenlp-tools/resources')

Path whose repr uses ~ instead of the expanded home directory.

example_txt = PosixPath('~/PycharmProjects/wuenlp-tools/wuenlp_tools/resources/example.txt')

Path whose repr uses ~ instead of the expanded home directory.

def resolve_resource_path(*parts: str) -> pathlib.Path:
30def resolve_resource_path(*parts: str) -> Path:
31    """Return an existing resource path (user cache first, then shipped package)."""
32    user_path = user_resource_dir.joinpath(*parts)
33    if user_path.is_file():
34        return user_path
35    builtin_path = resource_dir.joinpath(*parts)
36    if builtin_path.is_file():
37        return builtin_path
38    return user_path

Return an existing resource path (user cache first, then shipped package).

def ensure_resource_path(*parts: str) -> pathlib.Path:
41def ensure_resource_path(*parts: str) -> Path:
42    """Writable path under ~/.local/share/wuenlp-tools/resources for downloads/caches."""
43    path = user_resource_dir.joinpath(*parts)
44    path.parent.mkdir(parents=True, exist_ok=True)
45    return path

Writable path under ~/.local/share/wuenlp-tools/resources for downloads/caches.

def seed_resource_from_package(*parts: str) -> pathlib.Path:
48def seed_resource_from_package(*parts: str) -> Path:
49    """Copy a shipped resource into the user cache if missing; return resolved path."""
50    dest = ensure_resource_path(*parts)
51    if dest.is_file():
52        return dest
53    src = resource_dir.joinpath(*parts)
54    if src.is_file():
55        shutil.copy2(src, dest)
56    return dest

Copy a shipped resource into the user cache if missing; return resolved path.