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