Skip to content

feat: use less restrictive io type for file ingest #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion nominal/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
from contextlib import contextmanager
from typing import Any, BinaryIO, Callable, Iterator, TypeVar
from typing import Any, BinaryIO, Callable, Iterator, Protocol, TypeVar

from typing_extensions import ParamSpec

Expand Down Expand Up @@ -64,3 +64,7 @@ def wrapper(*args: Param.args, **kwargs: Param.kwargs) -> T:
return wrapper

return _deprecate_keyword_argument_decorator


class HasBinaryRead(Protocol):
def read(self, length: int = ..., /) -> bytes: ...
9 changes: 5 additions & 4 deletions nominal/core/_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import urllib.parse
from functools import partial
from queue import Queue
from typing import BinaryIO, Iterable
from typing import Iterable

import requests

from nominal._api.scout_service_api import ingest_api, upload_api
from nominal._utils import HasBinaryRead
from nominal.core.filetype import FileType
from nominal.exceptions import NominalMultipartUploadFailed

Expand Down Expand Up @@ -49,14 +50,14 @@ def _sign_and_upload_part_job(
q.task_done()


def _iter_chunks(f: BinaryIO, chunk_size: int) -> Iterable[bytes]:
def _iter_chunks(f: HasBinaryRead, chunk_size: int) -> Iterable[bytes]:
while (data := f.read(chunk_size)) != b"":
yield data


def put_multipart_upload(
auth_header: str,
f: BinaryIO,
f: HasBinaryRead,
filename: str,
mimetype: str,
upload_client: upload_api.UploadService,
Expand Down Expand Up @@ -138,7 +139,7 @@ def put_multipart_upload(

def upload_multipart_io(
auth_header: str,
f: BinaryIO,
f: HasBinaryRead,
name: str,
file_type: FileType,
upload_client: upload_api.UploadService,
Expand Down
12 changes: 6 additions & 6 deletions nominal/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import datetime
from io import BytesIO, TextIOBase, TextIOWrapper
from pathlib import Path
from typing import BinaryIO, Iterable, Mapping, Sequence
from typing import Iterable, Mapping, Sequence

import certifi
from conjure_python_client import ServiceConfiguration, SslConfiguration
Expand All @@ -28,7 +28,7 @@
storage_datasource_api,
timeseries_logicalseries_api,
)
from nominal._utils import deprecate_keyword_argument
from nominal._utils import HasBinaryRead, deprecate_keyword_argument
from nominal.core._clientsbunch import ClientsBunch
from nominal.core._conjure_utils import _available_units, _build_unit_update
from nominal.core._multipart import upload_multipart_file, upload_multipart_io
Expand Down Expand Up @@ -284,7 +284,7 @@ def create_mcap_dataset(

def create_dataset_from_io(
self,
dataset: BinaryIO,
dataset: HasBinaryRead,
name: str,
timestamp_column: str,
timestamp_type: _AnyTimestampType,
Expand Down Expand Up @@ -367,7 +367,7 @@ def create_video(

def create_video_from_io(
self,
video: BinaryIO,
video: HasBinaryRead,
name: str,
start: datetime | IntegralNanosecondsUTC | None = None,
frame_timestamps: Sequence[IntegralNanosecondsUTC] | None = None,
Expand Down Expand Up @@ -567,7 +567,7 @@ def checklist_builder(

def create_attachment_from_io(
self,
attachment: BinaryIO,
attachment: HasBinaryRead,
name: str,
file_type: tuple[str, str] | FileType = FileTypes.BINARY,
description: str | None = None,
Expand Down Expand Up @@ -689,7 +689,7 @@ def get_connection(self, rid: str) -> Connection:

def create_video_from_mcap_io(
self,
mcap: BinaryIO,
mcap: HasBinaryRead,
topic: str,
name: str,
description: str | None = None,
Expand Down
5 changes: 3 additions & 2 deletions nominal/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from io import TextIOBase
from pathlib import Path
from types import MappingProxyType
from typing import BinaryIO, Iterable, Mapping, Protocol, Sequence
from typing import Iterable, Mapping, Protocol, Sequence

import pandas as pd
from typing_extensions import Self
Expand All @@ -23,6 +23,7 @@
timeseries_logicalseries_api,
upload_api,
)
from nominal._utils import HasBinaryRead
from nominal.core._clientsbunch import HasAuthHeader
from nominal.core._conjure_utils import _available_units, _build_unit_update
from nominal.core._multipart import upload_multipart_io
Expand Down Expand Up @@ -173,7 +174,7 @@ def add_data_to_dataset(self, path: Path | str, timestamp_column: str, timestamp

def add_to_dataset_from_io(
self,
dataset: BinaryIO,
dataset: HasBinaryRead,
timestamp_column: str,
timestamp_type: _AnyTimestampType,
file_type: tuple[str, str] | FileType = FileTypes.CSV,
Expand Down
Loading