tracereports 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ """
2
+ TraceReports — cliente Python.
3
+
4
+ from tracereports import TraceReports # API REST (steps, capturas, red)
5
+ from tracereports import attach_listeners, reportar_red # captura de red con Playwright
6
+
7
+ Con pytest no hace falta código: `pytest --tracereports` (ver tracereports.pytest_plugin).
8
+ """
9
+
10
+ from .client import TraceReports, __version__
11
+ from .dom import capturar_dom
12
+ from .network import (
13
+ attach_listeners,
14
+ conexiones_del_test,
15
+ esperar_conexion,
16
+ guardar_red_local,
17
+ reportar_red,
18
+ resumir_red,
19
+ )
20
+
21
+ __all__ = [
22
+ "TraceReports",
23
+ "attach_listeners",
24
+ "capturar_dom",
25
+ "conexiones_del_test",
26
+ "esperar_conexion",
27
+ "guardar_red_local",
28
+ "reportar_red",
29
+ "resumir_red",
30
+ "__version__",
31
+ ]
tracereports/_env.py ADDED
@@ -0,0 +1,90 @@
1
+ """
2
+ Variables de entorno del cliente: TRACEREPORTS_<NOMBRE>.
3
+
4
+ Si la variable no está en el entorno, se busca en el archivo .env del proyecto (el mismo que lee el
5
+ servidor): así el token vive en un solo lugar, ignorado por git, y no hace falta definirlo en cada
6
+ terminal ni de forma global en el sistema. Las variables del entorno siempre ganan (en CI llegan
7
+ como secrets). Del archivo solo se usan las claves TRACEREPORTS_*.
8
+
9
+ El .env se busca desde la carpeta actual hacia arriba, hasta la raíz del repositorio (la carpeta
10
+ con .git). TRACEREPORTS_ENV_FILE indica otro archivo, u "off" para no leer ninguno.
11
+ """
12
+
13
+ import os
14
+ from typing import Dict, Optional
15
+
16
+ PREFIX = "TRACEREPORTS_"
17
+ _OFF = "off"
18
+
19
+ _file_values: Optional[Dict[str, str]] = None
20
+ _file_path: Optional[str] = None
21
+
22
+
23
+ def parse_env_file(path: str) -> Dict[str, str]:
24
+ """Líneas KEY=VALUE con las mismas reglas que el servidor: comentarios "#", prefijo "export ",
25
+ comillas alrededor del valor y comentario al final de la línea (" #")."""
26
+ values: Dict[str, str] = {}
27
+ try:
28
+ with open(path, encoding="utf-8-sig") as fh:
29
+ lines = fh.read().splitlines()
30
+ except OSError:
31
+ return values
32
+ for raw in lines:
33
+ line = raw.strip()
34
+ if not line or line.startswith("#"):
35
+ continue
36
+ if line.startswith("export "):
37
+ line = line[len("export "):]
38
+ key, sep, val = line.partition("=")
39
+ key = key.strip()
40
+ if not sep or not key or " " in key or "\t" in key:
41
+ continue
42
+ val = val.strip()
43
+ if len(val) >= 2 and val[0] == val[-1] and val[0] in "\"'":
44
+ val = val[1:-1]
45
+ elif " #" in val:
46
+ val = val[: val.index(" #")].strip()
47
+ values[key] = val
48
+ return values
49
+
50
+
51
+ def find_env_file(start: Optional[str] = None) -> Optional[str]:
52
+ """El .env que corresponde: TRACEREPORTS_ENV_FILE, o el primero desde start (la carpeta
53
+ actual) hacia arriba sin pasar de la raíz del repositorio. None si no hay o está desactivado."""
54
+ explicit = os.getenv(PREFIX + "ENV_FILE")
55
+ if explicit:
56
+ return None if explicit.strip().lower() == _OFF else explicit
57
+ d = os.path.abspath(start or os.getcwd())
58
+ while True:
59
+ candidate = os.path.join(d, ".env")
60
+ if os.path.isfile(candidate):
61
+ return candidate
62
+ parent = os.path.dirname(d)
63
+ if os.path.exists(os.path.join(d, ".git")) or parent == d:
64
+ return None
65
+ d = parent
66
+
67
+
68
+ def _file() -> Dict[str, str]:
69
+ global _file_values, _file_path
70
+ if _file_values is None:
71
+ _file_path = find_env_file()
72
+ loaded = parse_env_file(_file_path) if _file_path else {}
73
+ _file_values = {k: v for k, v in loaded.items() if k.startswith(PREFIX)}
74
+ return _file_values
75
+
76
+
77
+ def env_file_in_use() -> Optional[str]:
78
+ """Ruta del .env del que se leyó alguna variable (para los mensajes), o None."""
79
+ return _file_path if _file() else None
80
+
81
+
82
+ def reset_env_file() -> None:
83
+ """Vuelve a buscar el .env en la próxima lectura (tests)."""
84
+ global _file_values, _file_path
85
+ _file_values, _file_path = None, None
86
+
87
+
88
+ def env(name: str, default: str = "") -> str:
89
+ """TRACEREPORTS_<name>: primero del entorno, después del .env del proyecto."""
90
+ return os.getenv(PREFIX + name, "") or _file().get(PREFIX + name, "") or default