paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Generic file access. |
paul@44 | 5 | |
paul@69 | 6 | Copyright (C) 2009, 2010 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@74 | 21 | from iixr.data import vint, vint_to_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@44 | 33 | self.reset() |
paul@44 | 34 | |
paul@44 | 35 | def reset(self): |
paul@44 | 36 | |
paul@44 | 37 | "To be used to reset the state of the reader or writer between records." |
paul@44 | 38 | |
paul@44 | 39 | pass |
paul@44 | 40 | |
paul@69 | 41 | def seek(self, offset): |
paul@69 | 42 | self.f.seek(offset) |
paul@69 | 43 | self.reset() |
paul@69 | 44 | |
paul@44 | 45 | def rewind(self): |
paul@52 | 46 | self.f.seek(0) |
paul@44 | 47 | self.reset() |
paul@44 | 48 | |
paul@44 | 49 | def close(self): |
paul@44 | 50 | if self.f is not None: |
paul@44 | 51 | self.f.close() |
paul@44 | 52 | self.f = None |
paul@44 | 53 | |
paul@74 | 54 | def get_value_size(self, value): |
paul@74 | 55 | if isinstance(value, (list, tuple)): |
paul@74 | 56 | return len(value) |
paul@74 | 57 | else: |
paul@74 | 58 | return 0 |
paul@74 | 59 | |
paul@74 | 60 | def get_initial_value(self, size): |
paul@74 | 61 | if size: |
paul@74 | 62 | return [0] * size |
paul@74 | 63 | else: |
paul@74 | 64 | return 0 |
paul@74 | 65 | |
paul@44 | 66 | class FileWriter(File): |
paul@44 | 67 | |
paul@44 | 68 | "Writing basic data types to files." |
paul@44 | 69 | |
paul@44 | 70 | def __init__(self, f): |
paul@44 | 71 | File.__init__(self, f) |
paul@44 | 72 | |
paul@44 | 73 | def write_number(self, number): |
paul@44 | 74 | |
paul@44 | 75 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 76 | |
paul@52 | 77 | self.f.write(vint(number)) |
paul@44 | 78 | |
paul@44 | 79 | def write_string(self, s, compress=0): |
paul@44 | 80 | |
paul@44 | 81 | """ |
paul@44 | 82 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 83 | if 'compress' is set to a true value. |
paul@44 | 84 | """ |
paul@44 | 85 | |
paul@44 | 86 | # Convert Unicode objects to strings. |
paul@44 | 87 | |
paul@44 | 88 | if isinstance(s, unicode): |
paul@44 | 89 | s = s.encode("utf-8") |
paul@44 | 90 | |
paul@44 | 91 | # Compress the string if requested. |
paul@44 | 92 | |
paul@44 | 93 | if compress: |
paul@50 | 94 | cs = zlib.compress(s) |
paul@50 | 95 | |
paul@50 | 96 | # Take any shorter than the original. |
paul@44 | 97 | |
paul@50 | 98 | if len(cs) < len(s): |
paul@50 | 99 | flag = "z" |
paul@50 | 100 | s = cs |
paul@44 | 101 | else: |
paul@44 | 102 | flag = "-" |
paul@44 | 103 | |
paul@44 | 104 | else: |
paul@44 | 105 | flag = "" |
paul@44 | 106 | |
paul@44 | 107 | # Write the length of the data before the data itself. |
paul@44 | 108 | |
paul@44 | 109 | length = len(s) |
paul@52 | 110 | self.f.write("".join([flag, vint(length), s])) |
paul@44 | 111 | |
paul@74 | 112 | def write_sequence(self, output, value, last, size, monotonic=1): |
paul@74 | 113 | if size: |
paul@74 | 114 | emit_delta = 1 |
paul@74 | 115 | for v, l in map(None, value, last)[:size]: |
paul@74 | 116 | if v is None: |
paul@74 | 117 | v = l |
paul@74 | 118 | if monotonic or emit_delta: |
paul@74 | 119 | v_out = v - l |
paul@74 | 120 | if emit_delta and v_out != 0: |
paul@74 | 121 | emit_delta = 0 |
paul@74 | 122 | else: |
paul@74 | 123 | v_out = v + 1 |
paul@74 | 124 | vint_to_array(v_out, output) |
paul@74 | 125 | else: |
paul@74 | 126 | vint_to_array(value - last, output) |
paul@74 | 127 | |
paul@74 | 128 | return value |
paul@74 | 129 | |
paul@44 | 130 | class FileReader(File): |
paul@44 | 131 | |
paul@44 | 132 | "Reading basic data types from files." |
paul@44 | 133 | |
paul@44 | 134 | def __init__(self, f): |
paul@44 | 135 | File.__init__(self, f) |
paul@44 | 136 | |
paul@44 | 137 | def read_number(self): |
paul@44 | 138 | |
paul@44 | 139 | "Read a number from the file." |
paul@44 | 140 | |
paul@44 | 141 | # Read each byte, adding it to the number. |
paul@44 | 142 | |
paul@67 | 143 | a = array('B') |
paul@67 | 144 | fromfile = a.fromfile |
paul@67 | 145 | f = self.f |
paul@44 | 146 | |
paul@67 | 147 | fromfile(f, 1) |
paul@67 | 148 | csd = a[-1] |
paul@67 | 149 | if csd < 128: |
paul@67 | 150 | return csd |
paul@55 | 151 | else: |
paul@67 | 152 | while csd & 128: |
paul@67 | 153 | fromfile(f, 1) |
paul@67 | 154 | csd = a[-1] |
paul@67 | 155 | return sum([((csd & 127) << (number * 7)) for (number, csd) in enumerate(a)]) |
paul@44 | 156 | |
paul@44 | 157 | def read_string(self, decompress=0): |
paul@44 | 158 | |
paul@44 | 159 | """ |
paul@44 | 160 | Read a string from the file, decompressing the stored data if |
paul@44 | 161 | 'decompress' is set to a true value. |
paul@44 | 162 | """ |
paul@44 | 163 | |
paul@52 | 164 | read = self.f.read |
paul@52 | 165 | |
paul@44 | 166 | # Decompress the data if requested. |
paul@44 | 167 | |
paul@44 | 168 | if decompress: |
paul@52 | 169 | flag = read(1) |
paul@44 | 170 | else: |
paul@44 | 171 | flag = "-" |
paul@44 | 172 | |
paul@44 | 173 | length = self.read_number() |
paul@52 | 174 | s = read(length) |
paul@44 | 175 | |
paul@44 | 176 | # Perform decompression if applicable. |
paul@44 | 177 | |
paul@50 | 178 | if flag == "z": |
paul@50 | 179 | s = zlib.decompress(s) |
paul@44 | 180 | |
paul@44 | 181 | # Convert strings to Unicode objects. |
paul@44 | 182 | |
paul@44 | 183 | return unicode(s, "utf-8") |
paul@44 | 184 | |
paul@74 | 185 | def read_sequence(self, last, size, monotonic=1): |
paul@74 | 186 | if size: |
paul@74 | 187 | expect_delta = 1 |
paul@74 | 188 | value = [] |
paul@74 | 189 | for v in last: |
paul@74 | 190 | v_in = self.read_number() |
paul@74 | 191 | if monotonic or expect_delta: |
paul@74 | 192 | value.append(v + v_in) |
paul@74 | 193 | if expect_delta and v_in != 0: |
paul@74 | 194 | expect_delta = 0 |
paul@74 | 195 | else: |
paul@74 | 196 | value.append(v_in - 1) |
paul@74 | 197 | return tuple(value) |
paul@74 | 198 | else: |
paul@74 | 199 | return last + self.read_number() |
paul@74 | 200 | |
paul@44 | 201 | # vim: tabstop=4 expandtab shiftwidth=4 |