# Written by bram cohen # see license.txt for license information
# File Name: Storage.py # Readword Diary: 2004-9-9 # I: ZFive5 (Drone Humble [Lonely Boat ") # # 分析 雪]]]]))) 数据 数据 数据 数据 数据 数据 数据 数据 数据 数据the type #
From Sha Import Sha from Bisect IMPORT BISECT_RIGHT
# 数据 数据 操作 操作类 Class Storage: # Constructor DEF __INIT __ (Self, Files, Open, EXISTS, GETSIZE): # can ras ioerror and valueerror self.ranges = [] Total = 0L so_far = 0L # here initialization RANGE list, here File name, file in total download size, for example (10-1000) for File, Length in files: if longth! = 0: Self.ranges.Append (Total, Total length, file) Total = Length if EXISTS (file): l = getSize (file) if l> length: l = length so_far = l Elif NOTSTS (file): # If this file size is zero generated an empty file. Open (File, 'WB'). Close () # Start File Size List Self.Begins = [i [0] for i in self.ranges] self.total_length = Total Self.Handles = {} self.WHandles = {} Self.WHandles = {} .tops = {} # assignments for file handles for File, Length in files: if exissrs: # If the file is not downloaded, Open files with additional ways L = getsize (file) if L! = Length: self.handles [file] = open (file, 'RB ') # 写 记 记 标 Self.wHandles [file] = 1 if l> length: # 强制 文件 大 l l... 打 打 文件 文件 文件 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 文件 文件 方式 方式 文件 文件 方式 文件 文件 文件 文件 文件 文件 文件 文件 文件 s = l else: # If the file does not exist, create and open the file self.handles [file] = Open (file, 'wb "
) Self.wHandles [file] = 1 # Decision space has been assigned DEF WAS_PREALLOCATED (Self, Pos, Length): for File, Begin, End In self._intervals (POS, Length): if Self.tops.get (File, 0) # Setting the file to read_readonly (self): # May raise orerror or osrror for file in self.whandles.keys (): OLD = Self.handles [file] Old.flush () Old.close () Self. Handles [file] = Open (file, 'rb') # Get the total download size Def get_total_length (self): Return Self.Total_Length # Get all the inclusive files of POS-> Amount, the stand position Def _intervals (Self, Pos, Amount): r = [] stop = POS AMOUNT P = Bisect_Right (Self.Begins, POS) - 1 While P # 从 数据 数据 d 数据 d,,,,,,,,,:,,:::::::::::::::::::::::::::::: ] H.seek (POS) R.Append (H.Read (End (End - POS)) Return '' '.join (R) # # 数据 数据 数据 d (s) in the space in the total data to the stand position range (Self, Pos, S): # might raise an orerortal = 0 for file, begin, end in self._intervals (Pos, Len (s) : If not self.whandles.has_key (file): self.handles [file] .close () self.handles [file] = Open (file, 'RB ') Self.WHandles [file] = 1 h = Self. Handles [file] h.seek (begin) h.write (s [Total: Total end - begin]) Total = End - Begin # Close all file handles Def Close (Self): for h in self.handles.Values (): H.close () # below is a bunch of test functions, here reminds me of test-drive development, about it in the New Year, # now in this way applied in an open source software, Start learning to use it in your own procedure ... # (to be continued)