Sources
timenet_connectors.sources ¶source
Shared source access and media conversion helpers for dataset connectors.
huggingface_hub ¶source
Helpers for downloading files from pinned Hugging Face dataset revisions.
:func:download_hub_files downloads with huggingface_hub.snapshot_download, limited to the
requested files. The Hub client runs the downloads in its own thread pool. Its progress bars stay
hidden: the bar that counts file bytes reaches the ambient :mod:~timenet_connectors.download.progress
sink instead, so the build CLI shows the download like any other connector download.
huggingface_hub is declared in the connector's requirements.txt and imported lazily, which
keeps --no-isolation usable. The Hub client reads HF_TOKEN from the environment, so gated
datasets work with no extra wiring.
download_hub_files
async
¶source
download_hub_files(
repo: str,
filenames: Sequence[str],
*,
revision: str,
cache_dir: Path,
) -> list[Path]
Download files from a dataset revision with one snapshot_download call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo
|
str
|
The Hub dataset id, such as |
required |
filenames
|
Sequence[str]
|
Repository-relative paths of the files to download. |
required |
revision
|
str
|
The revision to read, normally a pinned commit. |
required |
cache_dir
|
Path
|
Directory where the connector caches Hub files. |
required |
Returns:
| Type | Description |
|---|---|
list[Path]
|
The local paths, in the order of |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
TimeNetDownloadError
|
If the download fails, or a file is absent from the revision. |
json_stream ¶source
Stream large JSON documents entry by entry, keeping each value's byte range.
Some sources publish one JSON document of hundreds of megabytes instead of JSON Lines. Decoding it
whole holds the entire corpus in memory. Instead, decode one value at a time, record its UTF-8 byte
range, and let a lazy loader reread just that range with :func:read_entry.
JsonCursor ¶source
Read JSON syntax incrementally while tracking consumed UTF-8 bytes.
The cursor reads raw bytes and decodes UTF-8 itself, so its offsets always count the file's own bytes, CRLF included. Dataset-specific key and structure validation belongs to the caller.
consume ¶source
consume(length: int) -> None
Consume length buffered characters, advancing the byte offset past them.
expect ¶source
expect(token: str) -> None
Consume one expected structural character.
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If the next character is different. |
expect_end ¶source
Check that nothing but whitespace follows the document.
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If content follows the document. |
object_keys ¶source
Yield object keys, leaving each value for the caller to consume.
The caller must consume the entire value before requesting the next key. This allows nested objects to be traversed without decoding them all at once.
Yields:
| Type | Description |
|---|---|
str
|
Distinct string keys, with the cursor positioned after each colon. |
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If object syntax or keys are invalid. |
peek ¶source
peek() -> str
Skip whitespace and return the next character, or "" at the end of the document.
Returns:
| Type | Description |
|---|---|
str
|
The next non-whitespace character. |
read ¶source
Append up to size more bytes of the file to the buffer.
Returns:
| Type | Description |
|---|---|
bool
|
False at the end of the file. |
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If the bytes are not valid UTF-8. |
iter_object ¶source
Traverse a document that is one object, without retaining decoded values.
Yields:
| Type | Description |
|---|---|
tuple[str, Any, int, int]
|
Each key, its decoded value, and the value's UTF-8 byte offset and length. |
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If the document is not one object with distinct string keys, with the file and key in the message. |
read_entry ¶source
Reread one value at the byte range a traversal recorded.
Decodes with the traversal's own cursor, so a reread accepts exactly what the traversal did: strict UTF-8 without a byte order mark, and no duplicate fields. Errors name their byte in the file.
Nothing records the file's identity, so a change that leaves a valid value in the range goes unnoticed. Pin the file's content, by checksum say, where a stale reread would matter.
Returns:
| Type | Description |
|---|---|
Any
|
The decoded value. |
Raises:
| Type | Description |
|---|---|
TimeFFormatError
|
If the range cannot be read, ends early, or does not decode as one value with distinct fields, as happens when the file changed since the traversal. |
parquet ¶source
Read parquet source files row by row.
iter_parquet_rows ¶source
Yield one plain dict per row of a parquet file, decoding a batch at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The parquet file. |
required |
columns
|
Sequence[str] | None
|
The columns to read, which leaves out the others' decoding cost. All columns by default. |
None
|
Yields:
| Type | Description |
|---|---|
dict[str, Any]
|
Each row, keyed by column name, in file order. |
Raises:
| Type | Description |
|---|---|
TimeFFormatError
|
If |