| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Reader class for indexed gzipped files |
| 5 |
""" |
| 6 |
|
| 7 |
|
| 8 |
n |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
import struct |
| 30 |
import zlib |
| 31 |
from collections import OrderedDict |
| 32 |
|
| 33 |
|
| 34 |
class GSGR(object): |
| 35 |
""" |
| 36 |
Generalized Gzip reader class which enables random access in files |
| 37 |
written with the :class:`~pymzml.utils.GSGW.GSGW` class. |
| 38 |
|
| 39 |
Keyword Arguments: |
| 40 |
file (str): path to file to read |
| 41 |
""" |
| 42 |
|
| 43 |
def __init__(self, file=None): |
| 44 |
|
| 45 |
self.file_in = open(file, "rb") |
| 46 |
self.filename = file |
| 47 |
self.magic_bytes = b"\x1f\x8b" |
| 48 |
self.indexed = True |
| 49 |
|
| 50 |
if not self._check_magic_bytes(): |
| 51 |
raise Exception("not a gzip file (wrong magic bytes)") |
| 52 |
|
| 53 |
self.random_access = False |
| 54 |
|
| 55 |
self._read_basic_header() |
| 56 |
if self.flg & 0 != 0: |
| 57 |
self.ascii_file = True |
| 58 |
if self.flg & 2 != 0: |
| 59 |
crc16 = self.file_in.read(2) |
| 60 |
if self.flg & 4 != 0: |
| 61 |
|
| 62 |
xlen = struct.unpack("<H", self.file_in.read(2))[0] |
| 63 |
self.file_in.seek(xlen) |
| 64 |
if self.flg & 8 != 0: |
| 65 |
self.fname = self._read_until_zero() |
| 66 |
if self.flg & 16 == 0: |
| 67 |
self.indexed = False |
| 68 |
else: |
| 69 |
self._read_index() |
| 70 |
|
| 71 |
def __del__(self): |
| 72 |
try: |
| 73 |
self.close() |
| 74 |
except: |
| 75 |
raise Exception(" cant close file") |
| 76 |
|
| 77 |
def seek(self, offset): |
| 78 |
""" |
| 79 |
Seek to byte offset in input file. |
| 80 |
|
| 81 |
Arguments: |
| 82 |
offset (int): byte offset to seek to in FileIn |
| 83 |
|
| 84 |
Returns: |
| 85 |
None |
| 86 |
""" |
| 87 |
self.file_in.seek(offset) |
| 88 |
return |
| 89 |
|
| 90 |
def read_block(self, index): |
| 91 |
""" |
| 92 |
Read and return the data block with the unique index `index` |
| 93 |
|
| 94 |
Arguments: |
| 95 |
index(int or str): identifier associated with a specific block |
| 96 |
|
| 97 |
Returns: |
| 98 |
data (str): indexed text block as string |
| 99 |
""" |
| 100 |
start = self.index[index] |
| 101 |
try: |
| 102 |
end = self.index[int(index) + 1] |
| 103 |
except: |
| 104 |
end = self.file_in.seek(0, 2) |
| 105 |
self.file_in.seek(start) |
| 106 |
readSize = end - start |
| 107 |
comp_data = self.file_in.read(readSize) |
| 108 |
data = zlib.decompress(comp_data, -zlib.MAX_WBITS) |
| 109 |
return data |
| 110 |
|
| 111 |
def _check_magic_bytes(self): |
| 112 |
""" |
| 113 |
Check if file is a gzip file. |
| 114 |
""" |
| 115 |
|
| 116 |
mb = self.file_in.read(2) |
| 117 |
return mb == self.magic_bytes |
| 118 |
|
| 119 |
def _read_basic_header(self): |
| 120 |
""" |
| 121 |
Read and save compression method, bitflags, changetime, |
| 122 |
compression speed and os. |
| 123 |
""" |
| 124 |
self.file_in.seek(2) |
| 125 |
vals = struct.unpack("<BBLBB", self.file_in.read(8)) |
| 126 |
self.cm = vals[0] |
| 127 |
self.flg = vals[1] |
| 128 |
self.mtime = vals[2] |
| 129 |
self.xfl = vals[3] |
| 130 |
self.os = vals[4] |
| 131 |
|
| 132 |
def _read_until_zero(self): |
| 133 |
""" |
| 134 |
Read input until \x00 is reached |
| 135 |
""" |
| 136 |
buf = b"" |
| 137 |
c = self.file_in.read(1) |
| 138 |
while c != b"\x00": |
| 139 |
buf += c |
| 140 |
c = self.file_in.read(1) |
| 141 |
return buf |
| 142 |
|
| 143 |
def _read_index(self): |
| 144 |
""" |
| 145 |
Read and save offset dict from indexed gzip file |
| 146 |
""" |
| 147 |
self.index = OrderedDict() |
| 148 |
self.file_in.seek(10) |
| 149 |
mb = self.file_in.read(3) |
| 150 |
if mb != b"FU\x01": |
| 151 |
print("No index in comment field found. No random access possible") |
| 152 |
self.indexed = False |
| 153 |
lengths = struct.unpack("<BB", self.file_in.read(2)) |
| 154 |
self.idx_len = lengths[0] |
| 155 |
self.offset_len = lengths[1] |
| 156 |
ID_block = b"" |
| 157 |
while b"\x00" not in ID_block: |
| 158 |
ID_block = self.file_in.read(self.idx_len) |
| 159 |
OffsetBlock = self.file_in.read(self.offset_len) |
| 160 |
try: |
| 161 |
try: |
| 162 |
Identifier = int(ID_block.decode("latin-1").strip("¬")) |
| 163 |
except: |
| 164 |
Identifier = ID_block.decode("latin-1").strip("¬") |
| 165 |
Offset = int(OffsetBlock.decode("latin-1").strip("¬")) |
| 166 |
self.index[Identifier] = Offset |
| 167 |
except: |
| 168 |
break |
| 169 |
self.file_in.seek(0) |
| 170 |
|
| 171 |
def read(self, size=-1): |
| 172 |
""" |
| 173 |
Read the content of the in File in binary mode |
| 174 |
|
| 175 |
Keyword Arguments: |
| 176 |
size (int, optional): number of bytes to read, -1 for everything |
| 177 |
|
| 178 |
Returns: |
| 179 |
data (bytes): parsed bytes from input file |
| 180 |
""" |
| 181 |
return self.file_in.read(size) |
| 182 |
|
| 183 |
def __enter__(self): |
| 184 |
""" |
| 185 |
Enable the with syntax for this class (entry point) |
| 186 |
""" |
| 187 |
return self.file_in |
| 188 |
|
| 189 |
def __exit__(self, exc_type, exc_value, traceback): |
| 190 |
""" |
| 191 |
destructor when using this class with 'with .. as ' |
| 192 |
""" |
| 193 |
self.file_in.close() |
| 194 |
|
| 195 |
def close(self): |
| 196 |
""" |
| 197 |
Close the internal Filehandler |
| 198 |
""" |
| 199 |
self.file_in.close() |
| 200 |
|
| 201 |
|
| 202 |
if __name__ == "__main__": |
| 203 |
print(__doc__) |