Skip to content

Commit 3495ffd

Browse files
authored
Move decompress_7z_base64_data to rust (#723)
* Move decompress_7z_base64_data to rust * Add error handling * go on * Fix setup * Fix typing * fix pylint * Enable renovate for rust
1 parent 4ca414f commit 3495ffd

18 files changed

Lines changed: 478 additions & 84 deletions

File tree

.devcontainer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@
4444
},
4545
// Add the IDs of extensions you want installed when the container is created.
4646
"features": {
47-
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
47+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
48+
"ghcr.io/devcontainers/features/rust:1": {}
4849
},
4950
"image": "mcr.microsoft.com/devcontainers/base:debian",
5051
"name": "Deebot client",
5152
// Use 'forwardPorts' to make a list of ports inside the container available locally.
5253
// "forwardPorts": [],
5354
// Use 'postCreateCommand' to run commands after the container is created.
54-
"postCreateCommand": "curl -LsSf https://astral.sh/uv/install.sh | sh && uv sync --frozen --dev && pre-commit install",
55+
"postCreateCommand": "scripts/setup.sh",
5556
"postStartCommand": "uv sync --dev",
5657
"remoteEnv": {
5758
"PATH": "/home/vscode/.local/bin:/home/vscode/.cargo/bin:/workspaces/client.py/.venv/bin:${containerEnv:PATH}"

.github/renovate.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
"addLabels": ["github_actions"],
2222
"matchManagers": ["github-actions"],
2323
"rangeStrategy": "pin"
24+
},
25+
{
26+
"addLabels": ["rust"],
27+
"matchManagers": ["cargo"]
2428
}
2529
],
2630
"rebaseWhen": "behind-base-branch"

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: uv run --frozen mypy deebot_client/
3333

3434
- name: Pylint review
35-
run: uv run --frozen pylint deebot_client/
35+
run: uv run --frozen pylint deebot_client/**/*.py
3636

3737
- name: Verify no getLogger usages
3838
run: scripts/check_getLogger.sh

.github/workflows/python-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
- name: 🏗 Set package version
3333
run: |
3434
sed -i "s/^version = \".*\"/version = \"${{ github.event.release.tag_name }}\"/" pyproject.toml
35+
sed -i "s/^version = \".*\"/version = \"${{ github.event.release.tag_name }}\"/" cargo.toml
3536
- name: 📦 Build package
3637
run: uv build
3738
- name: 🚀 Publish to PyPi

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ nosetests.xml
2121
.*_cache
2222

2323
test.py
24-
.env
24+
.env
25+
26+
/target
27+
28+
# rust so
29+
*.cpython*.so

Cargo.lock

Lines changed: 201 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "deebot_client"
3+
version = "0.0.0"
4+
edition = "2021"
5+
authors = ["Robert Resch <robert@resch.dev>"]
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
[lib]
9+
name = "deebot_client"
10+
crate-type = ["cdylib"]
11+
12+
[dependencies]
13+
base64 = "0.22.1"
14+
pyo3 = "0.23.3"
15+
rust-lzma = "0.6.0"

deebot_client/commands/json/map.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from deebot_client.events.map import CachedMapInfoEvent
1919
from deebot_client.logging_filter import get_logger
2020
from deebot_client.message import HandlingResult, HandlingState, MessageBodyDataDict
21-
from deebot_client.util import decompress_7z_base64_data
21+
from deebot_client.rs import decompress_7z_base64_data
2222

2323
from .common import JsonCommandWithMessageHandling
2424

@@ -275,7 +275,7 @@ def _handle_body_data_dict(
275275
# This command is used by new and old bots
276276
if data.get("compress", 0) == 1:
277277
# Newer bot's return coordinates as base64 decoded string
278-
coordinates = decompress_7z_base64_data(data["value"]).decode()
278+
coordinates = decompress_7z_base64_data(data["value"])
279279
else:
280280
# Older bot's return coordinates direct as comma/semicolon separated list
281281
coordinates = data["value"]
@@ -305,7 +305,7 @@ def _get_subset_ids(
305305
) -> list[int] | None:
306306
"""Return subset ids."""
307307
# subset is based64 7z compressed
308-
subsets = json.loads(decompress_7z_base64_data(data["subsets"]).decode())
308+
subsets = json.loads(decompress_7z_base64_data(data["subsets"]))
309309

310310
match data["type"]:
311311
case MapSetType.ROOMS:

deebot_client/map.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
from .exceptions import MapError
3636
from .logging_filter import get_logger
3737
from .models import Room
38+
from .rs import (
39+
decompress_7z_base64_data,
40+
)
3841
from .util import (
3942
OnChangedDict,
4043
OnChangedList,
41-
decompress_7z_base64_data,
4244
)
4345

4446
if TYPE_CHECKING:
@@ -393,7 +395,7 @@ async def on_map_subset(event: MapSubsetEvent) -> None:
393395

394396
def _update_trace_points(self, data: str) -> None:
395397
_LOGGER.debug("[_update_trace_points] Begin")
396-
trace_points = decompress_7z_base64_data(data)
398+
trace_points = decompress_7z_base64_data(data).encode()
397399

398400
for i in range(0, len(trace_points), 5):
399401
position_x, position_y = struct.unpack("<hh", trace_points[i : i + 4])
@@ -620,7 +622,7 @@ def image(self) -> Image.Image:
620622

621623
def update_points(self, base64_data: str) -> None:
622624
"""Add map piece points."""
623-
decoded = decompress_7z_base64_data(base64_data)
625+
decoded = decompress_7z_base64_data(base64_data).encode()
624626
old_crc32 = self._crc32
625627
self._crc32 = zlib.crc32(decoded)
626628

deebot_client/rs.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def decompress_7z_base64_data(value: str) -> str:
2+
"""Decompress base64 decoded 7z compressed string."""

0 commit comments

Comments
 (0)