#!/usr/bin/env python # -*- coding: latin-1 -*- """ Writer class for indexed gzipped files """ # Python mzML module - pymzml # Copyright (C) 2010-2019 M. Kösters, C. Fufezan # The MIT License (MIT) # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import struct import time import zlib from collections import OrderedDict class GSGW(object): """ Generalized Gzip writer class with random access to indexed offsets. Keyword Arguments: file (string) : Filename for the resulting file max_idx (int) : max number of indices which can be saved in this file max_idx_len (int) : maximal length of the index in bytes, must be between 1 and 255 max_offset_len (int) : maximal length of the offset in bytes output_path (str) : path to the output file """ def __init__( self, file=None, max_idx=10000, max_idx_len=8, max_offset_len=8, output_path="./test.dat.igzip", comp_str=-1, ): self.Lock = False self._format_version = 1 # max 255!!! self.file_name = output_path self.max_idx_num = max_idx self.max_idx_len = max_idx_len self.max_offset_len = max_offset_len self.generic_header = OrderedDict( [ ("MAGIC_BYTE_1", b"\x1f"), ("MAGIC_BYTE_2", b"\x8b"), ("COMPRESSION", b"\x08"), ("FLAGS", b"\x00"), ("DATE", b"\x00\x00\x00\x00"), ("XFL", b"\x02"), ("OS", b"\x03"), ] ) self.index = OrderedDict() self.first_header_set = False self._file_out = None self._encoding = "latin-1" # magic bytes. FU+version self.index_magic_bytes = b"FU" + struct.pack(" self.max_idx_num: print( """ WARNING: Reached maximum number of indexed data blocks '({0}), cannot add any more data! """.format( self.max_idx_num ) ) return False if not self.first_header_set: self._write_gen_header(Index=True) self.first_header_set = True else: # do we need this? self._write_gen_header(Index=False) self.index[identifier] = self.file_out.tell() self._write_data(data) return else: raise Exception("Cant add any more data if index is already written") def _write_identifier(self, identifier): """ Convert and write the identifier into output file. Arguments: identifier (str or int): identifier to write into index """ id_format = "{0:\xAC>" + str(self.max_idx_len) + "}" identifier = str(identifier) identifier = id_format.format(identifier).encode("latin-1") self.file_out.write(identifier) return def _write_offset(self, offset): """ Convert and write offset to output file. Arguments: offset (int): offset which will be formatted and written into file index """ offset_format = "{0:\xAC>" + str(self.max_offset_len) + "}" offset = str(offset) offset = offset_format.format(offset).encode("latin-1") self.file_out.write(offset) return def write_index(self): """ Only called after all the data is written, i.e. all calls to :func:`~GSGW.add_data` have been done. Seek back to the beginning of the file and write the index into the allocated comment bytes (see _write_gen_header(Index=True)). """ self.Lock = True self.file_out.seek(self.index_offset) for identifier, offset in self.index.items(): self._write_identifier(identifier) self._write_offset(offset) def __enter__(self): """ Enable the with syntax for this class (entry point). """ return self def __exit__(self, exc_type, exc_value, traceback): """Destructor when using this class with 'with .. as'.""" self.file_out.close() if __name__ == "__main__": print(__doc__)