-
Notifications
You must be signed in to change notification settings - Fork 146
[Issue 305] add new json parser #366
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
Changes from 1 commit
296816b
cd095e2
f2f91fd
190cd5a
2834000
6297673
4741a43
080d848
5826922
e6332cb
1f6d5b6
fc980b1
3fe3e11
0be1780
c5b8d3c
03cce38
2dcd125
50c3038
562f288
a722036
c8851d8
347051a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…with exception handling Signed-off-by: Meret Behrens <meret.behrens@tngtech.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
from datetime import datetime | ||
from typing import Any, Callable, Dict | ||
|
||
from src.model.external_document_ref import ExternalDocumentRef | ||
from src.model.typing.constructor_type_errors import ConstructorTypeErrors | ||
from src.parser.error import SPDXParsingError | ||
from src.parser.logger import Logger | ||
|
||
|
||
def parse_optional_field(field: Any, method_to_parse:Callable=lambda x: x, default=None): | ||
def parse_optional_field(field: Any, method_to_parse: Callable = lambda x: x, default=None): | ||
if not field: | ||
return default | ||
return method_to_parse(field) | ||
|
||
|
||
def datetime_from_str(created: str) -> datetime: | ||
date = datetime.strptime(created, "%Y-%m-%dT%H:%M:%SZ") | ||
def datetime_from_str(date_str: str) -> datetime: | ||
if not isinstance(date_str, str): | ||
raise SPDXParsingError([f"Could not convert str to datetime, invalid type: {type(date_str).__name__}"]) | ||
date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ") | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return date | ||
|
||
|
||
def transform_json_str_to_enum_name(json_str: str) -> str: | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return json_str.replace("-","_").upper() | ||
return json_str.replace("-", "_").upper() | ||
|
||
|
||
def try_construction_raise_parsing_error(object_to_construct: Any, args_for_construction: Dict) -> Any: | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -27,3 +29,21 @@ def try_construction_raise_parsing_error(object_to_construct: Any, args_for_cons | |
except ConstructorTypeErrors as err: | ||
raise SPDXParsingError([f"Error while constructing {object_to_construct.__name__}: {err.get_messages()}"]) | ||
return constructed_object | ||
|
||
|
||
def try_parse_optional_field_append_logger_when_failing(logger: Logger, field: Any, method_to_parse: Callable, default=None): | ||
try: | ||
parsed_element = parse_optional_field(field=field, method_to_parse=method_to_parse, default=default) | ||
except SPDXParsingError as err: | ||
logger.append_all(err.get_messages()) | ||
parsed_element = default | ||
return parsed_element | ||
|
||
def try_parse_required_field_append_logger_when_failing(logger: Logger, field: Any, method_to_parse: Callable, default=None): | ||
try: | ||
parsed_element = method_to_parse(field) | ||
except SPDXParsingError as err: | ||
logger.append_all(err.get_messages()) | ||
parsed_element = default | ||
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. Won't we get another error down the line when using the 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. No, before calling any constructor we are checking if the logger has any messages and raise a |
||
return parsed_element | ||
|
Uh oh!
There was an error while loading. Please reload this page.