def quick_parse_structure(act_text: str, *, parse_block_amendments: bool = False) -> Act: lines = [] for l in act_text.split('\n'): parts = [] spaces_num = 1 bold = '<BOLD>' in l justified = '<NJ>' not in l l = l.replace("<BOLD>", " ") l = l.replace("<NJ>", " ") for char in l: if char == ' ': if spaces_num == 0: parts.append(IndentedLinePart(5, char, bold=bold)) spaces_num += 1 else: parts.append( IndentedLinePart(5 + spaces_num * 5, char, bold=bold)) spaces_num = 0 lines.append(IndentedLine(tuple(parts), 5 if justified else 40)) act = ActStructureParser.parse("2345 évi I. törvény", Date(2345, 6, 7), "A tesztelésről", lines) if parse_block_amendments: act = ActBlockAmendmentParser.parse(act) print( json.dumps(dict2object.to_dict(act, Act), indent=' ', ensure_ascii=False)) return act
def serialize_to_json_file(act: Any, f: TextIO) -> Any: json.dump( dict2object.to_dict(act, type(act)), f, indent=' ', ensure_ascii=False, )
def test_as_field(cls: Type, obj: Any, dct: Any) -> None: @attr.s(slots=True, frozen=True, auto_attribs=True) class Tester: field: cls # type: ignore obj = Tester(obj) dct = {'field': dct} assert to_dict(obj, Tester) == dct assert to_object(dct, Tester) == obj
def test_structure_parsing_exact(text: str, expected_structure: Any) -> None: resulting_structure = quick_parse_structure(text) result_as_dict = dict2object.to_dict(resulting_structure, type(resulting_structure)) json.dump(result_as_dict, sys.stdout, indent=' ', ensure_ascii=False, sort_keys=True) assert result_as_dict == expected_structure
def test_obj_to_dict_can_handle_specials() -> None: # This also used to test for Type fields, but we no longer have those. # But the dict2object unittests do, so it's properly tested. # This test will remain as a real-world test though. test_data = BlockAmendment(position=StructuralReference( "Btk.", special=SubtitleArticleCombo( SubtitleArticleComboType.BEFORE_WITH_ARTICLE, "123")), ) the_dict = dict2object.to_dict(test_data, BlockAmendment) # This should not throw the_json = json.dumps(the_dict) reconstructed_dict = json.loads(the_json) reconstructed_data = dict2object.to_object(reconstructed_dict, BlockAmendment) assert reconstructed_data == test_data
def test_optional(cls: Type, obj: Any, dct: Any) -> None: assert to_dict(obj, Optional[cls]) == dct assert to_object(dct, Optional[cls]) == obj assert to_dict(None, Optional[cls]) is None assert to_object(None, Optional[cls]) is None
def test_to_dict(cls: Type, obj: Any, dct: Any) -> None: assert to_dict(obj, cls) == dct