paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Generic file access. |
paul@44 | 5 | |
paul@44 | 6 | Copyright (C) 2009 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@44 | 21 | from iixr.data import vint |
paul@44 | 22 | import bz2, zlib |
paul@44 | 23 | |
paul@44 | 24 | # Constants. |
paul@44 | 25 | |
paul@44 | 26 | WRITE_CACHE_SIZE = 100000 |
paul@44 | 27 | READ_CACHE_SIZE = 10000 |
paul@44 | 28 | READ_CACHE_RESIZE = 5000 |
paul@44 | 29 | |
paul@44 | 30 | compressors = [("b", bz2.compress), ("z", zlib.compress)] |
paul@44 | 31 | decompressors = {"b" : bz2.decompress, "z" : zlib.decompress} |
paul@44 | 32 | |
paul@44 | 33 | class File: |
paul@44 | 34 | |
paul@44 | 35 | "A basic file abstraction." |
paul@44 | 36 | |
paul@44 | 37 | def __init__(self, f): |
paul@44 | 38 | self.f = f |
paul@44 | 39 | self.reset() |
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@44 | 47 | def rewind(self): |
paul@44 | 48 | self.seek(0) |
paul@44 | 49 | self.reset() |
paul@44 | 50 | |
paul@44 | 51 | def seek(self, offset): |
paul@44 | 52 | |
paul@44 | 53 | "To be defined by readers." |
paul@44 | 54 | |
paul@44 | 55 | pass |
paul@44 | 56 | |
paul@44 | 57 | def flush(self): |
paul@44 | 58 | |
paul@44 | 59 | "To be defined by writers." |
paul@44 | 60 | |
paul@44 | 61 | pass |
paul@44 | 62 | |
paul@44 | 63 | def close(self): |
paul@44 | 64 | if self.f is not None: |
paul@44 | 65 | self.flush() |
paul@44 | 66 | self.f.close() |
paul@44 | 67 | self.f = None |
paul@44 | 68 | |
paul@44 | 69 | class FileWriter(File): |
paul@44 | 70 | |
paul@44 | 71 | "Writing basic data types to files." |
paul@44 | 72 | |
paul@44 | 73 | def __init__(self, f): |
paul@44 | 74 | File.__init__(self, f) |
paul@44 | 75 | self.cache = [] |
paul@44 | 76 | self.cache_length = 0 |
paul@44 | 77 | |
paul@44 | 78 | def write_number(self, number): |
paul@44 | 79 | |
paul@44 | 80 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 81 | |
paul@44 | 82 | self.write(vint(number)) |
paul@44 | 83 | |
paul@44 | 84 | def write_string(self, s, compress=0): |
paul@44 | 85 | |
paul@44 | 86 | """ |
paul@44 | 87 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 88 | if 'compress' is set to a true value. |
paul@44 | 89 | """ |
paul@44 | 90 | |
paul@44 | 91 | # Convert Unicode objects to strings. |
paul@44 | 92 | |
paul@44 | 93 | if isinstance(s, unicode): |
paul@44 | 94 | s = s.encode("utf-8") |
paul@44 | 95 | |
paul@44 | 96 | # Compress the string if requested. |
paul@44 | 97 | |
paul@44 | 98 | if compress: |
paul@44 | 99 | for flag, fn in compressors: |
paul@44 | 100 | cs = fn(s) |
paul@44 | 101 | |
paul@44 | 102 | # Take the first string shorter than the original. |
paul@44 | 103 | |
paul@44 | 104 | if len(cs) < len(s): |
paul@44 | 105 | s = cs |
paul@44 | 106 | break |
paul@44 | 107 | else: |
paul@44 | 108 | flag = "-" |
paul@44 | 109 | |
paul@44 | 110 | else: |
paul@44 | 111 | flag = "" |
paul@44 | 112 | |
paul@44 | 113 | # Write the length of the data before the data itself. |
paul@44 | 114 | |
paul@44 | 115 | length = len(s) |
paul@44 | 116 | self.write(flag + vint(length) + s) |
paul@44 | 117 | |
paul@44 | 118 | # Cache-affected methods. |
paul@44 | 119 | |
paul@44 | 120 | def write(self, s): |
paul@44 | 121 | self.cache.append(s) |
paul@44 | 122 | self.cache_length += len(s) |
paul@44 | 123 | if self.cache_length >= WRITE_CACHE_SIZE: |
paul@44 | 124 | self.flush() |
paul@44 | 125 | |
paul@44 | 126 | def tell(self): |
paul@44 | 127 | return self.f.tell() + self.cache_length |
paul@44 | 128 | |
paul@44 | 129 | def flush(self): |
paul@44 | 130 | self.f.write("".join(self.cache)) |
paul@44 | 131 | self.cache = [] |
paul@44 | 132 | self.cache_length = 0 |
paul@44 | 133 | |
paul@44 | 134 | class FileReader(File): |
paul@44 | 135 | |
paul@44 | 136 | "Reading basic data types from files." |
paul@44 | 137 | |
paul@44 | 138 | def __init__(self, f): |
paul@44 | 139 | File.__init__(self, f) |
paul@46 | 140 | self.reset_cache(0) |
paul@44 | 141 | |
paul@46 | 142 | def reset_cache(self, offset): |
paul@44 | 143 | self.cache = "" |
paul@44 | 144 | self.cache_length = 0 |
paul@44 | 145 | self.cache_start = 0 |
paul@46 | 146 | self.cache_offset = offset |
paul@46 | 147 | self.f.seek(offset) |
paul@46 | 148 | |
paul@46 | 149 | def resize_cache(self, next_start): |
paul@46 | 150 | self.cache = self.cache[next_start:] |
paul@46 | 151 | self.cache_length = len(self.cache) |
paul@46 | 152 | self.cache_start = 0 |
paul@46 | 153 | self.cache_offset += next_start |
paul@44 | 154 | |
paul@44 | 155 | def read_number(self): |
paul@44 | 156 | |
paul@44 | 157 | "Read a number from the file." |
paul@44 | 158 | |
paul@44 | 159 | # Read each byte, adding it to the number. |
paul@44 | 160 | |
paul@44 | 161 | shift = 0 |
paul@44 | 162 | number = 0 |
paul@44 | 163 | read = self.read |
paul@44 | 164 | |
paul@44 | 165 | try: |
paul@44 | 166 | csd = ord(read(1)) |
paul@44 | 167 | while csd & 128: |
paul@44 | 168 | number += ((csd & 127) << shift) |
paul@44 | 169 | shift += 7 |
paul@44 | 170 | csd = ord(read(1)) |
paul@44 | 171 | else: |
paul@44 | 172 | number += (csd << shift) |
paul@44 | 173 | except TypeError: |
paul@44 | 174 | raise EOFError |
paul@44 | 175 | |
paul@44 | 176 | return number |
paul@44 | 177 | |
paul@44 | 178 | def read_string(self, decompress=0): |
paul@44 | 179 | |
paul@44 | 180 | """ |
paul@44 | 181 | Read a string from the file, decompressing the stored data if |
paul@44 | 182 | 'decompress' is set to a true value. |
paul@44 | 183 | """ |
paul@44 | 184 | |
paul@44 | 185 | # Decompress the data if requested. |
paul@44 | 186 | |
paul@44 | 187 | if decompress: |
paul@44 | 188 | flag = self.read(1) |
paul@44 | 189 | else: |
paul@44 | 190 | flag = "-" |
paul@44 | 191 | |
paul@44 | 192 | length = self.read_number() |
paul@44 | 193 | s = self.read(length) |
paul@44 | 194 | |
paul@44 | 195 | # Perform decompression if applicable. |
paul@44 | 196 | |
paul@44 | 197 | if flag != "-": |
paul@44 | 198 | fn = decompressors[flag] |
paul@44 | 199 | s = fn(s) |
paul@44 | 200 | |
paul@44 | 201 | # Convert strings to Unicode objects. |
paul@44 | 202 | |
paul@44 | 203 | return unicode(s, "utf-8") |
paul@44 | 204 | |
paul@44 | 205 | # Cache-affected methods. |
paul@44 | 206 | |
paul@44 | 207 | def read(self, n): |
paul@44 | 208 | needed = n - (self.cache_length - self.cache_start) |
paul@44 | 209 | |
paul@44 | 210 | # Read the needed number of characters, if possible. |
paul@44 | 211 | |
paul@44 | 212 | if needed > 0: |
paul@44 | 213 | s = self.f.read(max(needed, READ_CACHE_SIZE)) |
paul@44 | 214 | self.cache += s |
paul@44 | 215 | self.cache_length += len(s) |
paul@44 | 216 | |
paul@44 | 217 | # Get the end of the requested block. |
paul@44 | 218 | |
paul@44 | 219 | next_start = self.cache_start + n |
paul@44 | 220 | s = self.cache[self.cache_start:next_start] |
paul@44 | 221 | |
paul@44 | 222 | # Reposition the pointer to the cache. |
paul@44 | 223 | |
paul@44 | 224 | self._seek_cache(len(s)) |
paul@44 | 225 | return s |
paul@44 | 226 | |
paul@44 | 227 | def tell(self): |
paul@46 | 228 | return self.cache_offset + self.cache_start |
paul@44 | 229 | |
paul@44 | 230 | def seek(self, offset): |
paul@44 | 231 | current = self.tell() |
paul@44 | 232 | |
paul@44 | 233 | # If seeking forward, attempt to navigate the cache. |
paul@44 | 234 | |
paul@44 | 235 | if offset >= current: |
paul@44 | 236 | self._seek_cache(offset - current) |
paul@44 | 237 | else: |
paul@46 | 238 | self.reset_cache(offset) |
paul@44 | 239 | |
paul@44 | 240 | def _seek_cache(self, delta): |
paul@44 | 241 | next_start = self.cache_start + delta |
paul@44 | 242 | |
paul@44 | 243 | if next_start > 0 and next_start >= len(self.cache): |
paul@46 | 244 | self.reset_cache(self.cache_offset + next_start) |
paul@44 | 245 | |
paul@44 | 246 | # If the cache is too big, resize it. |
paul@44 | 247 | |
paul@44 | 248 | elif next_start > READ_CACHE_RESIZE: |
paul@46 | 249 | self.resize_cache(next_start) |
paul@44 | 250 | |
paul@44 | 251 | # Otherwise, just reference the next part of the cache. |
paul@44 | 252 | |
paul@44 | 253 | else: |
paul@44 | 254 | self.cache_start = next_start |
paul@44 | 255 | |
paul@44 | 256 | class FileOpener: |
paul@44 | 257 | |
paul@44 | 258 | "Opening files using their filenames." |
paul@44 | 259 | |
paul@44 | 260 | def __init__(self, filename): |
paul@44 | 261 | self.filename = filename |
paul@44 | 262 | |
paul@44 | 263 | def open(self, mode): |
paul@44 | 264 | return open(self.filename, mode) |
paul@44 | 265 | |
paul@44 | 266 | def close(self): |
paul@44 | 267 | pass |
paul@44 | 268 | |
paul@44 | 269 | # vim: tabstop=4 expandtab shiftwidth=4 |