-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d08c62
commit 2aabcc4
Showing
5 changed files
with
137 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
"""Singer tap for files in a directory.""" | ||
|
||
from __future__ import annotations | ||
|
||
import enum | ||
import functools | ||
import os | ||
from pathlib import Path | ||
|
||
import fsspec | ||
|
||
import singer_sdk.typing as th | ||
from samples.sample_tap_csv.client import CSVStream | ||
from singer_sdk import Tap | ||
from singer_sdk.contrib.filesystem.stream import SDC_META_FILEPATH | ||
|
||
DEFAULT_MERGE_STREAM_NAME = "files" | ||
|
||
|
||
def file_path_to_stream_name(file_path: str) -> str: | ||
"""Convert a file path to a stream name. | ||
- Get rid of any extensions | ||
- Preserve the full path, but replace slashes with double underscores | ||
Args: | ||
file_path: The file path to convert. | ||
Returns: | ||
The stream name. | ||
""" | ||
path_obj = Path(file_path) | ||
return path_obj.with_suffix("").as_posix().replace("/", "__") | ||
|
||
|
||
class ReadMode(str, enum.Enum): | ||
"""Sync mode for the tap.""" | ||
|
||
one_stream_per_file = "one_stream_per_file" | ||
merge = "merge" | ||
|
||
|
||
class FolderTap(Tap): | ||
"""Singer tap for files in a directory.""" | ||
|
||
valid_extensions: tuple[str, ...] | ||
|
||
config_jsonschema = th.PropertiesList( | ||
th.Property( | ||
"path", | ||
th.StringType, | ||
required=True, | ||
description="Path to CSV files.", | ||
), | ||
th.Property( | ||
"read_mode", | ||
th.StringType, | ||
required=True, | ||
description=( | ||
"Use `one_stream_per_file` to read each file as a separate stream, or " | ||
"`merge` to merge all files into a single stream." | ||
), | ||
allowed_values=[ | ||
ReadMode.one_stream_per_file, | ||
ReadMode.merge, | ||
], | ||
), | ||
th.Property( | ||
"stream_name", | ||
th.StringType, | ||
required=True, | ||
default=DEFAULT_MERGE_STREAM_NAME, | ||
description="Name of the stream to use when `read_mode` is `merge`.", | ||
), | ||
# TODO(edgarmondragon): Other configuration options. | ||
).to_dict() | ||
|
||
@functools.cached_property | ||
def read_mode(self) -> ReadMode: | ||
"""Folder read mode.""" | ||
return ReadMode(self.config["read_mode"]) | ||
|
||
def discover_streams(self) -> list: | ||
"""Return a list of discovered streams. | ||
Raises: | ||
ValueError: If the path does not exist or is not a directory. | ||
""" | ||
# TODO(edgarmondragon): Implement stream discovery, based on the configured path | ||
# and read mode. | ||
# A directory for now, but could be a glob pattern. | ||
path: str = self.config["path"] | ||
|
||
fs: fsspec.AbstractFileSystem = fsspec.filesystem("local") | ||
|
||
if not fs.exists(path) or not fs.isdir(path): | ||
# Raise a more specific error if the path is not a directory. | ||
msg = f"Path {path} does not exist or is not a directory" | ||
raise ValueError(msg) | ||
|
||
# One stream per file | ||
if self.read_mode == ReadMode.one_stream_per_file: | ||
return [ | ||
CSVStream( | ||
tap=self, | ||
name=file_path_to_stream_name(member), | ||
partitions=[{SDC_META_FILEPATH: os.path.join(path, member)}], # noqa: PTH118 | ||
) | ||
for member in os.listdir(path) | ||
if member.endswith(self.valid_extensions) | ||
] | ||
|
||
# Merge | ||
contexts = [ | ||
{ | ||
SDC_META_FILEPATH: os.path.join(path, member), # noqa: PTH118 | ||
} | ||
for member in os.listdir(path) | ||
if member.endswith(self.valid_extensions) | ||
] | ||
return [ | ||
CSVStream( | ||
tap=self, | ||
name=self.config["stream_name"], | ||
partitions=contexts, | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters