Media type refractors, pep8, lint

- Removed THUMB_SIZE, MEDIUM_SIZE constants, depend on
  configuration values instead.
- pep8 refractoring
This commit is contained in:
Joar Wandborg 2012-03-26 17:44:08 +02:00
parent a9d84d4cb7
commit c56d4b55a1
7 changed files with 34 additions and 32 deletions

View File

@ -23,6 +23,7 @@ import os
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
class AsciiToImage(object): class AsciiToImage(object):
''' '''
Converter of ASCII art into image files, preserving whitespace Converter of ASCII art into image files, preserving whitespace

View File

@ -19,13 +19,14 @@ import Image
import logging import logging
from mediagoblin import mg_globals as mgg 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 from mediagoblin.media_types.ascii import asciitoimage
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
SUPPORTED_EXTENSIONS = ['txt', 'asc', 'nfo'] SUPPORTED_EXTENSIONS = ['txt', 'asc', 'nfo']
def sniff_handler(media_file, **kw): def sniff_handler(media_file, **kw):
if kw.get('media') is not None: if kw.get('media') is not None:
name, ext = os.path.splitext(kw['media'].filename) name, ext = os.path.splitext(kw['media'].filename)
@ -36,6 +37,7 @@ def sniff_handler(media_file, **kw):
return False return False
def process_ascii(entry): def process_ascii(entry):
''' '''
Code to process a txt file Code to process a txt file
@ -81,7 +83,10 @@ def process_ascii(entry):
queued_file.read()) queued_file.read())
with file(tmp_thumb_filename, 'w') as thumb_file: 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) thumb.save(thumb_file)
_log.debug('Copying local file to public storage') _log.debug('Copying local file to public storage')
@ -96,7 +101,6 @@ def process_ascii(entry):
as original_file: as original_file:
original_file.write(queued_file.read()) original_file.write(queued_file.read())
queued_file.seek(0) # Rewind *again* queued_file.seek(0) # Rewind *again*
unicode_filepath = create_pub_filepath(entry, 'ascii-portable.txt') unicode_filepath = create_pub_filepath(entry, 'ascii-portable.txt')

View File

@ -24,7 +24,7 @@ from mediagoblin.media_types.audio import audioprocessing
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
CPU_COUNT = 2 # Just assuming for now CPU_COUNT = 2 # Just assuming for now
# IMPORT MULTIPROCESSING # IMPORT MULTIPROCESSING
try: try:
@ -60,6 +60,7 @@ except ImportError:
import numpy import numpy
class AudioThumbnailer(object): class AudioThumbnailer(object):
def __init__(self): def __init__(self):
_log.info('Initializing {0}'.format(self.__class__.__name__)) _log.info('Initializing {0}'.format(self.__class__.__name__))
@ -178,7 +179,7 @@ class AudioTranscoder(object):
# Set up pipeline # Set up pipeline
self.pipeline = gst.parse_launch( self.pipeline = gst.parse_launch(
'filesrc location="{src}" ! ' 'filesrc location="{src}" ! '
'decodebin2 ! queue ! audiorate tolerance={tolerance} ! ' 'decodebin2 ! queue ! audiorate tolerance={tolerance} ! '
'audioconvert ! audio/x-raw-float,channels=2 ! ' 'audioconvert ! audio/x-raw-float,channels=2 ! '
'{mux_string} ! ' '{mux_string} ! '

View File

@ -20,7 +20,7 @@ import logging
from mediagoblin import mg_globals as mgg from mediagoblin import mg_globals as mgg
from mediagoblin.processing import BadMediaFail, \ from mediagoblin.processing import BadMediaFail, \
create_pub_filepath, THUMB_SIZE, MEDIUM_SIZE create_pub_filepath
from mediagoblin.tools.exif import exif_fix_image_orientation, \ from mediagoblin.tools.exif import exif_fix_image_orientation, \
extract_exif, clean_exif, get_gps_data, get_useful extract_exif, clean_exif, get_gps_data, get_useful
@ -28,6 +28,7 @@ _log = logging.getLogger(__name__)
SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg'] SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg']
def sniff_handler(media_file, **kw): def sniff_handler(media_file, **kw):
if kw.get('media') is not None: # That's a double negative! if kw.get('media') is not None: # That's a double negative!
name, ext = os.path.splitext(kw['media'].filename) name, ext = os.path.splitext(kw['media'].filename)
@ -50,6 +51,7 @@ def sniff_handler(media_file, **kw):
return False return False
def process_image(entry): def process_image(entry):
""" """
Code to process an image Code to process an image
@ -80,7 +82,10 @@ def process_image(entry):
thumb = exif_fix_image_orientation(thumb, exif_tags) 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. # Copy the thumb to the conversion subdir, then remotely.
thumb_filename = 'thumbnail' + extension thumb_filename = 'thumbnail' + extension
@ -103,8 +108,12 @@ def process_image(entry):
# Fix orientation # Fix orientation
medium = exif_fix_image_orientation(medium, exif_tags) medium = exif_fix_image_orientation(medium, exif_tags)
if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]: if medium.size[0] > mgg.global_config['media:medium']['max_width'] \
medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS) 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_filename = 'medium' + extension
medium_filepath = create_pub_filepath(entry, medium_filename) medium_filepath = create_pub_filepath(entry, medium_filename)
@ -124,7 +133,7 @@ def process_image(entry):
with queued_file: with queued_file:
#create_pub_filepath(entry, queued_filepath[-1]) #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') \ with mgg.public_store.get_file(original_filepath, 'wb') \
as original_file: as original_file:

