vendor/tramline

changeset 7:d17bf871c609 gracinet-progress-bar

A new branch to implement progress bars with tramline
author gracinet
date Sat, 21 Feb 2009 17:30:54 +0000
parents dee3a43cd538
children b97646337e10
files src/tramline/core.py
diffstat 1 files changed, 19 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/src/tramline/core.py
     1.2 +++ b/src/tramline/core.py
     1.3 @@ -11,6 +11,7 @@
     1.4        s = filter.read()
     1.5        while s:
     1.6            filter.write(s)
     1.7 +          filter.flush()
     1.8            s = filter.read()
     1.9      except IOError:
    1.10         s = None 
    1.11 @@ -139,7 +140,6 @@
    1.12      # we're done if we're in a subrequset
    1.13      if filter.req.main is not None:
    1.14          pass_on(filter)
    1.15 -        filter.flush()
    1.16          return
    1.17  
    1.18      # in case of post request, we may need to do a commit/abort
    1.19 @@ -154,7 +154,6 @@
    1.20          return
    1.21      
    1.22      pass_on(filter)
    1.23 -    filter.flush()
    1.24      
    1.25  def outputfilter_post(filter):
    1.26      # get id
    1.27 @@ -163,7 +162,6 @@
    1.28      if id is None:
    1.29          # we're done now, just pass along data
    1.30          pass_on(filter)
    1.31 -        filter.flush()
    1.32          return
    1.33  
    1.34      # reuse existing processor instance based on id
    1.35 @@ -185,13 +183,11 @@
    1.36  
    1.37      # now pass along the data.
    1.38      pass_on(filter)
    1.39 -    filter.flush()
    1.40  
    1.41  def outputfilter_get(filter):
    1.42      # check whether we want to do file serving using tramline
    1.43      if not filter.req.headers_out.has_key('tramline_file'):
    1.44          pass_on(filter)
    1.45 -        filter.flush()
    1.46          return
    1.47  
    1.48      status = filter.req.status
    1.49 @@ -231,6 +227,7 @@
    1.50      f = open(p, 'rb')
    1.51      dump_file_range(f, 0, size-1, filter)
    1.52      f.close()
    1.53 +    filter.close()
    1.54  
    1.55  def serve_ranges(path, ranges, filter, boundary=None):
    1.56      size = os.stat(path).st_size
    1.57 @@ -250,13 +247,15 @@
    1.58               hout['Content-Range'] = 'bytes */%d' % size
    1.59               hout['Content-Length'] = str(size)
    1.60               filter.req.status = 416
    1.61 +             filter.close()
    1.62               return
    1.63  
    1.64  	log("serve_file_ranges: one chunk, %d-%d" % (start, end), filter.req)
    1.65    	hout['Content-Length'] = str(end - start + 1)
    1.66          hout['Content-Range'] = 'bytes %d-%d/%d' % (start, end, size)
    1.67  	dump_file_range(fd, start, end, filter)
    1.68 -	return
    1.69 +        filter.close()
    1.70 +        return	
    1.71  
    1.72      del hout['Content-Range'] # If present, was from app server and can't be ok
    1.73      if boundary is None:
    1.74 @@ -298,7 +297,11 @@
    1.75          filter.write(data)
    1.76          # flush the data out as soon as possible, so we don't
    1.77          # waste memory
    1.78 -        filter.flush()
    1.79 +        try:
    1.80 +	    filter.flush()
    1.81 +	except IOError:
    1.82 +	    # Happens if user cancels download
    1.83 +	    return
    1.84  
    1.85      
    1.86  class ProcessorRegistry:
    1.87 @@ -327,6 +330,7 @@
    1.88          self.id = id
    1.89          self._upload_files = []
    1.90          self._incoming = []
    1.91 +	self._isize = 0
    1.92          # we use a state pattern where the handle method gets
    1.93          # replaced by the current handle method for this state.
    1.94          self.handle = self.handle_first_boundary
    1.95 @@ -341,9 +345,14 @@
    1.96      def pushInputLine(self, data, out):
    1.97          # collect data
    1.98          self._incoming.append(data)
    1.99 +	self._isize += len(data)
   1.100 +
   1.101          # if we're not at the end of the line, input was broken
   1.102 -        # somewhere. We return to collect more first.
   1.103 -        if data[-1] != '\n':
   1.104 +        # somewhere, unless we are handling file data which might be binary.
   1.105 +	# We return to collect more first (also for file data if too small)
   1.106 +        if data[-1] != '\n' and (
   1.107 +           self.handle != self.handle_file_data or
   1.108 +               self._isize < FILE_CHUNKSIZE/2):
   1.109              return
   1.110          # now use the line in whatever handle method is current
   1.111          if len(self._incoming) == 1:
   1.112 @@ -351,6 +360,7 @@
   1.113          else:
   1.114              line = ''.join(self._incoming)
   1.115          self._incoming = []
   1.116 +        self._isize = 0
   1.117  
   1.118          self.handle(line, out)
   1.119