cdm_data_loaders.utils.file_items ¶
Helpers for treating dlt FileItemDict objects as the unit of pipeline input.
Readers accept a local path (str or Path) or a FileItemDict emitted by the dlt filesystem
resource. FileItemDict works for any fsspec protocol (file, s3, memory, ...), so readers that
only ever call FileItemDict.open work the same way for local and remote files.
FileItemDict.open (with dlt's default compression="auto") decompresses gzip when the listing marked the
file as gzip-encoded. dlt sets that from the file name (*.gz, *.tgz) for every protocol, so no
repository-specific gzip handling is needed. Build FileItemDict objects with dlt's glob_files (as
as_file_item does) rather than by hand: a hand-built dict has no encoding key and will not be decompressed.
FileSource
module-attribute
¶
A local file path or a dlt file item.
as_file_item ¶
Return file as a FileItemDict.
A FileItemDict is returned unchanged. A local path is listed with dlt's glob_files, so the result
carries the same fields (including encoding) as items emitted by the dlt filesystem resource. Its
relative_path is the file name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
FileSource
|
local path or dlt file item |
required |
Returns:
| Type | Description |
|---|---|
FileItemDict
|
file item for |
Raises:
| Type | Description |
|---|---|
IsADirectoryError
|
if |
FileNotFoundError
|
if |
Source code in src/cdm_data_loaders/utils/file_items.py
join_url ¶
Join path parts onto a bucket URL (s3://bucket/prefix, file:///dir, ...) or a local path.
URLs are joined with /; local paths with the operating system's separator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_url
|
str
|
bucket URL or local directory path |
required |
parts
|
str
|
path parts to append |
()
|
Returns:
| Type | Description |
|---|---|
str
|
joined URL or path |
Source code in src/cdm_data_loaders/utils/file_items.py
source_id ¶
Return the identifier recorded for file in output tables.
This is the file's path relative to the listed bucket or directory (relative_path), which does not
depend on where the input lives. For a local path, it is the file name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
FileSource
|
local path or dlt file item |
required |
Returns:
| Type | Description |
|---|---|
str
|
source identifier |
Source code in src/cdm_data_loaders/utils/file_items.py
source_url ¶
Return a human-readable location for file, for log messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
FileSource | IO[bytes]
|
local path, dlt file item, or open handle |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in src/cdm_data_loaders/utils/file_items.py
open_binary ¶
Open file for binary reading, decompressing gzip files.
An already-open binary handle is yielded as-is and is not closed on exit, so callers can share one handle
between several readers (e.g. read the XML head matter, seek(0), then stream the file).
:yield: open binary handle
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
FileSource | IO[bytes]
|
local path, dlt file item, or open binary handle |
required |
Source code in src/cdm_data_loaders/utils/file_items.py
is_local ¶
Return True if file_item is on the local filesystem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_item
|
FileItemDict
|
dlt file item |
required |
Returns:
| Type | Description |
|---|---|
bool
|
whether the item's fsspec filesystem is the local filesystem |
Source code in src/cdm_data_loaders/utils/file_items.py
local_copy ¶
Yield a local path for file_item, downloading it first if it is not on the local filesystem.
Local files are not copied: their native path is yielded. Remote files are downloaded, as stored (not
decompressed), to dest_dir / relative_path and deleted on exit. Keeping the relative path means files
with the same name in different directories do not overwrite each other.
:yield: local path to the file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_item
|
FileItemDict
|
dlt file item |
required |
dest_dir
|
Path
|
directory to download remote files into |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the item's relative path would place the download outside |