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@93 | 25 | # Classes. |
paul@93 | 26 | |
paul@44 | 27 | class File: |
paul@44 | 28 | |
paul@44 | 29 | "A basic file abstraction." |
paul@44 | 30 | |
paul@44 | 31 | def __init__(self, f): |
paul@44 | 32 | self.f = f |
paul@89 | 33 | self.record = array('B') # record buffer |
paul@96 | 34 | self.data_start = None |
paul@96 | 35 | |
paul@96 | 36 | def begin(self): |
paul@96 | 37 | |
paul@96 | 38 | """ |
paul@96 | 39 | Initialise file-wide parameters. In writers, this method may require |
paul@96 | 40 | parameters to be specified. In readers, the parameters may be read from |
paul@96 | 41 | the file. |
paul@96 | 42 | """ |
paul@96 | 43 | |
paul@91 | 44 | self.data_start = 0 |
paul@44 | 45 | |
paul@96 | 46 | def tell(self): |
paul@96 | 47 | # NOTE: Will not be accurate within the current record. |
paul@96 | 48 | return self.f.tell() |
paul@44 | 49 | |
paul@69 | 50 | def seek(self, offset): |
paul@69 | 51 | self.f.seek(offset) |
paul@69 | 52 | self.reset() |
paul@69 | 53 | |
paul@44 | 54 | def rewind(self): |
paul@93 | 55 | self.seek(self.data_start) |
paul@44 | 56 | |
paul@44 | 57 | def close(self): |
paul@44 | 58 | if self.f is not None: |
paul@44 | 59 | self.f.close() |
paul@44 | 60 | self.f = None |
paul@44 | 61 | |
paul@44 | 62 | class FileWriter(File): |
paul@44 | 63 | |
paul@44 | 64 | "Writing basic data types to files." |
paul@44 | 65 | |
paul@89 | 66 | def begin_record(self): |
paul@89 | 67 | pass |
paul@89 | 68 | |
paul@89 | 69 | def end_record(self): |
paul@90 | 70 | if self.record: |
paul@96 | 71 | self.f.write(vint(len(self.record))) |
paul@96 | 72 | self.record.tofile(self.f) |
paul@90 | 73 | self.record = array('B') |
paul@96 | 74 | |
paul@96 | 75 | def write_remaining(self, a): |
paul@96 | 76 | |
paul@96 | 77 | "Write remaining data from the raw array 'a'." |
paul@96 | 78 | |
paul@96 | 79 | self.record += a |
paul@96 | 80 | |
paul@96 | 81 | def write_byte(self, b): |
paul@96 | 82 | |
paul@96 | 83 | "Write the given byte 'b'." |
paul@96 | 84 | |
paul@96 | 85 | self.record.append(b) |
paul@89 | 86 | |
paul@44 | 87 | def write_number(self, number): |
paul@44 | 88 | |
paul@44 | 89 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 90 | |
paul@89 | 91 | vint_to_array(number, self.record) |
paul@44 | 92 | |
paul@86 | 93 | def write_numbers(self, numbers): |
paul@86 | 94 | |
paul@86 | 95 | "Write 'numbers' to the file using a variable length encoding." |
paul@86 | 96 | |
paul@86 | 97 | for number in numbers: |
paul@89 | 98 | vint_to_array(number, self.record) |
paul@86 | 99 | |
paul@44 | 100 | def write_string(self, s, compress=0): |
paul@44 | 101 | |
paul@44 | 102 | """ |
paul@44 | 103 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 104 | if 'compress' is set to a true value. |
paul@44 | 105 | """ |
paul@44 | 106 | |
paul@44 | 107 | # Convert Unicode objects to strings. |
paul@44 | 108 | |
paul@44 | 109 | if isinstance(s, unicode): |
paul@44 | 110 | s = s.encode("utf-8") |
paul@44 | 111 | |
paul@44 | 112 | # Compress the string if requested. |
paul@44 | 113 | |
paul@44 | 114 | if compress: |
paul@50 | 115 | cs = zlib.compress(s) |
paul@50 | 116 | |
paul@50 | 117 | # Take any shorter than the original. |
paul@44 | 118 | |
paul@50 | 119 | if len(cs) < len(s): |
paul@50 | 120 | flag = "z" |
paul@50 | 121 | s = cs |
paul@44 | 122 | else: |
paul@44 | 123 | flag = "-" |
paul@44 | 124 | |
paul@44 | 125 | else: |
paul@44 | 126 | flag = "" |
paul@44 | 127 | |
paul@44 | 128 | # Write the length of the data before the data itself. |
paul@44 | 129 | |
paul@44 | 130 | length = len(s) |
paul@89 | 131 | self.record.fromstring("".join([flag, vint(length), s])) |
paul@89 | 132 | |
paul@91 | 133 | def write_sequence_value(self, value, size): |
paul@91 | 134 | sequence_to_array(value, size, self.record) |
paul@89 | 135 | |
paul@91 | 136 | def write_sequence_values(self, values, size): |
paul@89 | 137 | vint_to_array(len(values), self.record) |
paul@89 | 138 | for value in values: |
paul@91 | 139 | self.write_sequence_value(value, size) |
paul@44 | 140 | |
paul@91 | 141 | def write_delta_sequence(self, values, size): |
paul@96 | 142 | self.write_sequence_values( |
paul@96 | 143 | convert_sequence(values, get_subtractor(size), 1), |
paul@96 | 144 | size) |
paul@89 | 145 | |
paul@91 | 146 | def write_monotonic_sequence(self, values, size): |
paul@96 | 147 | self.write_sequence_values( |
paul@96 | 148 | convert_sequence(values, get_monotonic_subtractor(size), 1), |
paul@96 | 149 | size) |
paul@89 | 150 | |
paul@89 | 151 | def close(self): |
paul@96 | 152 | self.end_record() |
paul@89 | 153 | File.close(self) |
paul@74 | 154 | |
paul@44 | 155 | class FileReader(File): |
paul@44 | 156 | |
paul@44 | 157 | "Reading basic data types from files." |
paul@44 | 158 | |
paul@91 | 159 | def __init__(self, f): |
paul@91 | 160 | File.__init__(self, f) |
paul@91 | 161 | self.begin() |
paul@91 | 162 | |
paul@89 | 163 | def begin_record(self): |
paul@89 | 164 | self.start = 0 |
paul@96 | 165 | self.record = array('B') |
paul@90 | 166 | try: |
paul@90 | 167 | size = self.read_number_from_file() |
paul@96 | 168 | self.record.fromfile(self.f, size) |
paul@90 | 169 | except EOFError: |
paul@90 | 170 | pass |
paul@89 | 171 | |
paul@89 | 172 | def end_record(self): |
paul@90 | 173 | pass |
paul@89 | 174 | |
paul@96 | 175 | def read_remaining(self): |
paul@93 | 176 | |
paul@96 | 177 | "Read remaining data as a raw array." |
paul@96 | 178 | |
paul@96 | 179 | return self.record[self.start:] |
paul@93 | 180 | |
paul@96 | 181 | def read_byte(self): |
paul@96 | 182 | |
paul@96 | 183 | "Read a byte from the record." |
paul@93 | 184 | |
paul@96 | 185 | b = self.record[self.start] |
paul@96 | 186 | self.start += 1 |
paul@96 | 187 | return b |
paul@93 | 188 | |
paul@89 | 189 | def read_number_from_file(self): |
paul@44 | 190 | |
paul@44 | 191 | "Read a number from the file." |
paul@44 | 192 | |
paul@44 | 193 | # Read each byte, adding it to the number. |
paul@44 | 194 | |
paul@89 | 195 | a = array('B') |
paul@96 | 196 | a.fromfile(self.f, 1) |
paul@89 | 197 | csd = a[-1] |
paul@89 | 198 | if csd < 128: |
paul@89 | 199 | return csd |
paul@89 | 200 | else: |
paul@89 | 201 | while csd & 128: |
paul@96 | 202 | a.fromfile(self.f, 1) |
paul@89 | 203 | csd = a[-1] |
paul@89 | 204 | return vint_from_array(a) |
paul@89 | 205 | |
paul@89 | 206 | def read_number(self): |
paul@89 | 207 | |
paul@89 | 208 | "Read a number from the current record." |
paul@89 | 209 | |
paul@89 | 210 | n, self.start = vint_from_array_start(self.record, self.start) |
paul@89 | 211 | return n |
paul@44 | 212 | |
paul@91 | 213 | def read_numbers(self, n): |
paul@91 | 214 | l = [] |
paul@91 | 215 | i = 0 |
paul@91 | 216 | while i < n: |
paul@91 | 217 | l.append(self.read_number()) |
paul@91 | 218 | i += 1 |
paul@91 | 219 | return l |
paul@91 | 220 | |
paul@44 | 221 | def read_string(self, decompress=0): |
paul@44 | 222 | |
paul@44 | 223 | """ |
paul@89 | 224 | Read a string from the current record, decompressing the stored data if |
paul@44 | 225 | 'decompress' is set to a true value. |
paul@44 | 226 | """ |
paul@44 | 227 | |
paul@44 | 228 | # Decompress the data if requested. |
paul@44 | 229 | |
paul@44 | 230 | if decompress: |
paul@89 | 231 | flag = chr(self.record[self.start]) |
paul@89 | 232 | self.start += 1 |
paul@44 | 233 | else: |
paul@44 | 234 | flag = "-" |
paul@44 | 235 | |
paul@44 | 236 | length = self.read_number() |
paul@89 | 237 | start = self.start |
paul@89 | 238 | self.start += length |
paul@89 | 239 | s = self.record[start:self.start].tostring() |
paul@44 | 240 | |
paul@89 | 241 | # Perform decompression if applicable. |
paul@44 | 242 | |
paul@89 | 243 | if flag == "z": |
paul@89 | 244 | s = zlib.decompress(s) |
paul@88 | 245 | |
paul@89 | 246 | # Convert strings to Unicode objects. |
paul@44 | 247 | |
paul@89 | 248 | return unicode(s, "utf-8") |
paul@44 | 249 | |
paul@91 | 250 | def read_sequence_value(self, size): |
paul@91 | 251 | value, self.start = sequence_from_array(self.record, size, self.start) |
paul@89 | 252 | return value |
paul@44 | 253 | |
paul@91 | 254 | def read_sequences(self, size): |
paul@89 | 255 | values = [] |
paul@89 | 256 | length = self.read_number() |
paul@89 | 257 | i = 0 |
paul@89 | 258 | while i < length: |
paul@91 | 259 | values.append(self.read_sequence_value(size)) |
paul@89 | 260 | i += 1 |
paul@89 | 261 | return values |
paul@87 | 262 | |
paul@91 | 263 | def read_delta_sequence(self, size): |
paul@96 | 264 | return convert_sequence(self.read_sequences(size), get_adder(size), 0) |
paul@87 | 265 | |
paul@91 | 266 | def read_monotonic_sequence(self, size): |
paul@96 | 267 | return convert_sequence(self.read_sequences(size), get_monotonic_adder(size), 0) |
paul@74 | 268 | |
paul@44 | 269 | # vim: tabstop=4 expandtab shiftwidth=4 |