holocubic-cli-python 0.1.0a1__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.
- holocubic_cli_python/__init__.py +3 -0
- holocubic_cli_python/__main__.py +3 -0
- holocubic_cli_python/app.py +85 -0
- holocubic_cli_python/cli.py +543 -0
- holocubic_cli_python/client.py +375 -0
- holocubic_cli_python/config.py +133 -0
- holocubic_cli_python/errors.py +27 -0
- holocubic_cli_python/models.py +179 -0
- holocubic_cli_python/remote_path.py +62 -0
- holocubic_cli_python/transfer.py +661 -0
- holocubic_cli_python/transport.py +99 -0
- holocubic_cli_python/url.py +52 -0
- holocubic_cli_python-0.1.0a1.dist-info/METADATA +67 -0
- holocubic_cli_python-0.1.0a1.dist-info/RECORD +17 -0
- holocubic_cli_python-0.1.0a1.dist-info/WHEEL +4 -0
- holocubic_cli_python-0.1.0a1.dist-info/entry_points.txt +2 -0
- holocubic_cli_python-0.1.0a1.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""HTTP transport with stable timeout, connection, and device error handling."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import socket
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any, Mapping
|
|
7
|
+
from urllib.error import HTTPError, URLError
|
|
8
|
+
from urllib.request import Request, urlopen
|
|
9
|
+
|
|
10
|
+
from .errors import CubicError, HttpError
|
|
11
|
+
from .url import api_url, normalize_device_url
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class HttpResponse:
|
|
16
|
+
body: bytes
|
|
17
|
+
headers: Mapping[str, str]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class HttpTransport:
|
|
21
|
+
def __init__(self, base_url: str, timeout_ms: int = 60_000) -> None:
|
|
22
|
+
if timeout_ms <= 0:
|
|
23
|
+
raise CubicError(
|
|
24
|
+
"Timeout must be greater than zero.", code="INVALID_TIMEOUT"
|
|
25
|
+
)
|
|
26
|
+
self.base_url = normalize_device_url(base_url)
|
|
27
|
+
self.timeout_ms = timeout_ms
|
|
28
|
+
|
|
29
|
+
def request(
|
|
30
|
+
self,
|
|
31
|
+
route: str,
|
|
32
|
+
*,
|
|
33
|
+
method: str = "GET",
|
|
34
|
+
query: Mapping[str, str | int | bool | None] | None = None,
|
|
35
|
+
body: bytes | None = None,
|
|
36
|
+
headers: Mapping[str, str] | None = None,
|
|
37
|
+
) -> HttpResponse:
|
|
38
|
+
url = api_url(self.base_url, route, query)
|
|
39
|
+
request = Request(
|
|
40
|
+
url,
|
|
41
|
+
data=body,
|
|
42
|
+
method=method,
|
|
43
|
+
headers={"Accept": "application/json", **(headers or {})},
|
|
44
|
+
)
|
|
45
|
+
try:
|
|
46
|
+
with urlopen(request, timeout=self.timeout_ms / 1000) as response:
|
|
47
|
+
return HttpResponse(response.read(), dict(response.headers.items()))
|
|
48
|
+
except HTTPError as error:
|
|
49
|
+
request_path = url.removeprefix(self.base_url)
|
|
50
|
+
message = f"{method} {request_path} failed with HTTP {error.code}."
|
|
51
|
+
try:
|
|
52
|
+
payload = json.loads(error.read().decode("utf-8"))
|
|
53
|
+
if isinstance(payload, dict):
|
|
54
|
+
detail = (
|
|
55
|
+
payload.get("error")
|
|
56
|
+
if isinstance(payload.get("error"), str)
|
|
57
|
+
else payload.get("message")
|
|
58
|
+
)
|
|
59
|
+
if isinstance(detail, str):
|
|
60
|
+
message = f"{message} {detail}"
|
|
61
|
+
except (json.JSONDecodeError, UnicodeDecodeError, OSError):
|
|
62
|
+
pass
|
|
63
|
+
raise HttpError(message, error.code, method, request_path) from error
|
|
64
|
+
except (socket.timeout, TimeoutError) as error:
|
|
65
|
+
raise CubicError(
|
|
66
|
+
f"Request timed out after {self.timeout_ms} ms.", code="TIMEOUT"
|
|
67
|
+
) from error
|
|
68
|
+
except URLError as error:
|
|
69
|
+
if isinstance(error.reason, (socket.timeout, TimeoutError)):
|
|
70
|
+
raise CubicError(
|
|
71
|
+
f"Request timed out after {self.timeout_ms} ms.", code="TIMEOUT"
|
|
72
|
+
) from error
|
|
73
|
+
raise CubicError(
|
|
74
|
+
f"Unable to connect to {self.base_url}.", code="CONNECTION_ERROR"
|
|
75
|
+
) from error
|
|
76
|
+
except OSError as error:
|
|
77
|
+
raise CubicError(
|
|
78
|
+
f"Unable to connect to {self.base_url}.", code="CONNECTION_ERROR"
|
|
79
|
+
) from error
|
|
80
|
+
|
|
81
|
+
def json(
|
|
82
|
+
self,
|
|
83
|
+
route: str,
|
|
84
|
+
*,
|
|
85
|
+
method: str = "GET",
|
|
86
|
+
query: Mapping[str, str | int | bool | None] | None = None,
|
|
87
|
+
body: bytes | None = None,
|
|
88
|
+
headers: Mapping[str, str] | None = None,
|
|
89
|
+
) -> Any:
|
|
90
|
+
response = self.request(
|
|
91
|
+
route, method=method, query=query, body=body, headers=headers
|
|
92
|
+
)
|
|
93
|
+
try:
|
|
94
|
+
return json.loads(response.body.decode("utf-8"))
|
|
95
|
+
except (json.JSONDecodeError, UnicodeDecodeError) as error:
|
|
96
|
+
raise CubicError(
|
|
97
|
+
f"Device returned malformed JSON for /api/{route}.",
|
|
98
|
+
code="INVALID_RESPONSE",
|
|
99
|
+
) from error
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
from urllib.parse import SplitResult, urlencode, urlsplit, urlunsplit
|
|
3
|
+
|
|
4
|
+
from .errors import UsageError
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def normalize_device_url(value: str) -> str:
|
|
8
|
+
trimmed = value.strip()
|
|
9
|
+
if not trimmed:
|
|
10
|
+
raise UsageError("Device host cannot be empty.")
|
|
11
|
+
if "\0" in trimmed:
|
|
12
|
+
raise UsageError("Device host contains an invalid NUL character.")
|
|
13
|
+
|
|
14
|
+
candidate = trimmed if "://" in trimmed else f"http://{trimmed}"
|
|
15
|
+
try:
|
|
16
|
+
parsed = urlsplit(candidate)
|
|
17
|
+
_ = parsed.port
|
|
18
|
+
except ValueError as error:
|
|
19
|
+
raise UsageError(f"Invalid device host: {value}") from error
|
|
20
|
+
|
|
21
|
+
if parsed.scheme not in {"http", "https"}:
|
|
22
|
+
raise UsageError(f"Unsupported device URL scheme: {parsed.scheme}")
|
|
23
|
+
if not parsed.hostname:
|
|
24
|
+
raise UsageError(f"Invalid device host: {value}")
|
|
25
|
+
if parsed.username is not None or parsed.password is not None:
|
|
26
|
+
raise UsageError("Credentials are not allowed in the device URL.")
|
|
27
|
+
if parsed.query or parsed.fragment:
|
|
28
|
+
raise UsageError("Device URL must not contain a query string or fragment.")
|
|
29
|
+
|
|
30
|
+
path = parsed.path.rstrip("/")
|
|
31
|
+
if path in {"", "/devtools"}:
|
|
32
|
+
path = "/devtools"
|
|
33
|
+
elif path == "/devtools/api":
|
|
34
|
+
path = "/devtools"
|
|
35
|
+
else:
|
|
36
|
+
raise UsageError("Device URL path must be /devtools or /devtools/api.")
|
|
37
|
+
|
|
38
|
+
normalized = SplitResult(parsed.scheme.lower(), parsed.netloc, path, "", "")
|
|
39
|
+
return urlunsplit(normalized)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def api_url(
|
|
43
|
+
base_url: str,
|
|
44
|
+
route: str,
|
|
45
|
+
query: Mapping[str, str | int | bool | None] | None = None,
|
|
46
|
+
) -> str:
|
|
47
|
+
clean_route = route.lstrip("/")
|
|
48
|
+
pairs = [
|
|
49
|
+
(key, str(value)) for key, value in (query or {}).items() if value is not None
|
|
50
|
+
]
|
|
51
|
+
suffix = f"?{urlencode(pairs)}" if pairs else ""
|
|
52
|
+
return f"{base_url}/api/{clean_route}{suffix}"
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: holocubic-cli-python
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Python CLI for HoloCubic DevTools file and app workflows
|
|
5
|
+
Project-URL: Repository, https://github.com/Tim-1e/holocubic-cli
|
|
6
|
+
Project-URL: Issues, https://github.com/Tim-1e/holocubic-cli/issues
|
|
7
|
+
Author: Tim-1e
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: cli,devtools,esp32,holocubic
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# HoloCubic CLI — Python
|
|
15
|
+
|
|
16
|
+
`cubic-py` is the Python implementation of the shared HoloCubic CLI v1
|
|
17
|
+
contract. It supports the same device configuration, SD-card filesystem,
|
|
18
|
+
recursive transfer, DevRun, and app workflows as the Node.js reference.
|
|
19
|
+
|
|
20
|
+
The package requires Python 3.10 or newer. It is source-ready but has not been
|
|
21
|
+
published to PyPI yet.
|
|
22
|
+
|
|
23
|
+
## Installation from source
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
python -m venv .venv
|
|
27
|
+
python -m pip install --editable .
|
|
28
|
+
cubic-py --version
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Activate `.venv` first if you want `cubic-py` available only inside the virtual
|
|
32
|
+
environment. Connect and inspect a device with:
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
cubic-py device add desk 192.168.3.26
|
|
36
|
+
cubic-py info
|
|
37
|
+
cubic-py ls /sd/apps
|
|
38
|
+
cubic-py push ./my-app /sd/apps/my-app
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Command overview
|
|
42
|
+
|
|
43
|
+
```text
|
|
44
|
+
cubic-py device add|list|use|remove
|
|
45
|
+
cubic-py ping|info
|
|
46
|
+
cubic-py ls|stat|cat|mkdir|mv|rm
|
|
47
|
+
cubic-py push|upload
|
|
48
|
+
cubic-py pull|download
|
|
49
|
+
cubic-py devrun read|save|run
|
|
50
|
+
cubic-py app list|install|remove
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Use `--host` for one-off access, `--json` for scripts, and `--help` on a command
|
|
54
|
+
for its transfer limits and safety options.
|
|
55
|
+
|
|
56
|
+
## Development checks
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
python -m ruff check src tests
|
|
60
|
+
python -m ruff format --check src tests
|
|
61
|
+
python -m unittest discover -s tests -v
|
|
62
|
+
python -m build
|
|
63
|
+
python -m pip install .
|
|
64
|
+
cubic-py --version
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The API and CLI contracts live in [`../../spec`](../../spec).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
holocubic_cli_python/__init__.py,sha256=hrdrnM-u_l0TjX46dnWc-7QE_kiTn2Tb29np3nwMRrM,75
|
|
2
|
+
holocubic_cli_python/__main__.py,sha256=k1ocEWawweo1qCJWNFAAvyxz3tcY13dzvCenHszij30,48
|
|
3
|
+
holocubic_cli_python/app.py,sha256=lWoCrVRdyNbGeOopKa5tsSa7h-PgPOZ9vA9tP1XkbcY,2646
|
|
4
|
+
holocubic_cli_python/cli.py,sha256=I5tCv_n_t2OMiPG5-FXt5a8J3CDtPUHmd2UTeGOzUS8,21546
|
|
5
|
+
holocubic_cli_python/client.py,sha256=q-TMvd0cXkppbIPctUGUoajkgpVMUulACYSPRCHiM5Q,13642
|
|
6
|
+
holocubic_cli_python/config.py,sha256=3-9AdyDz7BK5HqxKfFFzSShfSunELwDGDENC5cCiaow,4626
|
|
7
|
+
holocubic_cli_python/errors.py,sha256=9HeW5hINsNTrBr3OJXTf4urGbm33l76q1neJk1gdLaU,864
|
|
8
|
+
holocubic_cli_python/models.py,sha256=-l9M147CSoMqKG-1PZUT7zmzUbr6e34BxCWQA1YKJNM,3682
|
|
9
|
+
holocubic_cli_python/remote_path.py,sha256=vcrWOV0yXlRY6xo82vIoREU5XP-3VLfhrv5-b5wTw5s,2246
|
|
10
|
+
holocubic_cli_python/transfer.py,sha256=jgerCQcN1e2m1S1cqwn5AfAb7NyoyxGKBtxMcyaGAEo,22880
|
|
11
|
+
holocubic_cli_python/transport.py,sha256=-FlmaHnWbEsQxn5xcXm8PNo_gsL_BxiE2bIztDSvLqM,3686
|
|
12
|
+
holocubic_cli_python/url.py,sha256=njCGbEiRhAQckE9FV4sbXlW2pcq3qMGgXV3V4iQVEBM,1841
|
|
13
|
+
holocubic_cli_python-0.1.0a1.dist-info/METADATA,sha256=LiIWNQpSnsGcNlIZyS0Ylr3s7se2mMzG4etn92mswVo,1766
|
|
14
|
+
holocubic_cli_python-0.1.0a1.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
|
|
15
|
+
holocubic_cli_python-0.1.0a1.dist-info/entry_points.txt,sha256=F51dwgEYSzV07UIMjK6AwgVua4ogasJkhqbAV3sS8IM,59
|
|
16
|
+
holocubic_cli_python-0.1.0a1.dist-info/licenses/LICENSE,sha256=XoTz3rF6vKPRejMH_vRunzeQCB2t0nX71o04KgFUnTU,1063
|
|
17
|
+
holocubic_cli_python-0.1.0a1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tim-1e
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|