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@86 | 21 | from iixr.data import vint, vint_to_array, vint_from_array |
paul@67 | 22 | from array import array |
paul@50 | 23 | import zlib |
paul@44 | 24 | |
paul@44 | 25 | # Constants. |
paul@44 | 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@88 | 33 | self.data = array('B') |
paul@44 | 34 | self.reset() |
paul@44 | 35 | |
paul@44 | 36 | def reset(self): |
paul@44 | 37 | |
paul@44 | 38 | "To be used to reset the state of the reader or writer between records." |
paul@44 | 39 | |
paul@44 | 40 | pass |
paul@44 | 41 | |
paul@69 | 42 | def seek(self, offset): |
paul@69 | 43 | self.f.seek(offset) |
paul@69 | 44 | self.reset() |
paul@69 | 45 | |
paul@44 | 46 | def rewind(self): |
paul@52 | 47 | self.f.seek(0) |
paul@44 | 48 | self.reset() |
paul@44 | 49 | |
paul@88 | 50 | def flush(self): |
paul@88 | 51 | if self.f is not None: |
paul@88 | 52 | self.data.tofile(self.f) |
paul@88 | 53 | self.data = array('B') |
paul@88 | 54 | |
paul@44 | 55 | def close(self): |
paul@44 | 56 | if self.f is not None: |
paul@88 | 57 | self.data.tofile(self.f) |
paul@44 | 58 | self.f.close() |
paul@44 | 59 | self.f = None |
paul@44 | 60 | |
paul@74 | 61 | def get_value_size(self, value): |
paul@74 | 62 | if isinstance(value, (list, tuple)): |
paul@74 | 63 | return len(value) |
paul@74 | 64 | else: |
paul@74 | 65 | return 0 |
paul@74 | 66 | |
paul@74 | 67 | def get_initial_value(self, size): |
paul@74 | 68 | if size: |
paul@74 | 69 | return [0] * size |
paul@74 | 70 | else: |
paul@74 | 71 | return 0 |
paul@74 | 72 | |
paul@44 | 73 | class FileWriter(File): |
paul@44 | 74 | |
paul@44 | 75 | "Writing basic data types to files." |
paul@44 | 76 | |
paul@88 | 77 | def tell(self): |
paul@88 | 78 | return self.f.tell() + len(self.data) |
paul@44 | 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@88 | 84 | vint_to_array(number, self.data) |
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@88 | 91 | vint_to_array(number, self.data) |
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@88 | 124 | self.data.fromstring("".join([flag, vint(length), s])) |
paul@44 | 125 | |
paul@88 | 126 | def write_sequence(self, value, last, size, monotonic=1): |
paul@74 | 127 | if size: |
paul@74 | 128 | emit_delta = 1 |
paul@74 | 129 | for v, l in map(None, value, last)[:size]: |
paul@74 | 130 | if v is None: |
paul@74 | 131 | v = l |
paul@74 | 132 | if monotonic or emit_delta: |
paul@74 | 133 | v_out = v - l |
paul@74 | 134 | if emit_delta and v_out != 0: |
paul@74 | 135 | emit_delta = 0 |
paul@74 | 136 | else: |
paul@74 | 137 | v_out = v + 1 |
paul@88 | 138 | vint_to_array(v_out, self.data) |
paul@74 | 139 | else: |
paul@88 | 140 | vint_to_array(value - last, self.data) |
paul@74 | 141 | |
paul@74 | 142 | return value |
paul@74 | 143 | |
paul@44 | 144 | class FileReader(File): |
paul@44 | 145 | |
paul@44 | 146 | "Reading basic data types from files." |
paul@44 | 147 | |
paul@44 | 148 | def read_number(self): |
paul@44 | 149 | |
paul@44 | 150 | "Read a number from the file." |
paul@44 | 151 | |
paul@44 | 152 | # Read each byte, adding it to the number. |
paul@44 | 153 | |
paul@88 | 154 | f = self.f |
paul@88 | 155 | a = self.data |
paul@67 | 156 | fromfile = a.fromfile |
paul@44 | 157 | |
paul@88 | 158 | try: |
paul@88 | 159 | fromfile(f, 1) |
paul@88 | 160 | csd = a[-1] |
paul@88 | 161 | if csd < 128: |
paul@88 | 162 | return csd |
paul@88 | 163 | else: |
paul@88 | 164 | while csd & 128: |
paul@88 | 165 | fromfile(f, 1) |
paul@88 | 166 | csd = a[-1] |
paul@88 | 167 | return vint_from_array(self.data) |
paul@88 | 168 | finally: |
paul@88 | 169 | self.data = array('B') |
paul@44 | 170 | |
paul@44 | 171 | def read_string(self, decompress=0): |
paul@44 | 172 | |
paul@44 | 173 | """ |
paul@44 | 174 | Read a string from the file, decompressing the stored data if |
paul@44 | 175 | 'decompress' is set to a true value. |
paul@44 | 176 | """ |
paul@44 | 177 | |
paul@44 | 178 | # Decompress the data if requested. |
paul@44 | 179 | |
paul@44 | 180 | if decompress: |
paul@88 | 181 | flag = self.f.read(1) |
paul@44 | 182 | else: |
paul@44 | 183 | flag = "-" |
paul@44 | 184 | |
paul@44 | 185 | length = self.read_number() |
paul@44 | 186 | |
paul@88 | 187 | try: |
paul@88 | 188 | self.data.fromfile(self.f, length) |
paul@88 | 189 | s = self.data.tostring() |
paul@88 | 190 | |
paul@88 | 191 | # Perform decompression if applicable. |
paul@44 | 192 | |
paul@88 | 193 | if flag == "z": |
paul@88 | 194 | s = zlib.decompress(s) |
paul@88 | 195 | |
paul@88 | 196 | # Convert strings to Unicode objects. |
paul@44 | 197 | |
paul@88 | 198 | return unicode(s, "utf-8") |
paul@44 | 199 | |
paul@88 | 200 | finally: |
paul@88 | 201 | self.data = array('B') |
paul@44 | 202 | |
paul@74 | 203 | def read_sequence(self, last, size, monotonic=1): |
paul@74 | 204 | if size: |
paul@74 | 205 | value = [] |
paul@86 | 206 | if monotonic: |
paul@86 | 207 | for v in last: |
paul@86 | 208 | v_in = self.read_number() |
paul@74 | 209 | value.append(v + v_in) |
paul@86 | 210 | else: |
paul@87 | 211 | i = 0 |
paul@87 | 212 | n = len(last) |
paul@87 | 213 | value = list(last) |
paul@87 | 214 | |
paul@87 | 215 | # Traverse a copy of the last value. |
paul@87 | 216 | |
paul@87 | 217 | while i < n: |
paul@86 | 218 | v_in = self.read_number() |
paul@87 | 219 | |
paul@87 | 220 | # While zeros are read, retain the last value elements. |
paul@87 | 221 | # Otherwise, add the delta... |
paul@87 | 222 | |
paul@87 | 223 | if v_in != 0: |
paul@87 | 224 | value[i] += v_in |
paul@87 | 225 | i += 1 |
paul@87 | 226 | |
paul@87 | 227 | # Then set absolute values for the remaining elements. |
paul@87 | 228 | |
paul@87 | 229 | while i < n: |
paul@87 | 230 | value[i] = self.read_number() - 1 |
paul@87 | 231 | i += 1 |
paul@87 | 232 | break |
paul@87 | 233 | |
paul@87 | 234 | i += 1 |
paul@87 | 235 | |
paul@74 | 236 | return tuple(value) |
paul@74 | 237 | else: |
paul@74 | 238 | return last + self.read_number() |
paul@74 | 239 | |
paul@44 | 240 | # vim: tabstop=4 expandtab shiftwidth=4 |