1 #!/usr/bin/env python 2 3 """ 4 File objects. 5 6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from __builtins__.types import check_int, check_string 23 from native import isinstance as _isinstance, fclose, fopen, fread, fwrite 24 25 class filestream: 26 27 "Generic file-oriented stream functionality." 28 29 def __init__(self, encoding=None, bufsize=1024): 30 31 "Initialise the stream with the given 'encoding' and 'bufsize'." 32 33 self.encoding = encoding 34 self.bufsize = bufsize 35 36 # Internal stream details. 37 38 self.__data__ = None 39 40 def _convert(self, bytes): 41 42 "Convert 'bytes' to text if necessary." 43 44 if self.encoding: 45 return unicode(bytes, self.encoding) 46 else: 47 return bytes 48 49 def read(self, n=0): 50 51 "Read 'n' bytes from the stream." 52 53 check_int(n) 54 55 # Read any indicated number of bytes. 56 57 if n > 0: 58 s = fread(self.__data__, n) 59 60 # Read all remaining bytes. 61 62 else: 63 l = [] 64 65 # Read until end-of-file. 66 67 try: 68 while True: 69 l.append(fread(self.__data__, self.bufsize)) 70 71 # Handle end-of-file reads. 72 73 except EOFError: 74 pass 75 76 s = "".join(l) 77 78 return self._convert(s) 79 80 def readline(self, n=0): 81 82 """ 83 Read until an end-of-line indicator is encountered or at most 'n' bytes, 84 if indicated. 85 """ 86 87 check_int(n) 88 89 # Read any indicated number of bytes. 90 91 if n > 0: 92 s = fread(self.__data__, n) 93 94 # Read until an end-of-line indicator. 95 96 else: 97 l = [] 98 99 # Read until end-of-line or end-of-file. 100 # NOTE: Only POSIX newlines are supported currently. 101 102 try: 103 while True: 104 s = fread(self.__data__, 1) 105 l.append(s) 106 107 # Where a newline has been read, provide the preceding data 108 # plus the newline indicator. 109 110 if s == "\n": 111 break 112 113 # Handle end-of-file reads. 114 115 except EOFError: 116 pass 117 118 s = "".join(l) 119 120 return self._convert(s) 121 122 def readlines(self, n=None): pass 123 124 def write(self, s): 125 126 "Write string 's' to the stream." 127 128 check_string(s) 129 130 # Encode text as bytes if necessary. When the encoding is not set, any 131 # original encoding of the text will be applied. 132 133 if _isinstance(s, utf8string): 134 s = s.encode(self.encoding) 135 136 fwrite(self.__data__, s) 137 138 def close(self): 139 140 "Close the stream." 141 142 fclose(self.__data__) 143 144 class file(filestream): 145 146 "A file abstraction." 147 148 def __init__(self, filename, mode="r", encoding=None, bufsize=1024): 149 150 """ 151 Open the file with the given 'filename' using the given access 'mode', 152 any specified 'encoding', and the given 'bufsize'. 153 """ 154 155 get_using(filestream.__init__, self)(encoding, bufsize) 156 self.__data__ = fopen(filename, mode) 157 158 # vim: tabstop=4 expandtab shiftwidth=4