Repository navigation
Expand file tree
/
Copy pathpaths.py
More file actions
64 lines (51 loc) · 2.33 KB
/
Copy pathpaths.py
File metadata and controls
64 lines (51 loc) · 2.33 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
"""
paths.py - rutas de datos de MechaLeveling y utilidades de plataforma.
Todo lo que la app escribe (config, mapas, historial, logs, pcb_output,
library, backups) cuelga de DATA_DIR. Por defecto DATA_DIR es la carpeta del
repo (los archivos generados estan en .gitignore). Se puede mover con la
variable de entorno MECHALEVELING_HOME.
"""
import os
import subprocess
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
FROZEN = bool(getattr(sys, "frozen", False)) # True dentro de un .app / .exe empaquetado
def _default_data_dir():
"""Repo cuando se corre desde el codigo; carpeta de datos del usuario cuando
la app viene empaquetada (el .app es de solo lectura y puede vivir en /Applications)."""
if not FROZEN:
return BASE_DIR
if sys.platform == "darwin":
return os.path.expanduser("~/Library/Application Support/MechaLeveling")
if sys.platform.startswith("win"):
return os.path.join(os.environ.get("APPDATA", os.path.expanduser("~")), "MechaLeveling")
return os.path.join(os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share")), "MechaLeveling")
DATA_DIR = os.path.abspath(os.path.expanduser(os.environ.get("MECHALEVELING_HOME") or _default_data_dir()))
LOG_DIR = os.path.join(DATA_DIR, "logs")
LIBRARY_DIR = os.path.join(DATA_DIR, "library")
PCB_OUTPUT_DIR = os.path.join(DATA_DIR, "pcb_output")
BACKUPS_DIR = os.path.join(DATA_DIR, "backups")
HISTORY_FILE = os.path.join(DATA_DIR, "job_history.json")
USER_CONFIG_FILE = os.path.join(DATA_DIR, "config_user.json")
def data_path(*parts):
"""Ruta absoluta dentro de DATA_DIR."""
return os.path.join(DATA_DIR, *parts)
def ensure_dirs():
"""Crea las carpetas de datos si no existen. Idempotente."""
for d in (DATA_DIR, LOG_DIR, LIBRARY_DIR, PCB_OUTPUT_DIR, BACKUPS_DIR):
os.makedirs(d, exist_ok=True)
def open_path(path):
"""
Abre un archivo o carpeta con la aplicacion por defecto del sistema.
Devuelve True si se pudo lanzar el comando. Nunca lanza.
"""
try:
if sys.platform == "darwin":
subprocess.Popen(["open", path])
elif sys.platform.startswith("win"):
os.startfile(path) # type: ignore[attr-defined]
else:
subprocess.Popen(["xdg-open", path])
return True
except (OSError, subprocess.SubprocessError):
return False