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@52 | 41 | self.f.seek(0) |
paul@44 | 42 | self.reset() |
paul@44 | 43 | |
paul@44 | 44 | def close(self): |
paul@44 | 45 | if self.f is not None: |
paul@44 | 46 | self.f.close() |
paul@44 | 47 | self.f = None |
paul@44 | 48 | |
paul@44 | 49 | class FileWriter(File): |
paul@44 | 50 | |
paul@44 | 51 | "Writing basic data types to files." |
paul@44 | 52 | |
paul@44 | 53 | def __init__(self, f): |
paul@44 | 54 | File.__init__(self, f) |
paul@44 | 55 | |
paul@44 | 56 | def write_number(self, number): |
paul@44 | 57 | |
paul@44 | 58 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 59 | |
paul@52 | 60 | self.f.write(vint(number)) |
paul@44 | 61 | |
paul@44 | 62 | def write_string(self, s, compress=0): |
paul@44 | 63 | |
paul@44 | 64 | """ |
paul@44 | 65 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 66 | if 'compress' is set to a true value. |
paul@44 | 67 | """ |
paul@44 | 68 | |
paul@44 | 69 | # Convert Unicode objects to strings. |
paul@44 | 70 | |
paul@44 | 71 | if isinstance(s, unicode): |
paul@44 | 72 | s = s.encode("utf-8") |
paul@44 | 73 | |
paul@44 | 74 | # Compress the string if requested. |
paul@44 | 75 | |
paul@44 | 76 | if compress: |
paul@50 | 77 | cs = zlib.compress(s) |
paul@50 | 78 | |
paul@50 | 79 | # Take any shorter than the original. |
paul@44 | 80 | |
paul@50 | 81 | if len(cs) < len(s): |
paul@50 | 82 | flag = "z" |
paul@50 | 83 | s = cs |
paul@44 | 84 | else: |
paul@44 | 85 | flag = "-" |
paul@44 | 86 | |
paul@44 | 87 | else: |
paul@44 | 88 | flag = "" |
paul@44 | 89 | |
paul@44 | 90 | # Write the length of the data before the data itself. |
paul@44 | 91 | |
paul@44 | 92 | length = len(s) |
paul@52 | 93 | self.f.write("".join([flag, vint(length), s])) |
paul@44 | 94 | |
paul@44 | 95 | class FileReader(File): |
paul@44 | 96 | |
paul@44 | 97 | "Reading basic data types from files." |
paul@44 | 98 | |
paul@44 | 99 | def __init__(self, f): |
paul@44 | 100 | File.__init__(self, f) |
paul@44 | 101 | |
paul@44 | 102 | def read_number(self): |
paul@44 | 103 | |
paul@44 | 104 | "Read a number from the file." |
paul@44 | 105 | |
paul@44 | 106 | # Read each byte, adding it to the number. |
paul@44 | 107 | |
paul@44 | 108 | shift = 0 |
paul@44 | 109 | number = 0 |
paul@52 | 110 | read = self.f.read |
paul@44 | 111 | |
paul@44 | 112 | try: |
paul@44 | 113 | csd = ord(read(1)) |
paul@44 | 114 | while csd & 128: |
paul@44 | 115 | number += ((csd & 127) << shift) |
paul@44 | 116 | shift += 7 |
paul@44 | 117 | csd = ord(read(1)) |
paul@44 | 118 | else: |
paul@44 | 119 | number += (csd << shift) |
paul@44 | 120 | except TypeError: |
paul@44 | 121 | raise EOFError |
paul@44 | 122 | |
paul@44 | 123 | return number |
paul@44 | 124 | |
paul@44 | 125 | def read_string(self, decompress=0): |
paul@44 | 126 | |
paul@44 | 127 | """ |
paul@44 | 128 | Read a string from the file, decompressing the stored data if |
paul@44 | 129 | 'decompress' is set to a true value. |
paul@44 | 130 | """ |
paul@44 | 131 | |
paul@52 | 132 | read = self.f.read |
paul@52 | 133 | |
paul@44 | 134 | # Decompress the data if requested. |
paul@44 | 135 | |
paul@44 | 136 | if decompress: |
paul@52 | 137 | flag = read(1) |
paul@44 | 138 | else: |
paul@44 | 139 | flag = "-" |
paul@44 | 140 | |
paul@44 | 141 | length = self.read_number() |
paul@52 | 142 | s = read(length) |
paul@44 | 143 | |
paul@44 | 144 | # Perform decompression if applicable. |
paul@44 | 145 | |
paul@50 | 146 | if flag == "z": |
paul@50 | 147 | s = zlib.decompress(s) |
paul@44 | 148 | |
paul@44 | 149 | # Convert strings to Unicode objects. |
paul@44 | 150 | |
paul@44 | 151 | return unicode(s, "utf-8") |
paul@44 | 152 | |
paul@44 | 153 | class FileOpener: |
paul@44 | 154 | |
paul@44 | 155 | "Opening files using their filenames." |
paul@44 | 156 | |
paul@44 | 157 | def __init__(self, filename): |
paul@44 | 158 | self.filename = filename |
paul@44 | 159 | |
paul@44 | 160 | def open(self, mode): |
paul@44 | 161 | return open(self.filename, mode) |
paul@44 | 162 | |
paul@44 | 163 | def close(self): |
paul@44 | 164 | pass |
paul@44 | 165 | |
paul@44 | 166 | # vim: tabstop=4 expandtab shiftwidth=4 |