1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#!/usr/bin/python3
__all__ = ["PathManager"]
import os
from pathlib import Path
class PathManager:
@staticmethod
def system_data(*relative_paths):
"""Search XDG_DATA_DIRS for the given relative path.
Returns the first existing path, or the first configured directory
if none exist.
"""
xdg_data_dirs = os.getenv('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')
for data_dir in xdg_data_dirs:
path = Path(data_dir).joinpath(*relative_paths)
if path.exists():
return str(path)
return str(Path(xdg_data_dirs[0]).joinpath(*relative_paths))
@staticmethod
def user_data(*relative_paths):
"""Return the user data directory path joined with *relative_paths.
Uses XDG_DATA_HOME if set, falling back to ~/.local/share.
"""
xdg_data_home = Path(os.getenv('XDG_DATA_HOME', Path.home() / '.local/share'))
return str(xdg_data_home.joinpath(*relative_paths))
@staticmethod
def user_config(*relative_paths):
"""Return the user config directory path joined with *relative_paths.
Uses XDG_CONFIG_HOME if set, falling back to ~/.config.
"""
xdg_config_home = Path(os.getenv('XDG_CONFIG_HOME', Path.home() / '.config'))
return str(xdg_config_home.joinpath(*relative_paths))
@staticmethod
def find_binary(binary_name):
"""Search the PATH environment variable for *binary_name.
Returns the full path as a string, or an empty string if not found.
"""
paths = os.getenv('PATH', '').split(':')
for path in paths:
binary_path = Path(path) / binary_name
if binary_path.exists():
return str(binary_path)
return ""
@staticmethod
def get_icon(icon_name):
"""Locate an icon by name, checking user data and system icon dirs.
Searches in order: user data, system hicolor 256x256, then system
icons. Returns the first existing path, or the last fallback path.
"""
icon_paths = [
PathManager.user_data('icons', icon_name),
PathManager.system_data('icons/hicolor/256x256/apps', icon_name),
PathManager.system_data('icons', icon_name)
]
for path in icon_paths:
if Path(path).exists():
return path
return icon_paths[-1] # Fallback
|