Better command detection for the converter library

git-svn-id: svn://svn.berlios.de/gpodder/trunk@451 b0d088ad-0a06-0410-aad2-9ed5178a7e87
This commit is contained in:
Thomas Perl 2007-11-05 00:15:56 +00:00
parent 70a9759471
commit 7fa2c4e38c
3 changed files with 49 additions and 12 deletions

View File

@ -1,3 +1,12 @@
Mon, 05 Nov 2007 01:12:15 +0100 <thp@perli.net>
Better command detection for the converter library
* src/gpodder/libconverter.py: Detect existence of commands before
invoking them for converting files; based on a patch by
Nick (nikosapi.org); ConverterCollection is now a dict itself :)
* src/gpodder/util.py: Add new find_command() function that will
search the system's PATH for a specific executable command
Mon, 05 Nov 2007 00:21:39 +0100 <thp@perli.net>
Remove extracted cover art when deleting episode files

View File

@ -29,13 +29,17 @@ import re
import popen2
import os
import os.path
import types
from gpodder import util
from gpodder.liblogger import log
class FileConverter:
percentage_match = re.compile('(\d+)%')
def __init__( self, decoder_command):
def __init__( self, decoder_command, decoder_arguments):
self.encoder_command = 'lame --nohist /dev/stdin "%s"'
self.decoder_command = decoder_command
self.decoder_command = ' '.join( ( decoder_command, decoder_arguments ))
def convert( self, input_filename, output_filename, callback = None):
input_command = self.decoder_command % input_filename
@ -54,25 +58,30 @@ class FileConverter:
return process.wait() == 0
class ConverterCollection:
def __init__( self):
self.dict = {}
def add_converter( self, extension, command):
self.dict[extension.lower()] = FileConverter( command)
class ConverterCollection( types.DictType):
def add_converter( self, extension, command, arguments):
if util.find_command( command) != None:
log( 'Found "%s", will try to convert ".%s" files.' % ( command, extension ), sender = self)
self[extension.lower()] = FileConverter( command, arguments)
else:
log( 'Could not find "%s", ".%s" files cannot be converted.' % ( command, extension ), sender = self)
def has_converter( self, extension):
return self.dict.has_key( extension.lower())
if util.find_command( 'lame') != None:
return self.has_key( extension.lower())
else:
log( 'Please install the "lame" package to convert files.', sender = self)
return False
def convert( self, input_filename, output_filename = None, callback = None):
extension = os.path.splitext( input_filename)[1][1:]
if extension.lower() not in self.dict:
if extension.lower() not in self:
return None
if not output_filename:
output_filename = os.path.splitext( input_filename)[0]+'.mp3'
if not self.dict[extension.lower()].convert( input_filename, output_filename, callback):
if not self[extension.lower()].convert( input_filename, output_filename, callback):
return None
return output_filename
@ -81,5 +90,5 @@ class ConverterCollection:
converters = ConverterCollection()
# Add known converter applications
converters.add_converter( 'ogg', 'oggdec --quiet --output=/dev/stdout "%s"')
converters.add_converter( 'ogg', 'oggdec', '--quiet --output=/dev/stdout "%s"')

View File

@ -396,3 +396,22 @@ def format_desktop_command( command, filename):
return '%s "%s"' % ( command, filename )
def find_command( command):
"""
Searches the system's PATH for a specific command that is
executable by the user. Returns the first occurence of an
executable binary in the PATH, or None if the command is
not available.
"""
if 'PATH' not in os.environ:
return None
for path in os.environ['PATH'].split( os.pathsep):
command_file = os.path.join( path, command)
if os.path.isfile( command_file) and os.access( command_file, os.X_OK):
return command_file
return None