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

Method write_file

recipes/Python/492211_dal3py/recipe-492211.py:270–353  ·  view source on GitHub ↗
(self, block, data)

Source from the content-addressed store, hash-verified

268
269 # Write A File
270 def write_file(self, block, data):
271 assert type(block) is int and 1 <= block <= self.__blocks
272 assert type(data) is str and len(data)
273 status = self.__disk.status(block)
274 assert self.STATUS.f1 <= status <= self.STATUS.fZ
275 self.__seed(data)
276 dir_map = [block]
277 if status == self.STATUS.fA:
278 temp = self.__disk.read(block)
279 block = (ord(temp[-2]) << 8) + ord(temp[-1])
280 dir_map = dir_map + [block]
281 while self.__disk.status(block) == self.STATUS.fB:
282 temp = self.__disk.read(block)
283 block = (ord(temp[-2]) << 8) + ord(temp[-1])
284 dir_map = dir_map + [block]
285 elif status == self.STATUS.fB:
286 temp = self.__disk.read(block)
287 dirA = (ord(temp[0]) << 8) + ord(temp[1])
288 dir_map = [dirA] + dir_map
289 dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
290 dir_map = dir_map + [dirZ]
291 while self.__disk.status(dirA) == self.STATUS.fB:
292 temp = self.__disk.read(dirA)
293 dirA = (ord(temp[0]) << 8) + ord(temp[1])
294 dir_map = [dirA] + dir_map
295 while self.__disk.status(dirZ) == self.STATUS.fB:
296 temp = self.__disk.read(dirZ)
297 dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
298 dir_map = dir_map + [dirZ]
299 elif status == self.STATUS.fZ:
300 temp = self.__disk.read(block)
301 block = (ord(temp[0]) << 8) + ord(temp[1])
302 dir_map = [block] + dir_map
303 while self.__disk.status(block) == self.STATUS.fB:
304 temp = self.__disk.read(block)
305 block = (ord(temp[0]) << 8) + ord(temp[1])
306 dir_map = [block] + dir_map
307 size = self.__size - 4
308 if len(data) % size:
309 blocks = len(data) / size + 1
310 else:
311 blocks = len(data) / size
312 blocks = [data[index*size:index*size+size] for index in range(blocks)]
313 while len(dir_map) > len(blocks):
314 self.__disk.close(dir_map[-1])
315 del dir_map[-1]
316 while len(dir_map) < len(blocks):
317 dir_map.append(self.__disk.open(255))
318 if len(blocks) == 1:
319 temp = len(data)
320 size = chr(temp >> 8) + chr(temp & 0xFF)
321 blocks[0] = size + blocks[0] + size
322 else:
323 temp = len(data)
324 size = chr(temp >> 8) + chr(temp & 0xFF)
325 temp = dir_map[1]
326 next = chr(temp >> 8) + chr(temp & 0xFF)
327 blocks[0] = size + blocks[0] + next

Callers

nothing calls this directly

Calls 8

rangeFunction · 0.85
statusMethod · 0.45
readMethod · 0.45
closeMethod · 0.45
appendMethod · 0.45
openMethod · 0.45
writeMethod · 0.45
editMethod · 0.45

Tested by

no test coverage detected