Update parsing to support defaulting on some values

This commit is contained in:
Tim Van Baak 2021-02-10 22:57:45 -08:00
parent 74d090f7c2
commit 6bb188f970
1 changed files with 46 additions and 39 deletions

View File

@ -125,7 +125,7 @@ class DocumentTab:
""" """
A division of tags within a document. A division of tags within a document.
""" """
def __init__(self, tags: List[DocumentTag], options: TabOptions) -> None: def __init__(self, name: str, tags: List[DocumentTag], options: TabOptions) -> None:
self.tags: List[DocumentTag] = tags self.tags: List[DocumentTag] = tags
self.options: TabOptions = options self.options: TabOptions = options
@ -153,7 +153,9 @@ def load(fd: IO) -> Document:
""" """
Load a document from a file descriptor. Load a document from a file descriptor.
""" """
parsed_json: list = json.load(fd, object_pairs_hook=OrderedDict) parsed_json = json.load(fd, object_pairs_hook=OrderedDict)
if not isinstance(parsed_json, list):
raise ValueError('Parsing as document, expected list')
return parse_document_from_json(parsed_json) return parse_document_from_json(parsed_json)
@ -161,7 +163,9 @@ def loads(string: str) -> Document:
""" """
Load a document from a string. Load a document from a string.
""" """
parsed_json: list = json.loads(string, object_pairs_hook=OrderedDict) parsed_json = json.loads(string, object_pairs_hook=OrderedDict)
if not isinstance(parsed_json, list):
raise ValueError('Parsing as document, expected list')
return parse_document_from_json(parsed_json) return parse_document_from_json(parsed_json)
@ -172,8 +176,8 @@ def parse_document_from_json(parsed_json: list) -> Document:
# Parse tabs # Parse tabs
tabs: List[DocumentTab] = [] tabs: List[DocumentTab] = []
for tab_json in parsed_json: for tab_json in parsed_json:
if type(tab_json) is not dict: if not isinstance(tab_json, dict):
raise ValueError() raise ValueError(f'Parsing tab, expected dict but got {type(tab_json)}')
tabs.append(parse_tab_from_json(tab_json)) tabs.append(parse_tab_from_json(tab_json))
return Document(tabs) return Document(tabs)
@ -183,23 +187,27 @@ def parse_tab_from_json(tab_json: dict) -> DocumentTab:
""" """
Parses JSON into a DocumentTab object. Parses JSON into a DocumentTab object.
""" """
# Parse tab name
if 'name' not in tab_json:
raise ValueError('Expected tab name')
name: str = tab_json['name']
# Parse tab options # Parse tab options
if 'options' not in tab_json: options_json: dict = {}
raise ValueError() if 'options' in tab_json:
options_json: dict = tab_json['options'] options_json = tab_json['options']
options: TabOptions = TabOptions(**options_json) options: TabOptions = TabOptions(**options_json)
# Parse tags # Parse tags
if 'tags' not in tab_json:
raise ValueError()
tags_json: list = tab_json['tags']
tags: List[DocumentTag] = [] tags: List[DocumentTag] = []
for tag_json in tags_json: if 'tags' in tab_json:
if type(tag_json) is not dict: tags_json: list = tab_json['tags']
raise ValueError() for tag_json in tags_json:
tags.append(parse_tag_from_json(tag_json)) if not isinstance(tag_json, dict):
raise ValueError(f'Parsing tag, expected dict but got {type(tag_json)}')
tags.append(parse_tag_from_json(tag_json))
return DocumentTab(tags, options) return DocumentTab(name, tags, options)
def parse_tag_from_json(tag_json: dict) -> DocumentTag: def parse_tag_from_json(tag_json: dict) -> DocumentTag:
@ -208,29 +216,28 @@ def parse_tag_from_json(tag_json: dict) -> DocumentTag:
""" """
# Parse name # Parse name
if 'name' not in tag_json: if 'name' not in tag_json:
raise ValueError() raise ValueError('Expected tag name')
name: str = tag_json['name'] name: str = tag_json['name']
# Parse value # Parse value
if 'value' not in tag_json: value: str = ''
raise ValueError() if 'value' in tag_json:
value: str = tag_json['value'] value = tag_json['value']
# Parse tag options # Parse tag options
if 'options' not in tag_json: options_json: dict = {}
raise ValueError() if 'options' in tag_json:
options_json: dict = tag_json['options'] options_json = tag_json['options']
options: TagOptions = TagOptions(**options_json) options: TagOptions = TagOptions(**options_json)
# Parse subtags # Parse subtags
if 'subtags' not in tag_json:
raise ValueError()
subtags_json: list = tag_json['subtags']
subtags: List[DocumentSubtag] = [] subtags: List[DocumentSubtag] = []
for subtag_json in subtags_json: if 'subtags' in tag_json:
if type(subtag_json) is not dict: subtags_json: list = tag_json['subtags']
raise ValueError() for subtag_json in subtags_json:
subtags.append(parse_subtag_from_json(subtag_json)) if not isinstance(subtag_json, dict):
raise ValueError(f'Parsing subtag, expected dict but got {type(subtag_json)}')
subtags.append(parse_subtag_from_json(subtag_json))
return DocumentTag(name, value, options, subtags) return DocumentTag(name, value, options, subtags)
@ -241,18 +248,18 @@ def parse_subtag_from_json(subtag_json: dict) -> DocumentSubtag:
""" """
# Parse name # Parse name
if 'name' not in subtag_json: if 'name' not in subtag_json:
raise ValueError() raise ValueError('Expected subtag name')
name: str = subtag_json['name'] name: str = subtag_json['name']
# Parse value # Parse value
if 'value' not in subtag_json: value: str = ''
raise ValueError() if 'value' in subtag_json:
value: str = subtag_json['value'] value = subtag_json['value']
# Parse tag options # Parse tag options
if 'options' not in subtag_json: options_json: dict = {}
raise ValueError() if 'options' in subtag_json:
options_json: dict = subtag_json['options'] options_json = subtag_json['options']
options: TagOptions = TagOptions(**options_json) options: TagOptions = TagOptions(**options_json)
return DocumentSubtag(name, value, options) return DocumentSubtag(name, value, options)
@ -272,8 +279,8 @@ def check(files):
try: try:
load(f) load(f)
print(f'OK {file}') print(f'OK {file}')
except: except Exception as ex:
print(f'ERROR {file}') print(f'ERROR {file} - {ex}')
def main() -> Any: def main() -> Any: