class TestSlashFromRootDict(_ProcessorTestCase): """Process with a slash starting from the root element""" xml_string = strip_xml(""" <location> <city> <name>Kansas City</name> <coordinates> <lat>39.0997</lat> <lon>94.5786</lon> </coordinates> </city> </location> """) value = { 'name': 'Kansas City', 'lat': 39.0997, 'lon': 94.5786, } processor = xml.dictionary('location/city', [ xml.string('name'), xml.floating_point('coordinates/lat', alias='lat'), xml.floating_point('coordinates/lon', alias='lon'), ])
def __init__(self): self.processor = xml.user_object( "annotation", Annotation, [ xml.user_object("size", Size, [ xml.integer("width"), xml.integer("height"), ]), xml.array( xml.user_object( "object", Object, [ xml.string("name"), xml.user_object( "bndbox", Box, [ xml.floating_point("xmin"), xml.floating_point("ymin"), xml.floating_point("xmax"), xml.floating_point("ymax"), ], alias="box" ) ] ), alias="objects" ), xml.string("filename") ] )
class TestNestedSlash(_ProcessorTestCase): """Process with nested slash specifiers""" xml_string = strip_xml(""" <root> <place> <city> <name>Kansas City</name> <location> <coordinates> <lat>39.0997</lat> <lon>94.5786</lon> </coordinates> </location> </city> </place> </root> """) value = { 'name': 'Kansas City', 'lat': 39.0997, 'lon': 94.5786, } processor = xml.dictionary('root', [ xml.string('place/city/name', alias='name'), xml.floating_point('place/city/location/coordinates/lat', alias='lat'), xml.floating_point('place/city/location/coordinates/lon', alias='lon') ])
def test_serialize_to_file(tmpdir): """Serialize XML data to a file""" value = { 'boolean': True, 'float': 3.14, 'int': 1, 'string': 'Hello, World' } processor = xml.dictionary('root', [ xml.boolean('boolean'), xml.floating_point('float'), xml.integer('int'), xml.string('string'), ]) expected = strip_xml(""" <root> <boolean>True</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """) xml_file_name = 'data.xml' xml_file_path = os.path.join(tmpdir.strpath, xml_file_name) xml.serialize_to_file(processor, value, xml_file_path) # Ensure the file contents match what is expected. xml_file = tmpdir.join(xml_file_name) actual = xml_file.read() assert expected == actual
def test_primitive_values_serialize(): """Serializes primitive values""" value = { 'boolean': True, 'float': 3.14, 'int': 1, 'string': 'Hello, World' } processor = xml.dictionary('root', [ xml.boolean('boolean'), xml.floating_point('float'), xml.integer('int'), xml.string('string'), ]) expected = strip_xml(""" <root> <boolean>True</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_primitive_default_present(): """Parses primitive values with defaults specified""" xml_string = """ <root> <boolean>false</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """ processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, default=True), xml.floating_point('float', required=False, default=0.0), xml.integer('int', required=False, default=0), xml.string('string', required=False, default=''), ]) expected = { 'boolean': False, 'float': 3.14, 'int': 1, 'string': 'Hello, World', } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_parse_primitive_aliased(): """Parses primitive values with aliases""" xml_string = """ <root> <boolean>true</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """ processor = xml.dictionary('root', [ xml.boolean('boolean', alias='b'), xml.floating_point('float', alias='f'), xml.integer('int', alias='i'), xml.string('string', alias='s'), ]) expected = { 'b': True, 'f': 3.14, 'i': 1, 's': 'Hello, World', } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_array_serialize_nested(): """Tests serializing nested arrays""" value = { 'date': '3-21', 'data-points': [ 21.1, 1897.17, 13.1, ] } processor = xml.dictionary('root', [ xml.string('date'), xml.array(xml.floating_point('value'), nested='data-points') ]) expected = strip_xml(""" <root> <date>3-21</date> <data-points> <value>21.1</value> <value>1897.17</value> <value>13.1</value> </data-points> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_serialize_none_object_issue_7(): """Attempts to serialize an object value that is None""" class Athlete(object): def __init__(self): self.name = '' self.age = 0 processor = xml.dictionary('race-result', [ xml.floating_point('time'), xml.user_object('athlete', Athlete, [ xml.string('name'), xml.integer('age'), ], required=False) ]) value = { 'time': 87.5, 'athlete': None, } expected_xml = strip_xml(""" <race-result> <time>87.5</time> </race-result> """) actual_xml = xml.serialize_to_string(processor, value) assert expected_xml == actual_xml
def test_serialize_none_namedtuple_issue_7(): """Tests attempting to serialize a named tuple value that is None""" Athlete = namedtuple('Athlete', [ 'name', 'age', ]) processor = xml.dictionary('race-result', [ xml.floating_point('time'), xml.named_tuple('athlete', Athlete, [ xml.string('name'), xml.integer('age'), ], required=False) ]) value = { 'time': 87.5, 'athlete': None, } expected_xml = strip_xml(""" <race-result> <time>87.5</time> </race-result> """) actual_xml = xml.serialize_to_string(processor, value) assert expected_xml == actual_xml
def test_array_serialize_root_not_nested(): """Serialize an array that is the root""" value = [3.14, 13.7, 6.22] processor = xml.array(xml.floating_point('constant')) with pytest.raises(xml.InvalidRootProcessor): xml.serialize_to_string(processor, value)
def insert_xml(): print('Inserting XML data') transaction_processor = xml.dictionary('transaction', [ xml.string('.', attribute='date'), xml.integer('.', attribute='reseller-id'), xml.integer('transactionId'), xml.string('productName'), xml.integer('qty'), xml.floating_point('totalAmount'), xml.string('salesChannel'), xml.dictionary('customer',[xml.string('firstname'), xml.string('lastname'), xml.string('email')]), xml.string('dateCreated'), xml.string('seriesCity') ]) for resellerid in XML_RESELLERS: tran_id = 0 export = generate_xml(resellerid) for day in ALL_DAYS: data = [tran for tran in export if tran['Created Date']== day] for entry in data: entry['transactionId'] = tran_id tran_id += 1 result = [] xml_header = None result.append('<?xml version="1.0" encoding="utf-8"?>') result.append('<transactions>') for tran in data: xml_str = xml.serialize_to_string(transaction_processor, tran, indent=' ') splitted =xml_str.split('\n') result += splitted[1:] result.append('</transactions>') date_nameformat = day.split('-') new_format = date_nameformat[0] + date_nameformat[2] + date_nameformat[1] with open(f"/home/generator/xml/rawDailySales_{new_format}_{resellerid}.xml", 'w') as output_file: output_file.write('\n'.join(result))
class TestMixed(_ProcessorTestCase): """Process with a mix of specifiers""" xml_string = strip_xml(""" <location name="Kansas City"> <coordinates> <lat>39.0997</lat> <lon>94.5786</lon> </coordinates> </location> """) value = { 'name': 'Kansas City', 'lat': 39.0997, 'lon': 94.5786, } processor = xml.dictionary('location', [ xml.string('.', attribute='name'), xml.floating_point('coordinates/lat', alias='lat'), xml.floating_point('coordinates/lon', alias='lon') ])
def test_parse_float_invalid(): """Parse an invalid float value""" xml_string = """ <root> <value>hello</value> </root> """ processor = xml.dictionary('root', [ xml.floating_point('value'), ]) with pytest.raises(xml.InvalidPrimitiveValue): xml.parse_from_string(processor, xml_string)
class TestSlash(_ProcessorTestCase): """Process with slash specifier""" xml_string = strip_xml(""" <city> <name>Kansas City</name> <coordinates> <lat>39.0997</lat> <lon>94.5786</lon> </coordinates> </city> """) value = { 'name': 'Kansas City', 'lat': 39.0997, 'lon': 94.5786, } processor = xml.dictionary('city', [ xml.string('name'), xml.floating_point('coordinates/lat', alias='lat'), xml.floating_point('coordinates/lon', alias='lon') ])
def test_array_serialize_root(): """Serialize an array that is the root""" value = [3.14, 13.7, 6.22] processor = xml.array(xml.floating_point('constant'), nested='constants') expected = strip_xml(""" <constants> <constant>3.14</constant> <constant>13.7</constant> <constant>6.22</constant> </constants> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_primitive_values_serialize_falsey_omitted(): """Serialize false primitive values""" value = {'boolean': False, 'float': 0.0, 'int': 0, 'string': ''} processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, omit_empty=True), xml.floating_point('float', required=False, omit_empty=True), xml.integer('int', required=False, omit_empty=True), xml.string('string', required=False, omit_empty=True), ]) expected = strip_xml(""" <root /> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_attribute_serialize_aliased(): """Serialize an aliased attribute""" value = { 'pi': 3.14, } processor = xml.dictionary('root', [ xml.floating_point('constant', attribute='value', alias='pi'), ]) expected = strip_xml(""" <root> <constant value="3.14" /> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_array_serialize_shared_element(): """Serialize an array on an element shared with an attribute""" value = {'units': 'grams', 'results': [32.4, 3.11]} processor = xml.dictionary('root', [ xml.string('results', attribute='units'), xml.array(xml.floating_point('value'), nested='results') ]) expected = strip_xml(""" <root> <results units="grams"> <value>32.4</value> <value>3.11</value> </results> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_floating_point_transform(): """Transform floating point values""" xml_string = strip_xml(""" <data> <value>13.1</value> </data> """) value = {'value': 26.2} def _after_parse(_, x): return x * 2.0 def _before_serialize(_, x): return x / 2.0 hooks = xml.Hooks(after_parse=_after_parse, before_serialize=_before_serialize) processor = xml.dictionary('data', [xml.floating_point('value', hooks=hooks)]) _transform_test_case_run(processor, value, xml_string)
def test_primitive_values_serialize_falsey(): """Serialize false primitive values""" value = {'boolean': False, 'float': 0.0, 'int': 0, 'string': ''} processor = xml.dictionary('root', [ xml.boolean('boolean', required=False), xml.floating_point('float', required=False), xml.integer('int', required=False), xml.string('string', required=False), ]) expected = strip_xml(""" <root> <boolean>False</boolean> <float>0.0</float> <int>0</int> <string /> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_primitive_values_serialize_none_default(): """Serialize primitive values where the default for the value is None""" value = {} processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, default=None), xml.floating_point('float', required=False, default=None), xml.integer('int', required=False, default=None), xml.string('string', required=False, default=None), ], required=False) expected = strip_xml(""" <root> <boolean /> <float /> <int /> <string /> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_primitive_default(): """Parses primitive values with defaults specified""" xml_string = """ <root> </root> """ processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, default=False), xml.floating_point('float', required=False, default=0.0), xml.integer('int', required=False, default=0), xml.string('string', required=False, default=''), ]) expected = { 'boolean': False, 'float': 0.0, 'int': 0, 'string': '', } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_parse_namespace(): """Parses an xml document with a namespace""" xml_string = """ <root xmlns="http://www.w3.org/1999/xhtml"> <message>Hello</message> <values> <int>321</int> <float>3.14</float> </values> </root> """ processor = xml.dictionary('root', [ xml.string('message'), xml.dictionary('values', [xml.integer('int'), xml.floating_point('float')]) ]) expected = {'message': 'Hello', 'values': {'int': 321, 'float': 3.14}} actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def __init__(self, group_name="Imported", group_desc=""): self.group_name = group_name self.group_desc = group_desc self.scenario_group = None def dict_to_scenario(state, value): session = Session() value['id'] = Scenario.scenario_hash(value['acq_time'], value['scen_materials'], value['scen_bckg_materials'], value['influences']) q = session.query(Scenario).filter_by(id=value['id']).first() if q: if self.scenario_group: self.scenario_group.scenarios.append(q) return q else: scenario = Scenario(value['acq_time'], value['replication'], value['scen_materials'], value['scen_bckg_materials'], value['influences'], [self.scenario_group]) return scenario def scenario_to_dict(state, value): return value.__dict__ def material_validate(state, value): session = Session() q = session.query(Material).filter_by(name=value.name).first() if q: return q else: session.add(value) return value def trace(state, value): print('Got {} at {}'.format(value, state)) print(value.__dict__) return value def influence_validate(state, value): session = Session() q = session.query(Influence).filter_by(name=value.name).first() if q: return q else: session.add(value) return value self.scenario_hooks = xml.Hooks( after_parse=dict_to_scenario, before_serialize=scenario_to_dict, ) self.material_hooks = xml.Hooks( after_parse=material_validate, before_serialize=lambda _, x: x, ) self.influence_hooks = xml.Hooks( after_parse=influence_validate, before_serialize=lambda _, x: x, ) self.material_processor = xml.user_object( 'Material', Material, [xml.string('BaseMaterialName', alias='name')], alias='material', hooks=self.material_hooks) self.sourcematerials_processor = xml.user_object( 'ScenarioMaterial', ScenarioMaterial, [ self.material_processor, xml.string('fd_mode'), xml.floating_point('dose'), ], alias='scen_materials') self.bkgmaterial_processor = xml.user_object( 'ScenarioBackgroundMaterial', ScenarioBackgroundMaterial, [ self.material_processor, xml.string('fd_mode'), xml.floating_point('dose'), ], alias='scen_bckg_materials', required=False) self.influence_processor = xml.user_object( 'Influence', Influence, [xml.string('InfluenceName', alias='name')], alias='influences', hooks=self.influence_hooks, required=False) self.scenario_processor = xml.dictionary('Scenario', [ xml.string("id", required=False), xml.floating_point("acq_time"), xml.integer("replication"), xml.array(self.sourcematerials_processor), xml.array(self.bkgmaterial_processor), xml.array(self.influence_processor) ], hooks=self.scenario_hooks) self.scenarios_processor = xml.dictionary( 'Scenarios', [xml.array(self.scenario_processor)])
], required=False), declxml.dictionary('createdin', [ declxml.string('.', attribute='version') ], required=False), declxml.dictionary('guiMode', [ declxml.string('.', attribute='mode') ], required=False), declxml.dictionary('dataset', [ declxml.string('.', attribute='path'), declxml.integer('.', attribute='overlay') ], required=False), ], required=False) node_processor = declxml.dictionary('node', [ declxml.integer('.', attribute='id'), declxml.floating_point('.', attribute='radius', required=False, default=1.0), declxml.integer('.', attribute='x', required=False), declxml.integer('.', attribute='y', required=False), declxml.integer('.', attribute='z', required=False), declxml.integer('.', attribute='inVp', required=False), declxml.integer('.', attribute='inMag', required=False), declxml.integer('.', attribute='time', required=False), declxml.string('.', attribute='comment', required=False) ], required=False) pyknossos_node_processor = declxml.dictionary('node', [ declxml.integer('.', attribute='id'), declxml.floating_point('.', attribute='radius', required=False, default=1.0), declxml.floating_point('.', attribute='x', required=False), declxml.floating_point('.', attribute='y', required=False), declxml.floating_point('.', attribute='z', required=False),