View File

@ -19,8 +19,7 @@ import logging
import os import os
from mediagoblin import mg_globals as mgg from mediagoblin import mg_globals as mgg
from mediagoblin.processing import mark_entry_failed, \ from mediagoblin.processing import create_pub_filepath
THUMB_SIZE, MEDIUM_SIZE, create_pub_filepath
from . import transcoders from . import transcoders
logging.basicConfig() logging.basicConfig()
@ -28,6 +27,7 @@ logging.basicConfig()
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
_log.setLevel(logging.DEBUG) _log.setLevel(logging.DEBUG)
def sniff_handler(media_file, **kw): def sniff_handler(media_file, **kw):
transcoder = transcoders.VideoTranscoder() transcoder = transcoders.VideoTranscoder()
data = transcoder.discover(media_file.name) data = transcoder.discover(media_file.name)
@ -44,6 +44,7 @@ def sniff_handler(media_file, **kw):
return False return False
def process_video(entry): def process_video(entry):
""" """
Process a video entry, transcode the queued media files (originals) and Process a video entry, transcode the queued media files (originals) and
@ -62,13 +63,12 @@ def process_video(entry):
entry, entry,
'{original}-640p.webm'.format( '{original}-640p.webm'.format(
original=os.path.splitext( 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( thumbnail_filepath = create_pub_filepath(
entry, 'thumbnail.jpg') entry, 'thumbnail.jpg')
# Create a temporary file for the video destination # Create a temporary file for the video destination
tmp_dst = tempfile.NamedTemporaryFile() tmp_dst = tempfile.NamedTemporaryFile()

View File

@ -21,7 +21,6 @@ os.putenv('GST_DEBUG_DUMP_DOT_DIR', '/tmp')
import sys import sys
import logging import logging
import pdb
import urllib import urllib
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
@ -267,7 +266,7 @@ class VideoThumbnailer:
return 0 return 0
try: try:
return pipeline.query_duration(gst.FORMAT_TIME)[0] return pipeline.query_duration(gst.FORMAT_TIME)[0]
except gst.QueryError: except gst.QueryError:
return self._get_duration(pipeline, retries + 1) return self._get_duration(pipeline, retries + 1)
@ -317,12 +316,11 @@ class VideoThumbnailer:
self.bus.disconnect(self.watch_id) self.bus.disconnect(self.watch_id)
self.bus = None self.bus = None
def __halt_final(self): def __halt_final(self):
_log.info('Done') _log.info('Done')
if self.errors: if self.errors:
_log.error(','.join(self.errors)) _log.error(','.join(self.errors))
self.loop.quit() self.loop.quit()
@ -454,7 +452,7 @@ class VideoTranscoder:
self.ffmpegcolorspace = gst.element_factory_make( self.ffmpegcolorspace = gst.element_factory_make(
'ffmpegcolorspace', 'ffmpegcolorspace') 'ffmpegcolorspace', 'ffmpegcolorspace')
self.pipeline.add(self.ffmpegcolorspace) self.pipeline.add(self.ffmpegcolorspace)
self.videoscale = gst.element_factory_make('ffvideoscale', 'videoscale') self.videoscale = gst.element_factory_make('ffvideoscale', 'videoscale')
#self.videoscale.set_property('method', 2) # I'm not sure this works #self.videoscale.set_property('method', 2) # I'm not sure this works
#self.videoscale.set_property('add-borders', 0) #self.videoscale.set_property('add-borders', 0)
@ -548,7 +546,6 @@ class VideoTranscoder:
# Setup the message bus and connect _on_message to the pipeline # Setup the message bus and connect _on_message to the pipeline
self._setup_bus() self._setup_bus()
def _on_dynamic_pad(self, dbin, pad, islast): def _on_dynamic_pad(self, dbin, pad, islast):
''' '''
Callback called when ``decodebin2`` has a pad that we can connect to Callback called when ``decodebin2`` has a pad that we can connect to
@ -593,11 +590,11 @@ class VideoTranscoder:
t = message.type t = message.type
if t == gst.MESSAGE_EOS: if message.type == gst.MESSAGE_EOS:
self._discover_dst_and_stop() self._discover_dst_and_stop()
_log.info('Done') _log.info('Done')
elif t == gst.MESSAGE_ELEMENT: elif message.type == gst.MESSAGE_ELEMENT:
if message.structure.get_name() == 'progress': if message.structure.get_name() == 'progress':
data = dict(message.structure) data = dict(message.structure)
@ -619,7 +616,6 @@ class VideoTranscoder:
self.dst_discoverer.discover() self.dst_discoverer.discover()
def __dst_discovered(self, data, is_media): def __dst_discovered(self, data, is_media):
self.dst_data = data self.dst_data = data
@ -694,4 +690,3 @@ if __name__ == '__main__':
transcoder.transcode(*args, progress_callback=cb) transcoder.transcode(*args, progress_callback=cb)
elif options.action == 'discover': elif options.action == 'discover':
print transcoder.discover(*args).__dict__ print transcoder.discover(*args).__dict__

View File

@ -27,14 +27,6 @@ from mediagoblin.media_types import get_media_manager
_log = logging.getLogger(__name__) _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): def create_pub_filepath(entry, filename):
return mgg.public_store.get_unique_filepath( return mgg.public_store.get_unique_filepath(