paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Generic file access. |
paul@44 | 5 | |
paul@86 | 6 | Copyright (C) 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@44 | 7 | |
paul@44 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@44 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@44 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@44 | 11 | version. |
paul@44 | 12 | |
paul@44 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@44 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@44 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@44 | 16 | |
paul@44 | 17 | You should have received a copy of the GNU General Public License along |
paul@44 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@44 | 19 | """ |
paul@44 | 20 | |
paul@89 | 21 | from iixr.data import * |
paul@67 | 22 | from array import array |
paul@50 | 23 | import zlib |
paul@44 | 24 | |
paul@44 | 25 | # Constants. |
paul@44 | 26 | |
paul@94 | 27 | CACHE_SIZE = 100000 |
paul@93 | 28 | |
paul@93 | 29 | # Classes. |
paul@93 | 30 | |
paul@44 | 31 | class File: |
paul@44 | 32 | |
paul@44 | 33 | "A basic file abstraction." |
paul@44 | 34 | |
paul@44 | 35 | def __init__(self, f): |
paul@44 | 36 | self.f = f |
paul@89 | 37 | self.record = array('B') # record buffer |
paul@93 | 38 | self.cache = array('B') |
paul@91 | 39 | self.data_start = 0 |
paul@44 | 40 | |
paul@44 | 41 | def reset(self): |
paul@44 | 42 | |
paul@44 | 43 | "To be used to reset the state of the reader or writer between records." |
paul@44 | 44 | |
paul@44 | 45 | pass |
paul@44 | 46 | |
paul@69 | 47 | def seek(self, offset): |
paul@69 | 48 | self.f.seek(offset) |
paul@69 | 49 | self.reset() |
paul@69 | 50 | |
paul@44 | 51 | def rewind(self): |
paul@93 | 52 | self.seek(self.data_start) |
paul@44 | 53 | |
paul@44 | 54 | def close(self): |
paul@44 | 55 | if self.f is not None: |
paul@44 | 56 | self.f.close() |
paul@44 | 57 | self.f = None |
paul@44 | 58 | |
paul@44 | 59 | class FileWriter(File): |
paul@44 | 60 | |
paul@44 | 61 | "Writing basic data types to files." |
paul@44 | 62 | |
paul@91 | 63 | def __init__(self, f): |
paul@91 | 64 | File.__init__(self, f) |
paul@91 | 65 | self.written = 0 |
paul@91 | 66 | |
paul@88 | 67 | def tell(self): |
paul@90 | 68 | # NOTE: Will not be accurate within the current record. |
paul@91 | 69 | return self.written |
paul@44 | 70 | |
paul@89 | 71 | def begin_record(self): |
paul@89 | 72 | pass |
paul@89 | 73 | |
paul@89 | 74 | def end_record(self): |
paul@90 | 75 | if self.record: |
paul@91 | 76 | length = len(self.record) |
paul@93 | 77 | before = len(self.cache) |
paul@93 | 78 | vint_to_array(length, self.cache) |
paul@93 | 79 | length_size = len(self.cache) - before |
paul@93 | 80 | self.cache += self.record |
paul@93 | 81 | self.written += length_size + length |
paul@90 | 82 | self.record = array('B') |
paul@94 | 83 | self.flush_cache() |
paul@89 | 84 | |
paul@44 | 85 | def write_number(self, number): |
paul@44 | 86 | |
paul@44 | 87 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 88 | |
paul@89 | 89 | vint_to_array(number, self.record) |
paul@44 | 90 | |
paul@86 | 91 | def write_numbers(self, numbers): |
paul@86 | 92 | |
paul@86 | 93 | "Write 'numbers' to the file using a variable length encoding." |
paul@86 | 94 | |
paul@86 | 95 | for number in numbers: |
paul@89 | 96 | vint_to_array(number, self.record) |
paul@86 | 97 | |
paul@44 | 98 | def write_string(self, s, compress=0): |
paul@44 | 99 | |
paul@44 | 100 | """ |
paul@44 | 101 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 102 | if 'compress' is set to a true value. |
paul@44 | 103 | """ |
paul@44 | 104 | |
paul@44 | 105 | # Convert Unicode objects to strings. |
paul@44 | 106 | |
paul@44 | 107 | if isinstance(s, unicode): |
paul@44 | 108 | s = s.encode("utf-8") |
paul@44 | 109 | |
paul@44 | 110 | # Compress the string if requested. |
paul@44 | 111 | |
paul@44 | 112 | if compress: |
paul@50 | 113 | cs = zlib.compress(s) |
paul@50 | 114 | |
paul@50 | 115 | # Take any shorter than the original. |
paul@44 | 116 | |
paul@50 | 117 | if len(cs) < len(s): |
paul@50 | 118 | flag = "z" |
paul@50 | 119 | s = cs |
paul@44 | 120 | else: |
paul@44 | 121 | flag = "-" |
paul@44 | 122 | |
paul@44 | 123 | else: |
paul@44 | 124 | flag = "" |
paul@44 | 125 | |
paul@44 | 126 | # Write the length of the data before the data itself. |
paul@44 | 127 | |
paul@44 | 128 | length = len(s) |
paul@89 | 129 | self.record.fromstring("".join([flag, vint(length), s])) |
paul@89 | 130 | |
paul@91 | 131 | def write_sequence_value(self, value, size): |
paul@91 | 132 | sequence_to_array(value, size, self.record) |
paul@89 | 133 | |
paul@91 | 134 | def write_sequence_values(self, values, size): |
paul@89 | 135 | vint_to_array(len(values), self.record) |
paul@89 | 136 | for value in values: |
paul@91 | 137 | self.write_sequence_value(value, size) |
paul@44 | 138 | |
paul@91 | 139 | def write_delta_sequence(self, values, size): |
paul@89 | 140 | convert_sequence(values, get_subtractor(values[0])) |
paul@91 | 141 | self.write_sequence_values(values, size) |
paul@89 | 142 | |
paul@91 | 143 | def write_monotonic_sequence(self, values, size): |
paul@89 | 144 | convert_sequence(values, get_monotonic_subtractor(values[0])) |
paul@91 | 145 | self.write_sequence_values(values, size) |
paul@74 | 146 | |
paul@93 | 147 | def flush(self, force=0): |
paul@94 | 148 | self.end_record() |
paul@94 | 149 | self.flush_cache(force) |
paul@94 | 150 | |
paul@94 | 151 | def flush_cache(self, force=0): |
paul@89 | 152 | if self.f is not None: |
paul@93 | 153 | if force or len(self.cache) > CACHE_SIZE: |
paul@93 | 154 | self.cache.tofile(self.f) |
paul@93 | 155 | self.cache = array('B') |
paul@89 | 156 | |
paul@89 | 157 | def close(self): |
paul@93 | 158 | self.flush(1) |
paul@89 | 159 | File.close(self) |
paul@74 | 160 | |
paul@44 | 161 | class FileReader(File): |
paul@44 | 162 | |
paul@44 | 163 | "Reading basic data types from files." |
paul@44 | 164 | |
paul@91 | 165 | def __init__(self, f): |
paul@91 | 166 | File.__init__(self, f) |
paul@93 | 167 | self.record_start = 0 |
paul@93 | 168 | self.record_end = 0 |
paul@93 | 169 | self.cache_start = 0 |
paul@91 | 170 | self.begin() |
paul@91 | 171 | |
paul@91 | 172 | def begin(self): |
paul@91 | 173 | |
paul@91 | 174 | "Initialise file-wide parameters." |
paul@91 | 175 | |
paul@91 | 176 | pass |
paul@91 | 177 | |
paul@89 | 178 | def begin_record(self): |
paul@89 | 179 | self.start = 0 |
paul@90 | 180 | try: |
paul@90 | 181 | size = self.read_number_from_file() |
paul@93 | 182 | self.record = self.from_cache(size) |
paul@90 | 183 | except EOFError: |
paul@90 | 184 | pass |
paul@89 | 185 | |
paul@89 | 186 | def end_record(self): |
paul@90 | 187 | pass |
paul@89 | 188 | |
paul@93 | 189 | def seek(self, offset): |
paul@93 | 190 | if self.cache_start <= offset < self.cache_start + len(self.cache): |
paul@93 | 191 | self.cache = self.cache[offset - self.cache_start:] |
paul@93 | 192 | else: |
paul@93 | 193 | self.f.seek(offset) |
paul@93 | 194 | self.cache = array('B') |
paul@93 | 195 | self.cache_start = offset |
paul@93 | 196 | self.record_start = 0 |
paul@93 | 197 | self.record_end = 0 |
paul@93 | 198 | self.reset() |
paul@93 | 199 | |
paul@93 | 200 | def tell(self): |
paul@93 | 201 | return self.cache_start + self.record_start + self.start |
paul@93 | 202 | |
paul@93 | 203 | def ensure_cache(self, size): |
paul@93 | 204 | if size > len(self.cache) - self.record_end: |
paul@93 | 205 | self.cache = self.cache[self.record_end:] |
paul@93 | 206 | self.cache_start += self.record_end |
paul@93 | 207 | s = self.f.read(CACHE_SIZE) |
paul@93 | 208 | self.cache.fromstring(s) |
paul@93 | 209 | self.record_start = 0 |
paul@93 | 210 | if not s: |
paul@93 | 211 | raise EOFError |
paul@93 | 212 | else: |
paul@93 | 213 | self.record_start = self.record_end |
paul@93 | 214 | self.record_end = self.record_start + size |
paul@93 | 215 | |
paul@93 | 216 | def from_cache(self, size): |
paul@93 | 217 | self.ensure_cache(size) |
paul@93 | 218 | return self.cache[self.record_start:self.record_end] |
paul@93 | 219 | |
paul@89 | 220 | def read_number_from_file(self): |
paul@44 | 221 | |
paul@44 | 222 | "Read a number from the file." |
paul@44 | 223 | |
paul@44 | 224 | # Read each byte, adding it to the number. |
paul@44 | 225 | |
paul@89 | 226 | a = array('B') |
paul@93 | 227 | a += self.from_cache(1) |
paul@89 | 228 | csd = a[-1] |
paul@89 | 229 | if csd < 128: |
paul@89 | 230 | return csd |
paul@89 | 231 | else: |
paul@89 | 232 | while csd & 128: |
paul@93 | 233 | a += self.from_cache(1) |
paul@89 | 234 | csd = a[-1] |
paul@89 | 235 | return vint_from_array(a) |
paul@89 | 236 | |
paul@89 | 237 | def read_number(self): |
paul@89 | 238 | |
paul@89 | 239 | "Read a number from the current record." |
paul@89 | 240 | |
paul@89 | 241 | n, self.start = vint_from_array_start(self.record, self.start) |
paul@89 | 242 | return n |
paul@44 | 243 | |
paul@91 | 244 | def read_numbers(self, n): |
paul@91 | 245 | l = [] |
paul@91 | 246 | i = 0 |
paul@91 | 247 | while i < n: |
paul@91 | 248 | l.append(self.read_number()) |
paul@91 | 249 | i += 1 |
paul@91 | 250 | return l |
paul@91 | 251 | |
paul@44 | 252 | def read_string(self, decompress=0): |
paul@44 | 253 | |
paul@44 | 254 | """ |
paul@89 | 255 | Read a string from the current record, decompressing the stored data if |
paul@44 | 256 | 'decompress' is set to a true value. |
paul@44 | 257 | """ |
paul@44 | 258 | |
paul@44 | 259 | # Decompress the data if requested. |
paul@44 | 260 | |
paul@44 | 261 | if decompress: |
paul@89 | 262 | flag = chr(self.record[self.start]) |
paul@89 | 263 | self.start += 1 |
paul@44 | 264 | else: |
paul@44 | 265 | flag = "-" |
paul@44 | 266 | |
paul@44 | 267 | length = self.read_number() |
paul@89 | 268 | start = self.start |
paul@89 | 269 | self.start += length |
paul@89 | 270 | s = self.record[start:self.start].tostring() |
paul@44 | 271 | |
paul@89 | 272 | # Perform decompression if applicable. |
paul@44 | 273 | |
paul@89 | 274 | if flag == "z": |
paul@89 | 275 | s = zlib.decompress(s) |
paul@88 | 276 | |
paul@89 | 277 | # Convert strings to Unicode objects. |
paul@44 | 278 | |
paul@89 | 279 | return unicode(s, "utf-8") |
paul@44 | 280 | |
paul@91 | 281 | def read_sequence_value(self, size): |
paul@91 | 282 | value, self.start = sequence_from_array(self.record, size, self.start) |
paul@89 | 283 | return value |
paul@44 | 284 | |
paul@91 | 285 | def read_sequences(self, size): |
paul@89 | 286 | values = [] |
paul@89 | 287 | length = self.read_number() |
paul@89 | 288 | i = 0 |
paul@89 | 289 | while i < length: |
paul@91 | 290 | values.append(self.read_sequence_value(size)) |
paul@89 | 291 | i += 1 |
paul@89 | 292 | return values |
paul@87 | 293 | |
paul@91 | 294 | def read_delta_sequence(self, size): |
paul@91 | 295 | values = self.read_sequences(size) |
paul@89 | 296 | convert_sequence(values, get_adder(values[0])) |
paul@89 | 297 | return values |
paul@87 | 298 | |
paul@91 | 299 | def read_monotonic_sequence(self, size): |
paul@91 | 300 | values = self.read_sequences(size) |
paul@89 | 301 | convert_sequence(values, get_monotonic_adder(values[0])) |
paul@89 | 302 | return values |
paul@74 | 303 | |
paul@44 | 304 | # vim: tabstop=4 expandtab shiftwidth=4 |