MCPcopy Create free account
hub / github.com/ActiveState/code / File

Class File

recipes/Python/492214_dal6py/recipe-492214.py:152–269  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

150################################################################################
151
152class File:
153
154 # MODES
155 READ = 'r'
156 WRITE = 'w'
157 APPEND = 'a'
158
159 # File Accessor Object
160 def __init__(self, disk_object, path, mode):
161 assert disk_object.__class__ is DAL5
162 assert type(path) is str and path
163 assert mode == self.READ or mode == self.WRITE or mode == self.APPEND
164 self.__disk = disk_object
165 if self.__disk.exists(path):
166 assert self.__disk.is_file(path)
167 else:
168 assert mode == self.WRITE
169 self.__disk.make_file(path)
170 parts = path.split(self.__disk.PATH_SEPARATOR)
171 self.closed = self.__closed = False
172 self.mode = self.__mode = mode
173 self.name = self.__name = parts[-1]
174 self.path = self.__path = path
175 if mode == self.WRITE:
176 self.__stream = ''
177 else:
178 self.__stream = self.__disk.read_file(path)
179 if mode == self.APPEND:
180 self.__pointer = len(self.__stream)
181 else:
182 self.__pointer = 0
183
184 # Permanently Close File
185 def close(self, force):
186 assert not self.__closed
187 success = True
188 if self.__disk.exists(self.__path):
189 self.__disk.write_file(self.__path, self.__stream)
190 else:
191 if force:
192 try:
193 self.__disk.make_file(self.__path)
194 self.__disk.write_file(self.__path, self.__stream)
195 except:
196 success = False
197 else:
198 success = False
199 self.closed = self.__closed = True
200 return success
201
202 # Read From File
203 def read(self, size=None):
204 assert not self.__closed
205 assert self.__mode == self.READ
206 if size is None:
207 size = (2 ** 31) - 1
208 else:
209 assert type(size) is int and size > 0

Callers 1

fileMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected