vendor/tramline

changeset 13:57410de1cafa gracinet-progress-bar

Replaced raw uploaded length by json data like gp.fileupload
author gracinet
date Sun, 01 Mar 2009 15:28:17 +0000
parents c77f592b081f
children 60db9e957c02
files src/tramline/core.py src/tramline/tests/test_core.py
diffstat 2 files changed, 26 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- a/src/tramline/core.py
     1.2 +++ b/src/tramline/core.py
     1.3 @@ -109,6 +109,7 @@
     1.4          # no id, so create new processor instance and store
     1.5          # away id
     1.6          processor = theProcessorRegistry.createProcessor()
     1.7 +        processor.initFromInputFilter(filter)
     1.8          filter.req.headers_in['tramline_id'] = str(processor.id)
     1.9      else:
    1.10          # reuse existing processor instance based on id
    1.11 @@ -348,6 +349,9 @@
    1.12  
    1.13         uploaded: this is the total file data upload this processor has seen. 
    1.14             there might be more than one file in this request.
    1.15 +
    1.16 +       upload_length: the full length of the POST request, as taken from the
    1.17 +       header
    1.18      """
    1.19         
    1.20      def __init__(self, id):
    1.21 @@ -364,6 +368,10 @@
    1.22          self.vars_to_handle = []
    1.23          self._enable_vars=''
    1.24  
    1.25 +    def initFromInputFilter(self, filter):
    1.26 +        headers = filter.req.headers_in
    1.27 +        self.upload_length = long(headers['Content-Length'])
    1.28 +
    1.29      def pushInput(self, data, out):
    1.30          lines = data.splitlines(True)
    1.31          for line in lines:
    1.32 @@ -385,7 +393,8 @@
    1.33      def _storeProgress(self, req):
    1.34          if self.progress_id is not None:
    1.35              f = open(get_progress_path(req, self.progress_id), 'w')
    1.36 -            f.write(str(self.uploaded))
    1.37 +            percent = int(self.uploaded*100 / self.upload_length)
    1.38 +            f.write(str({'state': 1, 'percent': int(percent)}))
    1.39              f.close()
    1.40  
    1.41      def pushInputLine(self, data, out):
    1.42 @@ -545,7 +554,10 @@
    1.43              self._previous_line = line
    1.44  
    1.45  def get_progress(req, progress_id):
    1.46 -    f = open(get_progress_path(req, progress_id), 'r')
    1.47 +    try:
    1.48 +        f = open(get_progress_path(req, progress_id), 'r')
    1.49 +    except IOError: # inaccessible progress log: process not really started
    1.50 +        return str({'state': 0, 'percent': 0})
    1.51      s = f.read()
    1.52      f.close() # let's be explicit :-)
    1.53      return s
     2.1 --- a/src/tramline/tests/test_core.py
     2.2 +++ b/src/tramline/tests/test_core.py
     2.3 @@ -27,7 +27,8 @@
     2.4  
     2.5  class Request:
     2.6      def __init__(self, method):
     2.7 -        self.headers_in = StringTable({'Content-Type' : 'multipart/form-data'})
     2.8 +        self.headers_in = StringTable({'Content-Type' : 'multipart/form-data',
     2.9 +                                       'Content-Length': '56'})
    2.10          self.headers_out = StringTable()
    2.11          self.main = None
    2.12          self.method = method
    2.13 @@ -156,8 +157,8 @@
    2.14          input.close()
    2.15  
    2.16          from tramline.core import get_progress
    2.17 -        uploaded = get_progress(filter.req, progress_id)
    2.18 -        self.assertFalse(uploaded is None)
    2.19 +        progress = get_progress(filter.req, progress_id)
    2.20 +        self.assertFalse(progress is None)
    2.21  
    2.22          output_data = output.getvalue()
    2.23  
    2.24 @@ -167,9 +168,15 @@
    2.25          data = f.read()
    2.26          f.close()
    2.27          expected = 'first line\nsecond line\n'
    2.28 -        self.assertEquals('first line\nsecond line\n', data)
    2.29 +        self.assertEquals(23, len(expected)) # update this if needed
    2.30 +        self.assertEquals(expected, data)
    2.31          # process has finished, so uploaded should be the entire length
    2.32 -        self.assertEquals(str(len(expected)), uploaded)
    2.33 +        # but the fake request is suppose to be 56 bytes long
    2.34 +        self.assertEquals("{'state': 1, 'percent': 41}", progress)
    2.35 +
    2.36 +        # Now requesting progress info with an unknown ID
    2.37 +        self.assertEquals("{'state': 0, 'percent': 0}",
    2.38 +                          get_progress(filter.req, 'TESTIP-4567'))
    2.39          
    2.40      def test_split_filter(self):
    2.41          f = open(get_data_path('input2.txt'), 'rb')