Skip to content

Commit

Permalink
Merge pull request #39 from rapIsKal/master
Browse files Browse the repository at this point in the history
#18 fix: __call__ now deprecated for parsers and performs as introduced parse_data method
  • Loading branch information
Arondit authored May 10, 2023
2 parents eda148a + 699f574 commit 3d5f437
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ from import_me.processors import StringProcessor, IntegerProcessor
>>> parser = XLSXParser(file_path=xlsx_filepath)
>>> parser()
>>> print(parser.has_errors) # False
>>> pprint(parser.cleaned_data)
>>> print(parser.cleaned_data)
[
{
'first_name': 'Ivan',
Expand Down
27 changes: 24 additions & 3 deletions import_me/parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,33 @@ def add_errors(
error.append(message)
self.errors.append(', '.join(error))

def __call__(self, raise_errors: bool = False, *args: Any, **kwargs: Any) -> None:
def parse_data(self, raise_errors: bool = False, *args: Any, **kwargs: Any) -> None:
try:
self.parse()
self._parse()
except Exception as e:
messages = getattr(e, 'messages', str(e))
self.add_errors(messages)

if raise_errors and self.has_errors:
raise ParserError(self.errors)

def __call__(self, raise_errors: bool = False, *args: Any, **kwargs: Any) -> None:
# for backward compatibility, deprecated
return self.parse_data(raise_errors, *args, **kwargs)

def _parse(self) -> None:
data = []

for row_index, row in self.iterate_file_rows():
try:
row_data = self.parse_row(row, row_index)
except SkipRow:
pass
else:
data.append(row_data)

self.cleaned_data = self.clean(data)


class BaseMultipleFileParser(ParserMixin):
parser_class: Type[BaseParser]
Expand All @@ -292,7 +309,7 @@ def add_errors(self, messages: Union[List, str], file_path: pathlib.Path) -> Non
for message in messages:
self.errors.append(f'{file_path}, {message}')

def __call__(self, raise_errors: bool = False, *args: Any, **kwargs: Any) -> None:
def parse_data(self, raise_errors: bool = False, *args: Any, **kwargs: Any) -> None:
for file_path in self.get_file_paths():
try:
parser = self.parser_class(file_path)
Expand All @@ -307,3 +324,7 @@ def __call__(self, raise_errors: bool = False, *args: Any, **kwargs: Any) -> Non

if raise_errors and self.has_errors:
raise ParserError(self.errors)

def __call__(self, raise_errors: bool = False, *args: Any, **kwargs: Any) -> None:
# for backward compatibility, deprecated
return self.parse_data(raise_errors, *args, **kwargs)
2 changes: 1 addition & 1 deletion import_me/parsers/xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def header_row_offset(self) -> Optional[int]:
index = self.first_data_row_index - 1
return index

def parse_data(self, raise_errors: bool = False) -> None:
def parse_data(self, raise_errors: bool = False, *args: Any, **kwargs: Any) -> None:
try:
self._parse()
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ flake8-class-attributes-order==0.1.0
flake8-broken-line==0.2.0
flake8-tidy-imports==4.1.0
flake8-typing-imports==1.9.0
safety==1.9.0
safety==2.3.5
dlint==0.10.3
flake8-if-statements==0.1.0
flake8-functions==0.0.4
Expand Down

0 comments on commit 3d5f437

Please sign in to comment.