]> git.parisson.com Git - telemeta.git/commitdiff
fix file close in cache, add MIME types to analyzers, fix timeside decoder call bug
authorGuillaume Pellerin <yomguy@parisson.com>
Tue, 11 Jan 2011 10:26:03 +0000 (11:26 +0100)
committerGuillaume Pellerin <yomguy@parisson.com>
Tue, 11 Jan 2011 10:26:03 +0000 (11:26 +0100)
telemeta/cache.py
telemeta/web/base.py

index 19ec76ec318865183c30b9f97cfaab8d854ade76..d856917550d85bab55b01d0794ec2d891ace2220 100644 (file)
@@ -73,14 +73,14 @@ class TelemetaCache(object):
         
     def read_stream_bin(self, file):
         path = self.dir + os.sep + file
-        chunk_size = 0xFFF
+        chunk_size = 0xFFFF
         f = open(path,  'r')
         while True:
-            _chunk = f.read(chunk_size)
-            if not len(_chunk):
+            chunk = f.read(chunk_size)
+            if not len(chunk):
+                f.close()
                 break
-            yield _chunk
-        f.close()
+            yield chunk
 
     def write_stream_bin(self, chunk, file_object):
         file_object.write(chunk)
@@ -88,6 +88,7 @@ class TelemetaCache(object):
     def read_analyzer_xml(self, file):
         list = []
         path = self.dir + os.sep + file
+        f = open(path, "r")
         doc = xml.dom.minidom.parse(path)
         for data in doc.documentElement.getElementsByTagName('data') :
             name = data.getAttribute('name')
@@ -95,6 +96,7 @@ class TelemetaCache(object):
             unit = data.getAttribute('unit')
             value = data.getAttribute('value')
             list.append({'name': name, 'id': id, 'unit': unit, 'value': value})
+        f.close()
         return list
         
     def write_analyzer_xml(self, data_list, file):
index 9c1257e10ff3efaf9f355ff167bd55cae84b7943..bfa1ec7c01d0a41942ec5eed085c01931d6153af 100644 (file)
@@ -66,10 +66,10 @@ def render(request, template, data = None, mimetype = None):
 def stream_from_processor(decoder, processor):
     while True:
         frames,  eod = decoder.process()
-        buffer,  eod_proc = processor.process(frames, eod)
-        yield buffer
+        _chunk,  eod_proc = processor.process(frames, eod)
         if eod_proc:
             break
+        yield _chunk
 
 def stream_from_file(file):
     chunk_size = 0xFFFF
@@ -77,10 +77,11 @@ def stream_from_file(file):
     while True:
         _chunk = f.read(chunk_size)
         if not len(_chunk):
+            f.close()
             break
         yield _chunk
-    f.close()
-
+    
+    
 class WebView(object):
     """Provide web UI methods"""
 
@@ -143,10 +144,13 @@ class WebView(object):
             if item.file:
                 decoder  = timeside.decoder.FileDecoder(item.file.path)
                 pipe = decoder
+                mime_type = decoder.mimetype
+                
                 for analyzer in self.analyzers:
                     subpipe = analyzer()
                     analyzers_sub.append(subpipe)
                     pipe = pipe | subpipe
+                    
                 pipe.run()
                 
                 for analyzer in analyzers_sub:
@@ -156,14 +160,16 @@ class WebView(object):
                         item.approx_duration = approx_value
                         item.save()
                         value = datetime.timedelta(0,value)
-                    
+                    if analyzer.id() == 'mime_type':
+                        value = decoder.format()
+                        
                     analyzers.append({'name':analyzer.name(),
                                       'id':analyzer.id(),
                                       'unit':analyzer.unit(),
                                       'value':str(value)})
-                decoder.release()
                 
             self.cache.write_analyzer_xml(analyzers, analyze_file)
+            
         
         return render(request, template, 
                     {'item': item, 'export_formats': formats, 
@@ -173,11 +179,12 @@ class WebView(object):
 
     def item_analyze(self):
         pass
-    
+        
     def item_visualize(self, request, public_id, visualizer_id, width, height):
         item = MediaItem.objects.get(public_id=public_id)
         mime_type = 'image/png'
         grapher_id = visualizer_id
+        
         for grapher in self.graphers:
             if grapher.id() == grapher_id:
                 break
@@ -186,18 +193,17 @@ class WebView(object):
             raise Http404
         
         size = width + '_' + height
-        file = '.'.join([public_id, grapher_id, size, 'png'])
+        image_file = '.'.join([public_id, grapher_id, size, 'png'])
 
-        if not self.cache.exists(file):
+        if not self.cache.exists(image_file):
             if item.file:
-                item = MediaItem.objects.get(public_id=public_id)
                 decoder  = timeside.decoder.FileDecoder(item.file.path)
-                graph = grapher(width=int(width), height=int(height))
+                graph = grapher(width = int(width), height = int(height))
                 pipe = decoder | graph
                 pipe.run()
-                graph.render(self.cache.dir + os.sep + file)
-        
-        response = HttpResponse(self.cache.read_stream_bin(file), mimetype = mime_type)
+                graph.render(self.cache.dir + os.sep + image_file)
+                
+        response = HttpResponse(self.cache.read_stream_bin(image_file), mimetype = mime_type)
         return response
 
     def list_export_extensions(self):
@@ -239,11 +245,10 @@ class WebView(object):
 #                enc.set_metadata(metadata)
                 pipe = decoder | proc
                 pipe.run()
-#                response = HttpResponse(stream_from_processor(decoder, proc), mimetype = mime_type)
-#            else:
-            response = HttpResponse(self.cache_export.read_stream_bin(file), mimetype = mime_type)
+                response = HttpResponse(stream_from_processor(decoder, proc), mimetype = mime_type)
+            else:
+                response = HttpResponse(self.cache_export.read_stream_bin(file), mimetype = mime_type)
         
-        decoder.release()
         response['Content-Disposition'] = 'attachment'
         return response