1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
#!/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("<B", self._format_version)
self.crc32 = 0
self.isize = 0
self.comp_str = comp_str
def __del__(self):
"""
Close the file object properly after this object is deleted
"""
self.file_out.close()
def close(self):
"""
Close the internal file object.
"""
self.file_out.close()
@property
def file_out(self):
"""
Output filehandler
"""
if self._file_out is None:
self._file_out = open(self.file_name, "wb")
return self._file_out
@property
def encoding(self):
"""
Returns the encoding used for this file
"""
return self._encoding
@encoding.setter
def encoding(self, encoding):
"""
Set the file encoding for the output file.
"""
assert type(encoding) == str, "encoding must be a string"
self._encoding = encoding
def _write_gen_header(self, Index=False, FLAGS=None):
"""
Write a valid gzip header with creation time, user defined flag fields
and allocated index.
Keyword Arguments:
Index (bool) : whether to or not to write an
index into this header.
FLAGS (list, optional) : list of flags (FTEXT, FHCRC, FEXTRA,
FNAME) to set for this header.
Returns:
offset (int): byte offset of the file pointer
"""
if FLAGS is None:
FLAGS = []
FTEXT, FHCRC, FEXTRA, FNAME = 1, 2, 4, 8 # extra field bit flags
current_time = int(time.time())
time_byte = struct.pack("<L", current_time)
self.generic_header["DATE"] = time_byte
if Index:
self.generic_header["FLAGS"] = b"\x10"
if FLAGS is not None:
if "FTEXT" in FLAGS:
self.generic_header["FLAGS"] = self.generic_header["FLAGS"] & FTEXT
if "FHCRC" in FLAGS:
header_crc32 = 0
self.generic_header["FLAGS"] = self.generic_header["FLAGS"] & FHCRC
for byte in self.generic_header.values():
header_crc32 = zlib.crc32(byte, header_crc32)
if "FEXTRA" in FLAGS:
self.generic_header["FLAGS"] = self.generic_header["FLAGS"] & FEXTRA
if "FNAME" in FLAGS:
self.generic_header["FLAGS"] = self.generic_header["FLAGS"] & FNAME
for value in self.generic_header.values():
self.file_out.write(value)
if "FEXTRA" in FLAGS:
# WRITE EXTRA FIELD
pass
if "FNAME" in FLAGS:
# WRITE FNAME FIELD
fName = self.file_name.split("/")[-1]
if Index:
self.generic_header["FLAGS"] = b"\x00"
self.file_out.write(self.index_magic_bytes)
self.file_out.write(struct.pack("<B", self.max_idx_len))
self.file_out.write(struct.pack("<B", self.max_offset_len))
self.index_offset = self.file_out.tell()
self._allocate_index_bytes()
if "FHCRC" in FLAGS:
# WRITE checksum for header
pass
return self.file_out.tell()
def _allocate_index_bytes(self):
"""
Allocate 'self.max_index_num' bytes of length 'self.max_idx_len'
in the header for inserting the index later on.
"""
id_placeholder = self.max_idx_len * b"\x01"
offset_placeholder = self.max_offset_len * b"\x01"
for i in range(self.max_idx_num):
self.file_out.write(id_placeholder)
self.file_out.write(offset_placeholder)
self.file_out.write(b"\x00")
return
def _write_data(self, data):
"""
Write data into file-stream.
Arguments:
data (str): uncompressed data
"""
Compressor = zlib.compressobj(
self.comp_str, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0
)
# compress data and flush (includes writing crc32 and isize)
if isinstance(data, bytes) is False:
data = bytes(data, "latin-1")
self.crc32 = zlib.crc32(data)
self.isize = len(data) % 2 ** 32
comp_data = Compressor.compress(data) + Compressor.flush()
self.file_out.write(comp_data)
self.file_out.write(struct.pack("<L", self.crc32))
self.file_out.write(struct.pack("<L", self.isize))
return
def add_data(self, data, identifier):
"""
Create a new gzip member with compressed 'data' indexed with 'index'.
Arguments:
data (str) : uncompressed data to write to file
index (str or int) : unique index for the data
"""
if self.Lock is False:
if len(self.index) + 1 > 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__)
|