tzafon 0.1.1__py3-none-any.whl → 0.1.2__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.

Potentially problematic release.


This version of tzafon might be problematic. Click here for more details.

tzafon/_connection.py CHANGED
@@ -2,8 +2,7 @@ from __future__ import annotations
2
2
  import asyncio
3
3
  import traceback
4
4
  import websockets
5
- from websockets.connection import State
6
-
5
+ from websockets.protocol import State
7
6
  from .models import Command, Result
8
7
 
9
8
 
tzafon/client.py CHANGED
@@ -56,7 +56,8 @@ class Waypoint:
56
56
  path: str | os.PathLike | None = None,
57
57
  *,
58
58
  mkdir: bool = True,
59
- ) -> bytes:
59
+ return_url: bool = False,
60
+ ) -> bytes | str:
60
61
  """
61
62
  Grab a **JPEG** screenshot.
62
63
 
@@ -66,8 +67,29 @@ class Waypoint:
66
67
  *and* the raw bytes are still returned.
67
68
  mkdir:
68
69
  Automatically create parent folders if they do not exist.
70
+ return_url:
71
+ If True, return the remote URL instead of the image bytes.
69
72
  """
70
73
  res = await self._send(Command(ActionType.SCREENSHOT))
74
+
75
+ if return_url:
76
+ if res.image_url:
77
+ return res.image_url
78
+ if path is not None:
79
+ from pathlib import Path
80
+
81
+ p = Path(path)
82
+ if mkdir:
83
+ p.parent.mkdir(parents=True, exist_ok=True)
84
+ p.write_bytes(res.image)
85
+ return str(p.resolve())
86
+ raise ScreenshotFailed(
87
+ "Server did not provide image_url; set return_url=False to get bytes"
88
+ )
89
+
90
+ if res.success and res.image is None and res.image_url is not None:
91
+ await res.download_image()
92
+
71
93
  if not (res.success and res.image):
72
94
  raise ScreenshotFailed(res.error_message or "unknown error")
73
95
 
tzafon/models.py CHANGED
@@ -52,8 +52,23 @@ class Command:
52
52
  class Result:
53
53
  success: bool
54
54
  image: Optional[bytes] = None # jpeg bytes
55
+ image_url: Optional[str] = None # remote location if bytes omitted
55
56
  error_message: Optional[str] = None
56
57
 
58
+ async def download_image(self) -> None:
59
+ """If `image` is None but `image_url` present, fetch the bytes (async)."""
60
+ if self.image is None and self.image_url is not None:
61
+ try:
62
+ import httpx
63
+
64
+ async with httpx.AsyncClient(timeout=20.0, follow_redirects=True) as c:
65
+ resp = await c.get(self.image_url)
66
+ resp.raise_for_status()
67
+ self.image = resp.content
68
+ except Exception:
69
+ # keep silent – caller will treat as missing image later
70
+ pass
71
+
57
72
  @classmethod
58
73
  def load(cls, body: bytes) -> Self:
59
74
  data = json.loads(body.decode("utf-8"))
@@ -62,6 +77,7 @@ class Result:
62
77
  return cls(
63
78
  success=data.get("success", False),
64
79
  image=img,
80
+ image_url=data.get("image_url"),
65
81
  error_message=data.get("error_message"),
66
82
  )
67
83
 
@@ -69,8 +85,12 @@ class Result:
69
85
  d: Dict[str, Any] = {
70
86
  "success": self.success,
71
87
  "error_message": self.error_message,
72
- "image": base64.b64encode(self.image).decode() if self.image else None,
73
88
  }
89
+
90
+ if self.image is not None:
91
+ d["image"] = base64.b64encode(self.image).decode()
92
+ if self.image_url is not None:
93
+ d["image_url"] = self.image_url
74
94
  return json.dumps({k: v for k, v in d.items() if v is not None}).encode()
75
95
 
76
96
  def __str__(self) -> str:
@@ -1,9 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tzafon
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: Tzafon Waypoint – browser automation
5
5
  License: MIT
6
6
  Requires-Python: >=3.10
7
+ Requires-Dist: httpx>=0.26.0
8
+ Requires-Dist: pytest-asyncio>=0.24.0
9
+ Requires-Dist: pytest>=8.3.4
10
+ Requires-Dist: python-dotenv>=1.1.0
7
11
  Requires-Dist: twine>=6.1.0
8
12
  Requires-Dist: websockets>=15.0.1
9
13
  Description-Content-Type: text/markdown
@@ -0,0 +1,8 @@
1
+ tzafon/__init__.py,sha256=QpZABzfciDC1uUdoRHp5tW0694ZJGtHNF9ieGilnt94,53
2
+ tzafon/_connection.py,sha256=6kqekASLSAGrjx5bWJNVTQ3mifw2MbemB8b8xJm8_Qg,1599
3
+ tzafon/client.py,sha256=uzh8ZLntw9cOIK5jAcfSokrpfBWxIksm-AuH3xfHM3s,3901
4
+ tzafon/exceptions.py,sha256=SeeZRBAsyeogXXcfF3b3IaKUsehWNeNZAgxbmrzjI48,168
5
+ tzafon/models.py,sha256=HTpU4YwjbggV85l5-_VxVvjHGOX_jupSNtMEUZ4f1oM,3239
6
+ tzafon-0.1.2.dist-info/METADATA,sha256=hjU1dVxy-MnGkXjaQNTeaj7WDdOIVnVCIAQjiAAOOic,1184
7
+ tzafon-0.1.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
+ tzafon-0.1.2.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- tzafon/__init__.py,sha256=QpZABzfciDC1uUdoRHp5tW0694ZJGtHNF9ieGilnt94,53
2
- tzafon/_connection.py,sha256=HmmnHjO0N9pN3esCsVucws7lm1ZfyB6F0G8GMZoOCXw,1602
3
- tzafon/client.py,sha256=NUwXgrGPhoFbmHq122ZtSLBJvs2hiz1d8EbaRsGDW8o,3149
4
- tzafon/exceptions.py,sha256=SeeZRBAsyeogXXcfF3b3IaKUsehWNeNZAgxbmrzjI48,168
5
- tzafon/models.py,sha256=woHESkPE3R2TbiQoZjlknz7VR938WU5ZYlPFCh0Td9U,2428
6
- tzafon-0.1.1.dist-info/METADATA,sha256=nJmiwa8WRJFuQvhPtxTqWDl4ugGMlZsfmzXtBZo2ooE,1052
7
- tzafon-0.1.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- tzafon-0.1.1.dist-info/RECORD,,
File without changes