Skip to content

Commit

Permalink
Make the Singer writer and reader classes generic
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Jul 13, 2024
1 parent 3f29a1f commit ff38602
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion singer_sdk/_singerlib/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _default_encoding(obj: t.Any) -> str: # noqa: ANN401
return obj.isoformat(sep="T") if isinstance(obj, datetime.datetime) else str(obj)


def deserialize_json(json_str: str, **kwargs: t.Any) -> dict:
def deserialize_json(json_str: str | bytes, **kwargs: t.Any) -> dict:
"""Deserialize a line of json.
Args:
Expand Down
94 changes: 53 additions & 41 deletions singer_sdk/io_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,30 @@

from singer_sdk._singerlib.messages import Message, SingerMessageType
from singer_sdk._singerlib.messages import format_message as singer_format_message
from singer_sdk._singerlib.messages import write_message as singer_write_message
from singer_sdk._singerlib.serde import deserialize_json
from singer_sdk.exceptions import InvalidInputLine

logger = logging.getLogger(__name__)

# TODO: Use to default to 'str' here
# https://peps.python.org/pep-0696/
T = t.TypeVar("T", str, bytes)

class SingerReader(metaclass=abc.ABCMeta):
"""Interface for all plugins reading Singer messages from stdin."""

class GenericSingerReader(t.Generic[T], metaclass=abc.ABCMeta):
"""Interface for all plugins reading Singer messages as strings or bytes."""

@t.final
def listen(self, file_input: t.IO[str] | None = None) -> None:
def listen(self, file_input: t.IO[T] | None = None) -> None:
"""Read from input until all messages are processed.
Args:
file_input: Readable stream of messages. Defaults to standard in.
This method is internal to the SDK and should not need to be overridden.
"""
if not file_input:
file_input = sys.stdin

self._process_lines(file_input)
self._process_lines(file_input or self.default_input)
self._process_endofpipe()

@staticmethod
def _assert_line_requires(line_dict: dict, requires: set[str]) -> None:
"""Check if dictionary .
Args:
line_dict: TODO
requires: TODO
Raises:
InvalidInputLine: raised if any required keys are missing
"""
if not requires.issubset(line_dict):
missing = requires - set(line_dict)
msg = f"Line is missing required {', '.join(missing)} key(s): {line_dict}"
raise InvalidInputLine(msg)

def deserialize_json(self, line: str) -> dict: # noqa: PLR6301
"""Deserialize a line of json.
Args:
line: A single line of json.
Returns:
A dictionary of the deserialized json.
"""
return deserialize_json(line)

def _process_lines(self, file_input: t.IO[str]) -> t.Counter[str]:
def _process_lines(self, file_input: t.IO[T]) -> t.Counter[str]:
"""Internal method to process jsonl lines from a Singer tap.
Args:
Expand Down Expand Up @@ -99,6 +70,29 @@ def _process_lines(self, file_input: t.IO[str]) -> t.Counter[str]:

return Counter(**stats)

@property
@abc.abstractmethod
def default_input(self) -> t.IO[T]: ... # noqa: D102

@staticmethod
def _assert_line_requires(line_dict: dict, requires: set[str]) -> None:
"""Check if dictionary .
Args:
line_dict: TODO
requires: TODO
Raises:
InvalidInputLine: raised if any required keys are missing
"""
if not requires.issubset(line_dict):
missing = requires - set(line_dict)
msg = f"Line is missing required {', '.join(missing)} key(s): {line_dict}"
raise InvalidInputLine(msg)

@abc.abstractmethod
def deserialize_json(self, line: T) -> dict: ... # noqa: D102

@abc.abstractmethod
def _process_schema_message(self, message_dict: dict) -> None: ...

Expand Down Expand Up @@ -131,8 +125,25 @@ def _process_endofpipe(self) -> None: # noqa: PLR6301
logger.debug("End of pipe reached")


class SingerReader(GenericSingerReader[str]):
"""Base class for all plugins reading Singer messages as strings from stdin."""

default_input = sys.stdin

def deserialize_json(self, line: str) -> dict: # noqa: PLR6301
"""Deserialize a line of json.
Args:
line: A single line of json.
Returns:
A dictionary of the deserialized json.
"""
return deserialize_json(line)


class SingerWriter:
"""Interface for all plugins writting Singer messages to stdout."""
"""Interface for all plugins writing Singer messages to stdout."""

def format_message(self, message: Message) -> str: # noqa: PLR6301
"""Format a message as a JSON string.
Expand All @@ -145,10 +156,11 @@ def format_message(self, message: Message) -> str: # noqa: PLR6301
"""
return singer_format_message(message)

def write_message(self, message: Message) -> None: # noqa: PLR6301
def write_message(self, message: Message) -> None:
"""Write a message to stdout.
Args:
message: The message to write.
"""
singer_write_message(message)
sys.stdout.write(self.format_message(message) + "\n")
sys.stdout.flush()

0 comments on commit ff38602

Please sign in to comment.