def print_nitf(file_name, dest=sys.stdout): """ Worker function to dump the NITF header and various subheader details to the provided destination. Parameters ---------- file_name : str|BinaryIO dest : TextIO """ # Configure print function for desired destination # - e.g., stdout, string buffer, file global print_func print_func = functools.partial(print, file=dest) details = NITFDetails(file_name) if isinstance(file_name, string_types): print_func('') print_func('Details for file {}'.format(file_name)) print_func('') print_func('----- File Header -----') _print_file_header(details.nitf_header) print_func('') if details.img_subheader_offsets is not None: for img_subhead_num in range(details.img_subheader_offsets.size): print_func('----- Image {} -----'.format(img_subhead_num)) hdr = details.parse_image_subheader(img_subhead_num) _print_image_header(hdr) print_func('') if details.graphics_subheader_offsets is not None: for graphics_subhead_num in range( details.graphics_subheader_offsets.size): print_func('----- Graphic {} -----'.format(graphics_subhead_num)) hdr = details.parse_graphics_subheader(graphics_subhead_num) _print_graphics_header(hdr) data = details.get_graphics_bytes(graphics_subhead_num) print_func('GSDATA = {}'.format(_decode_effort(data))) print_func('') if details.symbol_subheader_offsets is not None: for symbol_subhead_num in range(details.symbol_subheader_offsets.size): print_func('----- Symbol {} -----'.format(symbol_subhead_num)) hdr = details.parse_symbol_subheader(symbol_subhead_num) _print_symbol_header(hdr) data = details.get_symbol_bytes(symbol_subhead_num) print_func('SSDATA = {}'.format(_decode_effort(data))) print_func('') if details.label_subheader_offsets is not None: for label_subhead_num in range(details.label_subheader_offsets.size): print_func('----- Label {} -----'.format(label_subhead_num)) hdr = details.parse_label_subheader(label_subhead_num) _print_label_header(hdr) data = details.get_label_bytes(label_subhead_num) print_func('LSDATA = {}'.format(_decode_effort(data))) print_func('') if details.text_subheader_offsets is not None: for text_subhead_num in range(details.text_subheader_offsets.size): print_func('----- Text {} -----'.format(text_subhead_num)) hdr = details.parse_text_subheader(text_subhead_num) _print_text_header(hdr) data = details.get_text_bytes(text_subhead_num) print_func('TSDATA = {}'.format(_decode_effort(data))) print_func('') if details.des_subheader_offsets is not None: for des_subhead_num in range(details.des_subheader_offsets.size): print_func('----- DES {} -----'.format(des_subhead_num)) hdr = details.parse_des_subheader(des_subhead_num) _print_des_header(hdr) data = details.get_des_bytes(des_subhead_num) des_id = hdr.DESID if details.nitf_version == '02.10' else hdr.DESTAG if des_id.strip() in ['XML_DATA_CONTENT', 'SICD_XML', 'SIDD_XML']: xml_str = minidom.parseString(data.decode()).toprettyxml( indent=' ', newl='\n') # NB: this may or not exhibit platform dependent choices in which codec (i.e. latin-1 versus utf-8) print_func('DESDATA =') for line_num, xml_entry in enumerate(xml_str.splitlines()): if line_num == 0: # Remove xml that gets inserted by minidom, if it's not actually there if (not data.startswith(b'<?xml version') ) and xml_entry.startswith('<?xml version'): continue print_func(xml_entry) elif xml_entry.strip() != '': # Remove extra new lines if XML is already formatted print_func(xml_entry) elif des_id.strip() in [ 'TRE_OVERFLOW', 'Registered Extensions', 'Controlled Extensions' ]: tres = TREList.from_bytes(data, 0) print_func('DESDATA = ') _print_tres(tres) else: # Unknown user-defined data print_func('DESDATA = {}'.format(_decode_effort(data))) print_func('') if details.res_subheader_offsets is not None: for res_subhead_num in range(details.res_subheader_offsets.size): print_func('----- RES {} -----'.format(res_subhead_num)) hdr = details.parse_res_subheader(res_subhead_num) _print_res_header(hdr) data = details.get_res_bytes(res_subhead_num) print_func('RESDATA = {}'.format(_decode_effort(data))) print_func('')
def print_nitf(file_name, dest=sys.stdout): # Configure print function for desired destination # - e.g., stdout, string buffer, file global print_func print_func = functools.partial(print, file=dest) details = NITFDetails(file_name) print_func('----- File Header -----') print_file_header(details.nitf_header) print_func('') if details.img_subheader_offsets is not None: for i in range(details.img_subheader_offsets.size): print_func('----- Image {} -----'.format(i)) hdr = details.parse_image_subheader(i) print_image_header(hdr) print_func('') if details.graphics_subheader_offsets is not None: for i in range(details.graphics_subheader_offsets.size): print_func('----- Graphic {} -----'.format(i)) hdr = details.parse_graphics_subheader(i) print_graphics_header(hdr) data = details.get_graphics_bytes(i) print_func('GSDATA = {}'.format(_decode_effort(data))) print_func('') if details.text_subheader_offsets is not None: for i in range(details.text_subheader_offsets.size): print_func('----- Text {} -----'.format(i)) hdr = details.parse_text_subheader(i) print_text_header(hdr) data = details.get_text_bytes(i) print_func('TSDATA = {}'.format(_decode_effort(data))) print_func('') if details.des_subheader_offsets is not None: for i in range(details.des_subheader_offsets.size): print_func('----- DES {} -----'.format(i)) hdr = details.parse_des_subheader(i) print_des_header(hdr) data = details.get_des_bytes(i) if hdr.DESID.strip() == 'XML_DATA_CONTENT': xml_str = xml.dom.minidom.parseString( data.decode()).toprettyxml(indent=' ') # Remove extra new lines if XML is already formatted xml_str = os.linesep.join( [s for s in xml_str.splitlines() if s.strip()]) print_func('DESDATA = {}'.format(xml_str)) elif hdr.DESID.strip() == 'TRE_OVERFLOW': tres = TREList.from_bytes(data, 0) print_func('DESDATA = ') print_tres(tres) else: # Unknown user-defined data print_func('DESDATA = {}'.format(_decode_effort(data))) print_func('') if details.res_subheader_offsets is not None: for i in range(details.res_subheader_offsets.size): print_func('----- RES {} -----'.format(i)) hdr = details.parse_res_subheader(i) print_res_header(hdr) data = details.get_res_bytes(i) print_func('RESDATA = {}'.format(_decode_effort(data))) print_func('')