wuenlp_tools.utils

 1from __future__ import annotations
 2
 3import os
 4import shutil
 5from pathlib import Path
 6
 7# Shipped, read-only resources (inside the installed package).
 8resource_dir = Path(__file__).parent.parent / "resources"
 9
10# Writable cache for downloads and generated files (like wuenlp extra typesystems).
11user_resource_dir = Path(os.path.expanduser("~/.local/share/wuenlp-tools/resources"))
12
13example_txt = resource_dir / "example.txt"
14
15
16def resolve_resource_path(*parts: str) -> Path:
17    """Return an existing resource path (user cache first, then shipped package)."""
18    user_path = user_resource_dir.joinpath(*parts)
19    if user_path.is_file():
20        return user_path
21    builtin_path = resource_dir.joinpath(*parts)
22    if builtin_path.is_file():
23        return builtin_path
24    return user_path
25
26
27def ensure_resource_path(*parts: str) -> Path:
28    """Writable path under ~/.local/share/wuenlp-tools/resources for downloads/caches."""
29    path = user_resource_dir.joinpath(*parts)
30    path.parent.mkdir(parents=True, exist_ok=True)
31    return path
32
33
34def seed_resource_from_package(*parts: str) -> Path:
35    """Copy a shipped resource into the user cache if missing; return resolved path."""
36    dest = ensure_resource_path(*parts)
37    if dest.is_file():
38        return dest
39    src = resource_dir.joinpath(*parts)
40    if src.is_file():
41        shutil.copy2(src, dest)
42    return dest
resource_dir = PosixPath('/Users/zehe/PycharmProjects/wuenlp-tools/wuenlp_tools/resources')

Path subclass for non-Windows systems.

On a POSIX system, instantiating a Path should return this object.
user_resource_dir = PosixPath('/Users/zehe/.local/share/wuenlp-tools/resources')

Path subclass for non-Windows systems.

On a POSIX system, instantiating a Path should return this object.
example_txt = PosixPath('/Users/zehe/PycharmProjects/wuenlp-tools/wuenlp_tools/resources/example.txt')

Path subclass for non-Windows systems.

On a POSIX system, instantiating a Path should return this object.
def resolve_resource_path(*parts: str) -> pathlib.Path:
17def resolve_resource_path(*parts: str) -> Path:
18    """Return an existing resource path (user cache first, then shipped package)."""
19    user_path = user_resource_dir.joinpath(*parts)
20    if user_path.is_file():
21        return user_path
22    builtin_path = resource_dir.joinpath(*parts)
23    if builtin_path.is_file():
24        return builtin_path
25    return user_path

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

def ensure_resource_path(*parts: str) -> pathlib.Path:
28def ensure_resource_path(*parts: str) -> Path:
29    """Writable path under ~/.local/share/wuenlp-tools/resources for downloads/caches."""
30    path = user_resource_dir.joinpath(*parts)
31    path.parent.mkdir(parents=True, exist_ok=True)
32    return path

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

def seed_resource_from_package(*parts: str) -> pathlib.Path:
35def seed_resource_from_package(*parts: str) -> Path:
36    """Copy a shipped resource into the user cache if missing; return resolved path."""
37    dest = ensure_resource_path(*parts)
38    if dest.is_file():
39        return dest
40    src = resource_dir.joinpath(*parts)
41    if src.is_file():
42        shutil.copy2(src, dest)
43    return dest

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