-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiplatform.py
More file actions
115 lines (93 loc) · 3.72 KB
/
Copy pathmultiplatform.py
File metadata and controls
115 lines (93 loc) · 3.72 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- encoding=utf-8 -*-
import os
import platform
import subprocess
from pathlib import Path
from typing import Optional
def _linux_download_dir_from_xdg_user_dir() -> Optional[Path]:
try:
output = subprocess.check_output(
["xdg-user-dir", "DOWNLOAD"],
stderr=subprocess.DEVNULL,
text=True,
)
except Exception:
return None
candidate = output.strip()
if not candidate:
return None
expanded = os.path.expandvars(candidate)
return Path(expanded).expanduser()
def _linux_download_dir_from_user_dirs_file() -> Optional[Path]:
config_path = Path("~/.config/user-dirs.dirs").expanduser()
if not config_path.exists():
return None
try:
with config_path.open("r", encoding="utf-8") as f:
for raw_line in f:
line = raw_line.strip()
if not line or line.startswith("#"):
continue
if not line.startswith("XDG_DOWNLOAD_DIR="):
continue
value = line.split("=", 1)[1].strip()
if (value.startswith('"') and value.endswith('"')) or (
value.startswith("'") and value.endswith("'")
):
value = value[1:-1]
expanded = os.path.expandvars(value)
if expanded.strip():
return Path(expanded).expanduser()
except Exception:
return None
return None
def get_system_downloads_dir() -> Path:
"""Return the current system Downloads directory.
On Windows this resolves the shell known-folder registry entry so customized
locations (for example D:\\Downloads) are respected.
"""
if platform.system() == "Windows":
try:
import winreg
with winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders",
) as key:
value, _ = winreg.QueryValueEx(key, "{374DE290-123F-4565-9164-39C4925E467B}")
resolved = os.path.expandvars(str(value))
if resolved.strip():
return Path(resolved).expanduser()
except Exception:
# Fall back to the conventional path when registry lookup fails.
pass
elif platform.system() == "Linux":
xdg_dir = _linux_download_dir_from_xdg_user_dir()
if xdg_dir is not None:
return xdg_dir
config_dir = _linux_download_dir_from_user_dirs_file()
if config_dir is not None:
return config_dir
return Path("~/Downloads").expanduser()
def get_default_download_dir(app_name: str = "YouGet") -> Path:
"""Return the app default directory under the system Downloads folder."""
return get_system_downloads_dir() / app_name
def open_file(file_path):
'''open a file. use in multiplatform'''
file_path = os.path.realpath(file_path)
if platform.system() == "Windows":
os.startfile(file_path)
elif platform.system() == "Darwin": # macOS
os.system(f'open "{file_path}"')
elif platform.system() == "Linux":
os.system(f'xdg-open "{file_path}"')
def open_folder(folder_path):
'''open a filefolder. use in multiplatform'''
system_name = platform.system()
if system_name == 'Windows':
os.startfile(folder_path)
elif system_name == 'Darwin': # macOS
subprocess.Popen(['open', folder_path])
elif system_name == 'Linux':
subprocess.Popen(['xdg-open', folder_path])
else:
raise OSError('Unsupported operating system: ' + system_name)