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@50 | 22 | import zlib |
paul@44 | 23 | |
paul@44 | 24 | # Constants. |
paul@44 | 25 | |
paul@44 | 26 | class File: |
paul@44 | 27 | |
paul@44 | 28 | "A basic file abstraction." |
paul@44 | 29 | |
paul@44 | 30 | def __init__(self, f): |
paul@44 | 31 | self.f = f |
paul@44 | 32 | self.reset() |
paul@44 | 33 | |
paul@44 | 34 | def reset(self): |
paul@44 | 35 | |
paul@44 | 36 | "To be used to reset the state of the reader or writer between records." |
paul@44 | 37 | |
paul@44 | 38 | pass |
paul@44 | 39 | |
paul@44 | 40 | def rewind(self): |
paul@44 | 41 | self.seek(0) |
paul@44 | 42 | self.reset() |
paul@44 | 43 | |
paul@44 | 44 | def seek(self, offset): |
paul@44 | 45 | |
paul@44 | 46 | "To be defined by readers." |
paul@44 | 47 | |
paul@44 | 48 | pass |
paul@44 | 49 | |
paul@44 | 50 | def flush(self): |
paul@44 | 51 | |
paul@44 | 52 | "To be defined by writers." |
paul@44 | 53 | |
paul@44 | 54 | pass |
paul@44 | 55 | |
paul@44 | 56 | def close(self): |
paul@44 | 57 | if self.f is not None: |
paul@44 | 58 | self.flush() |
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@44 | 66 | def __init__(self, f): |
paul@44 | 67 | File.__init__(self, f) |
paul@44 | 68 | |
paul@44 | 69 | def write_number(self, number): |
paul@44 | 70 | |
paul@44 | 71 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 72 | |
paul@44 | 73 | self.write(vint(number)) |
paul@44 | 74 | |
paul@44 | 75 | def write_string(self, s, compress=0): |
paul@44 | 76 | |
paul@44 | 77 | """ |
paul@44 | 78 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 79 | if 'compress' is set to a true value. |
paul@44 | 80 | """ |
paul@44 | 81 | |
paul@44 | 82 | # Convert Unicode objects to strings. |
paul@44 | 83 | |
paul@44 | 84 | if isinstance(s, unicode): |
paul@44 | 85 | s = s.encode("utf-8") |
paul@44 | 86 | |
paul@44 | 87 | # Compress the string if requested. |
paul@44 | 88 | |
paul@44 | 89 | if compress: |
paul@50 | 90 | cs = zlib.compress(s) |
paul@50 | 91 | |
paul@50 | 92 | # Take any shorter than the original. |
paul@44 | 93 | |
paul@50 | 94 | if len(cs) < len(s): |
paul@50 | 95 | flag = "z" |
paul@50 | 96 | s = cs |
paul@44 | 97 | else: |
paul@44 | 98 | flag = "-" |
paul@44 | 99 | |
paul@44 | 100 | else: |
paul@44 | 101 | flag = "" |
paul@44 | 102 | |
paul@44 | 103 | # Write the length of the data before the data itself. |
paul@44 | 104 | |
paul@44 | 105 | length = len(s) |
paul@44 | 106 | self.write(flag + vint(length) + s) |
paul@44 | 107 | |
paul@44 | 108 | # Cache-affected methods. |
paul@44 | 109 | |
paul@44 | 110 | def write(self, s): |
paul@50 | 111 | self.f.write(s) |
paul@44 | 112 | |
paul@44 | 113 | def tell(self): |
paul@50 | 114 | return self.f.tell() |
paul@44 | 115 | |
paul@44 | 116 | class FileReader(File): |
paul@44 | 117 | |
paul@44 | 118 | "Reading basic data types from files." |
paul@44 | 119 | |
paul@44 | 120 | def __init__(self, f): |
paul@44 | 121 | File.__init__(self, f) |
paul@44 | 122 | |
paul@44 | 123 | def read_number(self): |
paul@44 | 124 | |
paul@44 | 125 | "Read a number from the file." |
paul@44 | 126 | |
paul@44 | 127 | # Read each byte, adding it to the number. |
paul@44 | 128 | |
paul@44 | 129 | shift = 0 |
paul@44 | 130 | number = 0 |
paul@44 | 131 | read = self.read |
paul@44 | 132 | |
paul@44 | 133 | try: |
paul@44 | 134 | csd = ord(read(1)) |
paul@44 | 135 | while csd & 128: |
paul@44 | 136 | number += ((csd & 127) << shift) |
paul@44 | 137 | shift += 7 |
paul@44 | 138 | csd = ord(read(1)) |
paul@44 | 139 | else: |
paul@44 | 140 | number += (csd << shift) |
paul@44 | 141 | except TypeError: |
paul@44 | 142 | raise EOFError |
paul@44 | 143 | |
paul@44 | 144 | return number |
paul@44 | 145 | |
paul@44 | 146 | def read_string(self, decompress=0): |
paul@44 | 147 | |
paul@44 | 148 | """ |
paul@44 | 149 | Read a string from the file, decompressing the stored data if |
paul@44 | 150 | 'decompress' is set to a true value. |
paul@44 | 151 | """ |
paul@44 | 152 | |
paul@44 | 153 | # Decompress the data if requested. |
paul@44 | 154 | |
paul@44 | 155 | if decompress: |
paul@44 | 156 | flag = self.read(1) |
paul@44 | 157 | else: |
paul@44 | 158 | flag = "-" |
paul@44 | 159 | |
paul@44 | 160 | length = self.read_number() |
paul@44 | 161 | s = self.read(length) |
paul@44 | 162 | |
paul@44 | 163 | # Perform decompression if applicable. |
paul@44 | 164 | |
paul@50 | 165 | if flag == "z": |
paul@50 | 166 | s = zlib.decompress(s) |
paul@44 | 167 | |
paul@44 | 168 | # Convert strings to Unicode objects. |
paul@44 | 169 | |
paul@44 | 170 | return unicode(s, "utf-8") |
paul@44 | 171 | |
paul@44 | 172 | # Cache-affected methods. |
paul@44 | 173 | |
paul@44 | 174 | def read(self, n): |
paul@50 | 175 | return self.f.read(n) |
paul@44 | 176 | |
paul@44 | 177 | def tell(self): |
paul@50 | 178 | return self.f.tell() |
paul@44 | 179 | |
paul@44 | 180 | def seek(self, offset): |
paul@50 | 181 | self.f.seek(offset) |
paul@44 | 182 | |
paul@44 | 183 | class FileOpener: |
paul@44 | 184 | |
paul@44 | 185 | "Opening files using their filenames." |
paul@44 | 186 | |
paul@44 | 187 | def __init__(self, filename): |
paul@44 | 188 | self.filename = filename |
paul@44 | 189 | |
paul@44 | 190 | def open(self, mode): |
paul@44 | 191 | return open(self.filename, mode) |
paul@44 | 192 | |
paul@44 | 193 | def close(self): |
paul@44 | 194 | pass |
paul@44 | 195 | |
paul@44 | 196 | # vim: tabstop=4 expandtab shiftwidth=4 |