Media type refractors, pep8, lint
authorJoar Wandborg <git@wandborg.com>
Mon, 26 Mar 2012 15:44:08 +0000 (17:44 +0200)
committerJoar Wandborg <git@wandborg.com>
Mon, 26 Mar 2012 15:44:08 +0000 (17:44 +0200)
- Removed THUMB_SIZE, MEDIUM_SIZE constants, depend on
  configuration values instead.
- pep8 refractoring

mediagoblin/media_types/ascii/asciitoimage.py
mediagoblin/media_types/ascii/processing.py
mediagoblin/media_types/audio/transcoders.py
mediagoblin/media_types/image/processing.py
mediagoblin/media_types/video/processing.py
mediagoblin/media_types/video/transcoders.py
mediagoblin/processing.py

index e1c4fb44c4a2e93b456de5735478f5e3aa076145..3017d2ad25f10539fd0c57d4f050a853f086e3d5 100644 (file)
@@ -23,6 +23,7 @@ import os
 
 _log = logging.getLogger(__name__)
 
+
 class AsciiToImage(object):
     '''
     Converter of ASCII art into image files, preserving whitespace
index 846c39fbb8675a992110628c81f3a6d7b6795a07..cd00b346d2856626ea7cd43aeecea5ca1c9998f3 100644 (file)
@@ -19,13 +19,14 @@ import Image
 import logging
 
 from mediagoblin import mg_globals as mgg
-from mediagoblin.processing import create_pub_filepath, THUMB_SIZE
+from mediagoblin.processing import create_pub_filepath
 from mediagoblin.media_types.ascii import asciitoimage
 
 _log = logging.getLogger(__name__)
 
 SUPPORTED_EXTENSIONS = ['txt', 'asc', 'nfo']
 
+
 def sniff_handler(media_file, **kw):
     if kw.get('media') is not None:
         name, ext = os.path.splitext(kw['media'].filename)
@@ -36,6 +37,7 @@ def sniff_handler(media_file, **kw):
 
     return False
 
+
 def process_ascii(entry):
     '''
     Code to process a txt file
@@ -81,7 +83,10 @@ def process_ascii(entry):
             queued_file.read())
 
         with file(tmp_thumb_filename, 'w') as thumb_file:
-            thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
+            thumb.thumbnail(
+                (mgg.global_config['media:thumb']['max_width'],
+                 mgg.global_config['media:thumb']['max_height']),
+                Image.ANTIALIAS)
             thumb.save(thumb_file)
 
         _log.debug('Copying local file to public storage')
@@ -96,7 +101,6 @@ def process_ascii(entry):
             as original_file:
             original_file.write(queued_file.read())
 
-
         queued_file.seek(0)  # Rewind *again*
 
         unicode_filepath = create_pub_filepath(entry, 'ascii-portable.txt')
index ed40e03c6bb9a786352100f34a237b57e4cc0c68..f84ab7f27b6177054e152f3e096a64cf6b9395a7 100644 (file)
@@ -24,7 +24,7 @@ from mediagoblin.media_types.audio import audioprocessing
 
 _log = logging.getLogger(__name__)
 
-CPU_COUNT = 2 # Just assuming for now
+CPU_COUNT = 2  # Just assuming for now
 
 # IMPORT MULTIPROCESSING
 try:
@@ -60,6 +60,7 @@ except ImportError:
 
 import numpy
 
+
 class AudioThumbnailer(object):
     def __init__(self):
         _log.info('Initializing {0}'.format(self.__class__.__name__))
@@ -178,7 +179,7 @@ class AudioTranscoder(object):
 
         # Set up pipeline
         self.pipeline = gst.parse_launch(
-            'filesrc location="{src}" ! ' 
+            'filesrc location="{src}" ! '
             'decodebin2 ! queue ! audiorate tolerance={tolerance} ! '
             'audioconvert ! audio/x-raw-float,channels=2 ! '
             '{mux_string} ! '
index 5275981e7fc1c32132199fe28dfb06a44c7f5381..bacfecb85e820c25fd57f16eccd2167315f64f7b 100644 (file)
@@ -20,7 +20,7 @@ import logging
 
 from mediagoblin import mg_globals as mgg
 from mediagoblin.processing import BadMediaFail, \
-    create_pub_filepath, THUMB_SIZE, MEDIUM_SIZE
+    create_pub_filepath
 from mediagoblin.tools.exif import exif_fix_image_orientation, \
     extract_exif, clean_exif, get_gps_data, get_useful
 
@@ -28,6 +28,7 @@ _log = logging.getLogger(__name__)
 
 SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg']
 
+
 def sniff_handler(media_file, **kw):
     if kw.get('media') is not None:  # That's a double negative!
         name, ext = os.path.splitext(kw['media'].filename)
@@ -50,6 +51,7 @@ def sniff_handler(media_file, **kw):
 
     return False
 
+
 def process_image(entry):
     """
     Code to process an image
@@ -80,7 +82,10 @@ def process_image(entry):
 
     thumb = exif_fix_image_orientation(thumb, exif_tags)
 
-    thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
+    thumb.thumbnail(
+        (mgg.global_config['media:thumb']['max_width'],
+         mgg.global_config['media:thumb']['max_height']),
+        Image.ANTIALIAS)
 
     # Copy the thumb to the conversion subdir, then remotely.
     thumb_filename = 'thumbnail' + extension
@@ -103,8 +108,12 @@ def process_image(entry):
     # Fix orientation
     medium = exif_fix_image_orientation(medium, exif_tags)
 
-    if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
-        medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
+    if medium.size[0] > mgg.global_config['media:medium']['max_width'] \
+        or medium.size[1] > mgg.global_config['media:medium']['max_height']:
+        medium.thumbnail(
+            (mgg.global_config['media:medium']['max_width'],
+             mgg.global_config['media:medium']['max_height']),
+            Image.ANTIALIAS)
 
     medium_filename = 'medium' + extension
     medium_filepath = create_pub_filepath(entry, medium_filename)
@@ -124,7 +133,7 @@ def process_image(entry):
 
     with queued_file:
         #create_pub_filepath(entry, queued_filepath[-1])
-        original_filepath = create_pub_filepath(entry, basename + extension) 
+        original_filepath = create_pub_filepath(entry, basename + extension)
 
         with mgg.public_store.get_file(original_filepath, 'wb') \
             as original_file:
index a8fcf86d829127400bdf896e3b5ef190299062e5..4c44e65f593143a2f3c3367001f44606211dd9ca 100644 (file)
@@ -19,8 +19,7 @@ import logging
 import os
 
 from mediagoblin import mg_globals as mgg
-from mediagoblin.processing import mark_entry_failed, \
-    THUMB_SIZE, MEDIUM_SIZE, create_pub_filepath
+from mediagoblin.processing import create_pub_filepath
 from . import transcoders
 
 logging.basicConfig()
@@ -28,6 +27,7 @@ logging.basicConfig()
 _log = logging.getLogger(__name__)
 _log.setLevel(logging.DEBUG)
 
+
 def sniff_handler(media_file, **kw):
     transcoder = transcoders.VideoTranscoder()
     data = transcoder.discover(media_file.name)
@@ -44,6 +44,7 @@ def sniff_handler(media_file, **kw):
 
     return False
 
+
 def process_video(entry):
     """
     Process a video entry, transcode the queued media files (originals) and
@@ -62,13 +63,12 @@ def process_video(entry):
         entry,
         '{original}-640p.webm'.format(
             original=os.path.splitext(
-                queued_filepath[-1])[0] # Select the file name without .ext
+                queued_filepath[-1])[0]  # Select the file name without .ext
             ))
 
     thumbnail_filepath = create_pub_filepath(
         entry, 'thumbnail.jpg')
 
