comparison mercurial/util.py @ 2462:d610bcfd66a8

util: add limit to amount filechunkiter will read
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Tue, 20 Jun 2006 15:13:17 -0700
parents b77a2ef61b81
children 6904e1ef8ad1
comparison
equal deleted inserted replaced
2461:ff9ee834e3b6 2462:d610bcfd66a8
820 self.iterempty = True 820 self.iterempty = True
821 self.buf = collector.getvalue() 821 self.buf = collector.getvalue()
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()
838 if lt[8] == 1 and time.daylight: 844 if lt[8] == 1 and time.daylight:
839 tz = time.altzone 845 tz = time.altzone