def test_simple_use(self): xml_string = """ <?xml version="1.0" encoding="utf-8"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> </country> </data> """.strip() class Neighbor(Declared): name = var(str, as_xml_attr=True) direction = var(str, as_xml_attr=True) class Country(Declared): rank = var(str) year = var(int) gdppc = var(int) name = var(str, as_xml_attr=True) neighbor = var(Neighbor) Data = vec(Country, field_name="country") data = xml.unmarshal(Data, ET.XML(xml_string)) self.assertEqual( ET.tostring(xml.marshal(data, xml.Options(True))).decode(), '<data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc>' '<neighbor direction="E" name="Austria" /></country><country name="Singapore"><rank>4</rank>' '<year>2011</year><gdppc>59900</gdppc><neighbor direction="N" name="Malaysia" /></country>' '<country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc>' '<neighbor direction="W" name="Costa Rica" /></country></data>', ) v = json.Vec(data.vec) v.extend(data) self.assertEqual( json.marshal(v), '[{"rank": "1", "year": 2008, "gdppc": 141100, "name": "Liechtenstein", ' '"neighbor": {"name": "Austria", "direction": "E"}}, {"rank": "4", "year": 2011, ' '"gdppc": 59900, "name": "Singapore", "neighbor": {"name": "Malaysia", "direction": "N"}}, ' '{"rank": "68", "year": 2011, "gdppc": 13600, "name": "Panama", "neighbor": ' '{"name": "Costa Rica", "direction": "W"}}]', )
def test_unmarshal_xml_text_v1(): class Struct(Declared): __xml_tag_name__ = "root" p0 = var(str, as_xml_text=True) _str = '<root>string</root>' out = unmarshal(Struct, _str) assert out.p0 == "string" assert marshal(out, xml.Options(True)) == _str
def test_unmarshal_xml_skip_none_field(): class Struct(Declared): __xml_tag_name__ = "root" p0 = var(str, required=False) _str = '<root><p0>hello</p0></root>' out = unmarshal(Struct, _str) assert out.p0 == "hello" out.p0 = None assert marshal(out, xml.Options(True)) == "<root />"
def to_xml(self, skip_none_field=False, indent=None): # type: (bool, Optional[str]) -> ET.Element """ <?xml version="1.0"?> <tag id="`id`" style="`style`"> `text` </tag> """ node = xml.marshal(self, xml.Options(skip_none_field, indent)) if indent is not None: xml_prettify(node, indent, "\n") return node
def from_xml(cls: Type[_DT], element: ET.Element) -> _DT: """ >>> class Struct(Declared): >>> tag = var(str) >>> text = var(str) >>> children = var(str) >>> # attrs >>> id = var(str) >>> style = var(str) >>> ...... """ return xml.unmarshal(cls, element, xml.Options())
def test_unmarshal_xml_literal_v5(): class Struct(Declared): __xml_tag_name__ = "root" p0 = var(int, as_xml_attr=True, required=False) p1 = var(str) p2 = var(float) _str = '<root p0=""><p1>string</p1><p2>1.1</p2></root>' out = unmarshal(Struct, _str) assert out.p0 is None assert out.p1 == "string" assert out.p2 == 1.1 assert marshal(out, xml.Options(True)) == '<root><p1>string</p1><p2>1.1</p2></root>'
def test_other_simple_use(self): xml_string = """ <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> <style name="ratingBarStyle" parent="@android:style/Widget.RatingBar"> <item name="android:progressDrawable">@drawable/ratingbar_drawable</item> <item name="android:minHeight">48dip</item> <item name="android:maxHeight">48dip</item> </style> </resources> """.strip() class Item(Declared): name = var(str, as_xml_attr=True) text = var(str, as_xml_text=True) class Style(Declared): name = var(str, as_xml_attr=True) parent = var(str, as_xml_attr=True, required=False) items = vec(Item, field_name="item") Resource = vec(Style, field_name="style") data = xml.unmarshal(Resource, ET.XML(xml_string)) assert len(data) == 5 assert format_xml(ET.tostring(xml.marshal(data, xml.Options(True))).decode()) == format_xml(xml_string) v = json.Vec(data.vec) v.extend(data) assert ( json.marshal(v) == '[{"name": "AppTheme", "parent": "Theme.AppCompat.Light.DarkActionBar", "item": ' '[{"name": "colorPrimary", "text": "@color/colorPrimary"}, ' '{"name": "colorPrimaryDark", "text": "@color/colorPrimaryDark"}, ' '{"name": "colorAccent", "text": "@color/colorAccent"}]}, ' '{"name": "AppTheme.NoActionBar", "parent": null, "item": [' '{"name": "windowActionBar", "text": "false"}, {"name": "windowNoTitle", "text": "true"}]}, ' '{"name": "AppTheme.AppBarOverlay", "parent": "ThemeOverlay.AppCompat.Dark.ActionBar", "item": []}, ' '{"name": "AppTheme.PopupOverlay", "parent": "ThemeOverlay.AppCompat.Light", "item": []}, ' '{"name": "ratingBarStyle", "parent": "@android:style/Widget.RatingBar", "item": ' '[{"name": "android:progressDrawable", "text": "@drawable/ratingbar_drawable"}, ' '{"name": "android:minHeight", "text": "48dip"}, ' '{"name": "android:maxHeight", "text": "48dip"}]}]' ) assert ( json.marshal(v, json.Options(True)) == '[{"name": "AppTheme", "parent": "Theme.AppCompat.Light.DarkActionBar", "item": ' '[{"name": "colorPrimary", "text": "@color/colorPrimary"}, ' '{"name": "colorPrimaryDark", "text": "@color/colorPrimaryDark"}, ' '{"name": "colorAccent", "text": "@color/colorAccent"}]}, ' '{"name": "AppTheme.NoActionBar", "item": [{"name": "windowActionBar", "text": "false"}, ' '{"name": "windowNoTitle", "text": "true"}]}, ' '{"name": "AppTheme.AppBarOverlay", "parent": "ThemeOverlay.AppCompat.Dark.ActionBar", "item": []}, ' '{"name": "AppTheme.PopupOverlay", "parent": "ThemeOverlay.AppCompat.Light", "item": []}, ' '{"name": "ratingBarStyle", "parent": "@android:style/Widget.RatingBar", "item": ' '[{"name": "android:progressDrawable", "text": "@drawable/ratingbar_drawable"}, ' '{"name": "android:minHeight", "text": "48dip"}, {"name": "android:maxHeight", "text": "48dip"}]}]' )