##// END OF EJS Templates
util: add limit to amount filechunkiter will read
Vadim Gelfer -
r2462:d610bcfd default
parent child Browse files
Show More
@@ -822,16 +822,22 b' class chunkbuffer(object):'
822 s, self.buf = self.buf[:l], buffer(self.buf, l)
822 s, self.buf = self.buf[:l], buffer(self.buf, l)
823 return s
823 return s
824
824
825 def filechunkiter(f, size = 65536):
825 def filechunkiter(f, size=65536, limit=None):
826 """Create a generator that produces all the data in the file size
826 """Create a generator that produces the data in the file size
827 (default 65536) bytes at a time. Chunks may be less than size
827 (default 65536) bytes at a time, up to optional limit (default is
828 bytes if the chunk is the last chunk in the file, or the file is a
828 to read all data). Chunks may be less than size bytes if the
829 socket or some other type of file that sometimes reads less data
829 chunk is the last chunk in the file, or the file is a socket or
830 than is requested."""
830 some other type of file that sometimes reads less data than is
831 s = f.read(size)
831 requested."""
832 while len(s) > 0:
832 assert size >= 0
833 assert limit is None or limit >= 0
834 while True:
835 if limit is None: nbytes = size
836 else: nbytes = min(limit, size)
837 s = nbytes and f.read(nbytes)
838 if not s: break
839 if limit: limit -= len(s)
833 yield s
840 yield s
834 s = f.read(size)
835
841
836 def makedate():
842 def makedate():
837 lt = time.localtime()
843 lt = time.localtime()
General Comments 0
You need to be logged in to leave comments. Login now