-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add Composite Raw Decoder #179
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
Merged
artem1205
merged 27 commits into
main
from
artem1205/add-composite-decoder-with-parsers
Dec 27, 2024
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5b7724d
Composite Decoder: add Composite Decoder
artem1205 990584e
Composite Decoder: add Models
artem1205 50782f7
Composite Decoder: ref to use BufferedIOBase
artem1205 17add9e
Composite Decoder: remove inner_parser from parser definition
artem1205 655ce35
Composite Decoder: ref models
artem1205 513aa43
Composite Decoder: clean
artem1205 eada5bf
Composite Decoder: ref todo
artem1205 1984ef1
Composite Decoder: remove args & kwargs
artem1205 8d3f82a
Composite Decoder: fmt mypy
artem1205 f2c9b0a
Composite Decoder: add to model factory
artem1205 961356c
Composite Decoder: add to model factory
artem1205 5fa937c
Composite Raw Decoder: add unittest for parsers
artem1205 1b85c26
Composite Raw Decoder: fix CompositeRawDecoder creation
artem1205 a181608
Composite Raw Decoder: ref: CompositeRawDecoder & jsonlinedecoder
artem1205 5276ed1
Composite Raw Decoder: fmt
artem1205 adf9d3d
Composite Raw Decoder: fix mypy
artem1205 7604b99
Composite Raw Decoder: fix mypy
artem1205 bbd5e3a
Merge remote-tracking branch 'origin/main' into artem1205/add-composi…
artem1205 f9a97db
CDK: fix conflicts
artem1205 7fd73ad
CDK: add type for JsonLineParser
artem1205 693a82d
CDK: rename
artem1205 ca8f31e
CDK: run prettier
artem1205 46e80d3
CDK: ref
artem1205 61a3919
CDK: ref to csv.DictReader
artem1205 2ff8edf
CDK: fix mypy
artem1205 9641d4d
CDK: apply coderabbit suggestions
artem1205 2b6cfb5
CDK: fix
artem1205 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ name: Packaging and Publishing | |
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
- "v*" | ||
workflow_dispatch: | ||
inputs: | ||
version: | ||
|
This file contains hidden or 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 hidden or 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
97 changes: 97 additions & 0 deletions
97
airbyte_cdk/sources/declarative/decoders/composite_raw_decoder.py
This file contains hidden or 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,97 @@ | ||
import csv | ||
import gzip | ||
import json | ||
import logging | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from io import BufferedIOBase, TextIOWrapper | ||
from typing import Any, Generator, MutableMapping, Optional | ||
|
||
import requests | ||
|
||
from airbyte_cdk.sources.declarative.decoders.decoder import Decoder | ||
|
||
logger = logging.getLogger("airbyte") | ||
|
||
|
||
@dataclass | ||
class Parser(ABC): | ||
@abstractmethod | ||
def parse( | ||
self, | ||
data: BufferedIOBase, | ||
) -> Generator[MutableMapping[str, Any], None, None]: | ||
""" | ||
Parse data and yield dictionaries. | ||
""" | ||
pass | ||
|
||
|
||
@dataclass | ||
class GzipParser(Parser): | ||
inner_parser: Parser | ||
|
||
def parse( | ||
self, | ||
data: BufferedIOBase, | ||
) -> Generator[MutableMapping[str, Any], None, None]: | ||
""" | ||
Decompress gzipped bytes and pass decompressed data to the inner parser. | ||
""" | ||
with gzip.GzipFile(fileobj=data, mode="rb") as gzipobj: | ||
yield from self.inner_parser.parse(gzipobj) | ||
|
||
|
||
@dataclass | ||
class JsonLineParser(Parser): | ||
encoding: Optional[str] = "utf-8" | ||
|
||
def parse( | ||
self, | ||
data: BufferedIOBase, | ||
) -> Generator[MutableMapping[str, Any], None, None]: | ||
for line in data: | ||
try: | ||
yield json.loads(line.decode(encoding=self.encoding or "utf-8")) | ||
except json.JSONDecodeError as e: | ||
logger.warning(f"Cannot decode/parse line {line!r} as JSON, error: {e}") | ||
|
||
|
||
@dataclass | ||
class CsvParser(Parser): | ||
# TODO: migrate implementation to re-use file-base classes | ||
encoding: Optional[str] = "utf-8" | ||
delimiter: Optional[str] = "," | ||
|
||
def parse( | ||
self, | ||
data: BufferedIOBase, | ||
) -> Generator[MutableMapping[str, Any], None, None]: | ||
""" | ||
Parse CSV data from decompressed bytes. | ||
""" | ||
text_data = TextIOWrapper(data, encoding=self.encoding) # type: ignore | ||
reader = csv.DictReader(text_data, delimiter=self.delimiter or ",") | ||
yield from reader | ||
|
||
|
||
@dataclass | ||
class CompositeRawDecoder(Decoder): | ||
""" | ||
Decoder strategy to transform a requests.Response into a Generator[MutableMapping[str, Any], None, None] | ||
passed response.raw to parser(s). | ||
Note: response.raw is not decoded/decompressed by default. | ||
parsers should be instantiated recursively. | ||
Example: | ||
composite_raw_decoder = CompositeRawDecoder(parser=GzipParser(inner_parser=JsonLineParser(encoding="iso-8859-1"))) | ||
""" | ||
|
||
parser: Parser | ||
|
||
def is_stream_response(self) -> bool: | ||
return True | ||
|
||
def decode( | ||
self, response: requests.Response | ||
) -> Generator[MutableMapping[str, Any], None, None]: | ||
yield from self.parser.parse(data=response.raw) # type: ignore[arg-type] |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.