def test_log_pass_write_XML(): log_pass = _log_pass() ostream = io.StringIO() xml_stream = XmlWrite.XmlStream(ostream) iflr_map = { RepCode.ObjectName(O=11, C=0, I=b'0B'): [], } TotalDepth.RP66V1.IndexXML.log_pass_to_XML(log_pass, iflr_map, xml_stream) # print(ostream.getvalue()) expected = """ <LogPass count="1"> <FrameArray C="0" I="0B" O="11" description="" x_axis="DEPT" x_units="0.1 in"> <Channels count="9"> <Channel C="0" I="DEPT" O="11" count="1" dimensions="1" long_name="MWD Tool Measurement Depth" rep_code="2" units="0.1 in"/> <Channel C="0" I="INC" O="11" count="1" dimensions="1" long_name="Inclination" rep_code="2" units="deg"/> <Channel C="0" I="AZI" O="11" count="1" dimensions="1" long_name="Azimuth" rep_code="2" units="deg"/> <Channel C="0" I="MTTVD" O="11" count="1" dimensions="1" long_name="MWD Tool Measurement TVD" rep_code="2" units="m"/> <Channel C="0" I="SECT" O="11" count="1" dimensions="1" long_name="Section" rep_code="2" units="m"/> <Channel C="0" I="RCN" O="11" count="1" dimensions="1" long_name="Rectangular Co-ordinates North" rep_code="2" units="m"/> <Channel C="0" I="RCE" O="11" count="1" dimensions="1" long_name="Rectangular Co-ordinates East" rep_code="2" units="m"/> <Channel C="0" I="DLSEV" O="11" count="1" dimensions="1" long_name="Dog-leg Severity" rep_code="2" units="deg/30m"/> <Channel C="0" I="TLTS" O="11" count="1" dimensions="1" long_name="Tool Temperature Static" rep_code="2" units="degC"/> </Channels> <IFLR count="0"> <FrameNumbers count="0" rle_len="0"/> <LRSH count="0" rle_len="0"/> <Xaxis count="0" rle_len="0"/> </IFLR> </FrameArray> </LogPass>""" assert ostream.getvalue() == expected
def test_08(self): """TestXmlWrite.test_08(): raise during write.""" myF = io.StringIO() try: with XmlWrite.XmlStream(myF) as xS: with XmlWrite.Element(xS, 'Root', {'version': '12.0'}): self.assertRaises(XmlWrite.ExceptionXmlEndElement, xS.endElement, 'NotRoot') with XmlWrite.Element(xS, 'E', {'attr_1': '1'}): xS._elemStk.pop() xS._elemStk.append('F') raise Exception('Some exception') except Exception as e: print(e) else: print('No exception raised') # print() # print(myF.getvalue()) self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <Root version="12.0"> <E attr_1="1"/> </Root> """)
def test_06(self): """TestXmlWrite.test_06(): encoded text in 'latin-1'.""" myF = io.StringIO() with XmlWrite.XmlStream(myF, 'latin-1') as xS: with XmlWrite.Element(xS, 'Root'): with XmlWrite.Element(xS, 'A'): xS.characters("""<&>"'""") with XmlWrite.Element(xS, 'A'): xS.characters('%s' % chr(147)) with XmlWrite.Element(xS, 'A'): xS.characters(chr(65)) with XmlWrite.Element(xS, 'A'): xS.characters(chr(128)) # print() # print(myF.getvalue()) self.assertEqual( """<?xml version='1.0' encoding="latin-1"?> <Root> <A><&>"'</A> <A>“</A> <A>A</A> <A>€</A> </Root> """, myF.getvalue(), )
def test_04(self): """TestXmlWrite.test_04(): raise on endElement when empty.""" myF = io.StringIO() with XmlWrite.XmlStream(myF) as xS: pass #print #print myF.getvalue() self.assertRaises(XmlWrite.ExceptionXmlEndElement, xS.endElement, '')
def test_00(self): """TestXmlWrite.test_00(): construction.""" myF = io.StringIO() with XmlWrite.XmlStream(myF): pass #print #print myF.getvalue() self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?>\n""")
def write_logical_file_sequence_to_xml(logical_index: LogicalFile.LogicalIndex, output_stream: typing.TextIO, private: bool) -> None: """Takes a LogicalIndex and writes the index to an XML stream.""" with XmlWrite.XmlStream(output_stream) as xml_stream: with XmlWrite.Element( xml_stream, 'RP66V1FileIndex', { 'path': logical_index.id, 'size': f'{os.path.getsize(logical_index.id):d}', 'schema_version': XML_SCHEMA_VERSION, 'utc_file_mtime': str( datetime.datetime.utcfromtimestamp( os.stat(logical_index.id).st_mtime)), 'utc_now': str(datetime.datetime.utcnow()), 'creator': f'{__name__}', }): with XmlWrite.Element( xml_stream, 'StorageUnitLabel', { 'sequence_number': str(logical_index.storage_unit_label. storage_unit_sequence_number), 'dlis_version': logical_index.storage_unit_label.dlis_version.decode( 'ascii'), 'storage_unit_structure': logical_index.storage_unit_label. storage_unit_structure.decode('ascii'), 'maximum_record_length': str(logical_index.storage_unit_label. maximum_record_length), 'storage_set_identifier': logical_index.storage_unit_label. storage_set_identifier.decode('ascii'), }): pass with XmlWrite.Element( xml_stream, 'LogicalFiles', {'count': f'{len(logical_index.logical_files):d}'}): for lf, logical_file in enumerate(logical_index.logical_files): write_logical_file_to_xml(lf, logical_file, xml_stream, private) # Visible records at the end rle_visible_records = Rle.create_rle( logical_index.visible_record_positions) xml_rle_write(rle_visible_records, 'VisibleRecords', xml_stream, hex_output=True)
def test_07(self): """TestXmlWrite.test_07(): comments.""" myF = io.StringIO() with XmlWrite.XmlStream(myF) as xS: with XmlWrite.Element(xS, 'Root', {'version': '12.0'}): xS.comment(' a comment ') #print #print myF.getvalue() self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <Root version="12.0"><!-- a comment --> </Root> """)
def test_03(self): """TestXmlWrite.test_03(): processing instruction.""" myF = io.StringIO() with XmlWrite.XmlStream(myF) as xS: with XmlWrite.Element(xS, 'Root', {'version': '12.0'}): with XmlWrite.Element(xS, 'A', {'attr_1': '1'}): xS.pI('Do <&> this') #print #print myF.getvalue() self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <Root version="12.0"> <A attr_1="1"><?Do <&> this?></A> </Root> """)
def test_02(self): """TestXmlWrite.test_02(): mixed content.""" myF = io.StringIO() with XmlWrite.XmlStream(myF) as xS: with XmlWrite.Element(xS, 'Root', {'version': '12.0'}): with XmlWrite.Element(xS, 'A', {'attr_1': '1'}): xS.characters('<&>') #print #print myF.getvalue() self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <Root version="12.0"> <A attr_1="1"><&></A> </Root> """)
def test_01(self): """TestXmlWrite.test_01(): simple elements.""" myF = io.StringIO() with XmlWrite.XmlStream(myF) as xS: with XmlWrite.Element(xS, 'Root', {'version': '12.0'}): with XmlWrite.Element(xS, 'A', {'attr_1': '1'}): pass #print #print myF.getvalue() self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <Root version="12.0"> <A attr_1="1"/> </Root> """)
def test_05(self): """TestXmlWrite.test_05(): raise on endElement missmatch.""" myF = io.StringIO() with XmlWrite.XmlStream(myF) as xS: with XmlWrite.Element(xS, 'Root', {'version': '12.0'}): self.assertRaises(XmlWrite.ExceptionXmlEndElement, xS.endElement, 'NotRoot') with XmlWrite.Element(xS, 'A', {'attr_1': '1'}): self.assertRaises(XmlWrite.ExceptionXmlEndElement, xS.endElement, 'NotA') #print #print myF.getvalue() self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <Root version="12.0"> <A attr_1="1"/> </Root> """)