-
Notifications
You must be signed in to change notification settings - Fork 0
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
✨ StreamParsing - Add streaming parsing capacity #12
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,11 @@ | ||
from typing import Any, Dict, List, Protocol | ||
|
||
|
||
class OnValidRowCallback(Protocol): | ||
def __call__(self, index: int, parsed_row: Dict[str, Any], raw_data: Any) -> None: | ||
... | ||
|
||
|
||
class OnInvalidRowCallback(Protocol): | ||
def __call__(self, errors_info: List[Dict[str, Any]], raw_data: Any) -> None: | ||
... |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import codecs | ||
from abc import ABC, abstractmethod | ||
import csv | ||
|
||
from magicparse import OnInvalidRowCallback, OnValidRowCallback | ||
from .fields import Field | ||
from io import BytesIO | ||
from typing import Any, Dict, List, Tuple, Union, Iterable | ||
|
@@ -21,6 +23,11 @@ def __init__(self, options: Dict[str, Any]) -> None: | |
def get_reader(self, stream: BytesIO) -> Iterable: | ||
pass | ||
|
||
def get_stream_readers(self, content: bytes) -> Tuple[Iterable, Iterable]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. J'ai pas trouvé mieux pour avoir le stream en double : en CSV, on ne peux pas acceder à la ligne lue dans le fichier (le reader ne la montre pas). |
||
schema_reader = self.get_reader(BytesIO(content)) | ||
raw_reader = BytesIO(content) | ||
return schema_reader, raw_reader | ||
|
||
@staticmethod | ||
def key() -> str: | ||
pass | ||
|
@@ -75,6 +82,44 @@ def parse(self, data: Union[bytes, BytesIO]) -> Tuple[List[dict], List[dict]]: | |
|
||
return result, errors | ||
|
||
def stream_parse( | ||
self, | ||
data: Union[bytes, BytesIO], | ||
on_valid_parsed_row: OnValidRowCallback, | ||
on_invalid_parsed_row: OnInvalidRowCallback, | ||
) -> None: | ||
if isinstance(data, BytesIO): | ||
data = data.read() | ||
|
||
reader, raw_stream = self.get_stream_readers(data) | ||
|
||
row_number = 0 | ||
if self.has_header: | ||
next(reader) | ||
next(raw_stream) | ||
row_number += 1 | ||
|
||
for row, raw_row in zip(reader, raw_stream): | ||
errors = [] | ||
row_is_valid = True | ||
item = {} | ||
for field in self.fields: | ||
try: | ||
value = field.read_value(row) | ||
except Exception as exc: | ||
errors.append({"row-number": row_number, **field.error(exc)}) | ||
row_is_valid = False | ||
continue | ||
|
||
item[field.key] = value | ||
|
||
if row_is_valid: | ||
on_valid_parsed_row(index=row_number, parsed_row=item, raw_data=raw_row) | ||
else: | ||
on_invalid_parsed_row(errors_info=errors, raw_data=raw_row) | ||
|
||
row_number += 1 | ||
|
||
|
||
class CsvSchema(Schema): | ||
def __init__(self, options: Dict[str, Any]) -> None: | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C'est quoi le besoin exact svp? @antoine-b-smartway @pewho @EwenBALOUIN
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Le but est d'agréger plusieurs fichiers d'import d'article en un seul. On n'a pas forcément besoin d'avoir la donnée parsée, on veut juste l'ean et la ligne brute originale pour la remettre dans le fichier de sortie.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Et pourquoi vous avez besoin de ça ? (désolé c'est vraiment pour comprendre 😄)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pour par exemple rejouer les X arti d'un mag dans l'ordre.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pourquoi avoir la version brute ? J'ai peut être raté un truc, mais si au final notre fichier va être "tagué/nommé" d'une manière "spécifique", finalement on sait que pour lui on n'a pas besoin d'appliquer de parser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c'est l'autre possiblité, mais ça veut dire que l'on fait un parser particulier pour celui ci. Je ne sais pas si c'est ce que l'on veut ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bas justement pas de parseur