Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API - to validate XML, serialized and deserialized
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
import xmltodict | ||
from StringIO import StringIO | ||
from lxml import etree | ||
|
||
|
||
class Schema: | ||
|
||
def __init__(self, xsd_input=None): | ||
""" | ||
Parse the XSD and attribute | ||
""" | ||
if xsd_input: | ||
xmlschema_doc = etree.parse(StringIO(xsd_input)) | ||
self.xmlschema = etree.XMLSchema(xmlschema_doc) | ||
|
||
def validate(self, xml_input): | ||
""" | ||
Validate XML | ||
""" | ||
parsed_xml = etree.parse(StringIO(xml_input)) | ||
if self.xmlschema.validate(parsed_xml): | ||
return True | ||
else: | ||
return False | ||
|
||
def deserialized(self, xml_input, *args, **kwargs): | ||
""" | ||
Convert XML to dict object | ||
""" | ||
if self.validate(xml_input): | ||
return xmltodict.parse(xml_input, *args, **kwargs) | ||
|
||
def serialized(self, dict_input, **kwargs): | ||
""" | ||
Convert dict to XML | ||
""" | ||
return xmltodict.unparse(dict_input, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# coding: utf-8 | ||
|
||
import unittest | ||
import test_asserts | ||
from schema import Schema | ||
|
||
|
||
class SchemaTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
|
||
self.xsd_string = test_asserts.XSD | ||
self.xml_string = test_asserts.XML | ||
self.xml_string_invalid = test_asserts.XML_INVALID | ||
|
||
def test_instance(self): | ||
sch = Schema(self.xsd_string) | ||
self.assertIsInstance(sch, Schema) | ||
|
||
def test_validate(self): | ||
sch = Schema(self.xsd_string) | ||
validated = sch.validate(self.xml_string) | ||
self.assertTrue(validated) | ||
|
||
def test_deserialized(self): | ||
sch = Schema(self.xsd_string) | ||
des = sch.deserialized(self.xml_string) | ||
self.assertIsInstance(des, dict) | ||
self.assertIn('wizard', des) | ||
|
||
def test_serialized(self): | ||
sch = Schema() | ||
ser = sch.serialized(test_asserts.DICT_OBJ) | ||
self.assertEqual(ser, | ||
'<?xml version="1.0" encoding="utf-8"?>\n<a><c>2</c><b>1</b></a>') | ||
|
||
def test_invalid_xml(self): | ||
sch = Schema(self.xsd_string) | ||
invalid = sch.validate(self.xml_string_invalid) | ||
self.assertFalse(invalid) | ||
|
||
def test_deserialized_invalid_xml(self): | ||
sch = Schema(self.xsd_string) | ||
des = sch.deserialized(self.xml_string_invalid) | ||
self.assertNotIsInstance(des, dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# coding: utf-8 | ||
|
||
DICT_OBJ = dict({'a': {'b': '1', 'c': '2'}}) | ||
|
||
XSD = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<schema xmlns="http://www.w3.org/2001/XMLSchema" | ||
targetNamespace="http://www.example.org/wizard" | ||
xmlns:tns="http://www.example.org/wizard" | ||
elementFormDefault="qualified"> | ||
<complexType name="Wizard"> | ||
<sequence> | ||
<element name="startpage" type="IDREF" /> | ||
<element name="name" type="string" /> | ||
<element name="welcometext" type="string" /> | ||
<element name="choicepage" type="tns:ChoicePage" /> | ||
</sequence> | ||
</complexType> | ||
<complexType name="ChoicePage"> | ||
<sequence> | ||
<element name="title" type="string" /> | ||
</sequence> | ||
<attribute name="id" type="ID" /> | ||
</complexType> | ||
<element name="wizard" type="tns:Wizard" /> | ||
</schema>''' | ||
|
||
|
||
XML = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<wizard xmlns="http://www.example.org/wizard"> | ||
<startpage>start</startpage> | ||
<name>My Example Setup</name> | ||
<welcometext>Welcome to this little demo application.</welcometext> | ||
<choicepage id="start"> | ||
<title>Wizard Page One</title> | ||
</choicepage> | ||
</wizard>''' | ||
|
||
XML_INVALID = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<wizard> | ||
<startpage>start</startpage> | ||
<name>My Example Setup</name> | ||
<welcometext>Welcome to this little demo application.</welcometext> | ||
<choicepage id="start"> | ||
<title>Wizard Page One</title> | ||
</choicepage> | ||
</wizard>''' |