Skip to content

Commit

Permalink
validate krec files
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Nov 23, 2024
1 parent d97a502 commit a6279de
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions kscale/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ pydantic
# CLI
click
aiofiles

# K-Scale
krec
17 changes: 12 additions & 5 deletions kscale/web/krec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import aiofiles
import click
import httpx
import krec

from kscale.utils.cli import coro
from kscale.web.gen.api import UploadKRecRequest
Expand All @@ -20,7 +21,6 @@
async def upload_krec(
robot_id: str,
file_path: Path,
name: str,
description: str | None = None,
upload_timeout: float = DEFAULT_UPLOAD_TIMEOUT,
) -> str:
Expand All @@ -32,11 +32,19 @@ async def upload_krec(
logger.info("File name: %s", file_path.name)
logger.info("File size: %.1f MB", file_size / 1024 / 1024)

if not file_path.suffix.lower() == ".krec":
logger.warning("File extension is not .krec - are you sure this is a valid K-Rec file?")

try:
krec.KRec.load(file_path)
except Exception as e:
raise ValueError(f"Failed to load K-Rec from {file_path} - are you sure this is a valid K-Rec file?") from e

async with KScaleWWWClient(upload_timeout=upload_timeout) as client:
create_response = await client.create_krec(
UploadKRecRequest(
robot_id=robot_id,
name=name,
name=file_path.name,
description=description,
)
)
Expand Down Expand Up @@ -146,12 +154,11 @@ def cli() -> None:
@cli.command()
@click.argument("robot_id")
@click.argument("file_path", type=click.Path(exists=True, path_type=Path))
@click.option("--name", "-n", help="Name of the K-Rec", required=True)
@click.option("--description", "-d", help="Description of the K-Rec")
@coro
async def upload(robot_id: str, file_path: Path, name: str, description: str | None = None) -> None:
async def upload(robot_id: str, file_path: Path, description: str | None = None) -> None:
"""Upload a K-Rec file."""
krec_id = await upload_krec(robot_id, file_path, name, description)
krec_id = await upload_krec(robot_id, file_path, description)
click.echo(f"Successfully uploaded K-Rec: {krec_id}")


Expand Down

0 comments on commit a6279de

Please sign in to comment.