-
     # Create a temporary file for the video destination
     tmp_dst = tempfile.NamedTemporaryFile()
 
index d865bc8d97bfaf82f95b5dab7faccd46641d5d45..e0bd0d3d012cd7045390826451c771b19158a34e 100644 (file)
@@ -21,7 +21,6 @@ os.putenv('GST_DEBUG_DUMP_DOT_DIR', '/tmp')
 
 import sys
 import logging
-import pdb
 import urllib
 
 _log = logging.getLogger(__name__)
@@ -267,7 +266,7 @@ class VideoThumbnailer:
             return 0
 
         try:
-            return pipeline.query_duration(gst.FORMAT_TIME)[0] 
+            return pipeline.query_duration(gst.FORMAT_TIME)[0]
         except gst.QueryError:
             return self._get_duration(pipeline, retries + 1)
 
@@ -317,12 +316,11 @@ class VideoThumbnailer:
             self.bus.disconnect(self.watch_id)
             self.bus = None
 
-
     def __halt_final(self):
         _log.info('Done')
         if self.errors:
             _log.error(','.join(self.errors))
-            
+
         self.loop.quit()
 
 
@@ -454,7 +452,7 @@ class VideoTranscoder:
         self.ffmpegcolorspace = gst.element_factory_make(
             'ffmpegcolorspace', 'ffmpegcolorspace')
         self.pipeline.add(self.ffmpegcolorspace)
-        
+
         self.videoscale = gst.element_factory_make('ffvideoscale', 'videoscale')
         #self.videoscale.set_property('method', 2)  # I'm not sure this works
         #self.videoscale.set_property('add-borders', 0)
@@ -548,7 +546,6 @@ class VideoTranscoder:
         # Setup the message bus and connect _on_message to the pipeline
         self._setup_bus()
 
-
     def _on_dynamic_pad(self, dbin, pad, islast):
         '''
         Callback called when ``decodebin2`` has a pad that we can connect to
@@ -593,11 +590,11 @@ class VideoTranscoder:
 
         t = message.type
 
-        if t == gst.MESSAGE_EOS:
+        if message.type == gst.MESSAGE_EOS:
             self._discover_dst_and_stop()
             _log.info('Done')
 
-        elif t == gst.MESSAGE_ELEMENT:
+        elif message.type == gst.MESSAGE_ELEMENT:
             if message.structure.get_name() == 'progress':
                 data = dict(message.structure)
 
@@ -619,7 +616,6 @@ class VideoTranscoder:
 
         self.dst_discoverer.discover()
 
-
     def __dst_discovered(self, data, is_media):
         self.dst_data = data
 
@@ -694,4 +690,3 @@ if __name__ == '__main__':
         transcoder.transcode(*args, progress_callback=cb)
     elif options.action == 'discover':
         print transcoder.discover(*args).__dict__
-    
index cc8b7f813ed18fe15e473b05bb30268c4bed7794..989591de33634b9e6f3b2e69613f6706de48cf32 100644 (file)
@@ -27,14 +27,6 @@ from mediagoblin.media_types import get_media_manager
 
 _log = logging.getLogger(__name__)
 
-# This might fail if this module is loaded before the global_config
-# is parsed although this far it has not.
-THUMB_SIZE = (mgg.global_config['media:thumb']['max_width'],
-              mgg.global_config['media:thumb']['max_height'])
-
-MEDIUM_SIZE = (mgg.global_config['media:medium']['max_width'],
-               mgg.global_config['media:medium']['max_height'])
-
 
 def create_pub_filepath(entry, filename):
     return mgg.public_store.get_unique_filepath(