# Author: Olivier Guilyardi <olivier@samalyse.com>
# Guillaume Pellerin <pellerin@parisson.com>
-from telemeta.core import *
+from telemeta.core import Interface, TelemetaError
+
class IExporter(Interface):
"""Export driver interface"""
It should be possible to make subsequent calls to process() with
different items, using the same driver instance.
"""
+
+class ExportProcessError(TelemetaError):
+
+ def __init__(self, message, command, subprocess):
+ self.message = message
+ self.command = command
+ self.subprocess = subprocess
+
+ def __str__(self):
+ error = self.subprocess.stderr.read()
+ return "%s ; command: %s; error: %s" % (self.message,
+ self.command,
+ error)
+
+
\ No newline at end of file
file_name = get_file_name(self.source)
file_name_wo_ext, file_ext = split_file_name(file_name)
self.cache_dir = cache_dir
-
self.metadata = metadata
#self.collection = self.metadata['Collection']
#self.artist = self.metadata['Artist']
# Define and create the destination path
# At the moment, the target directory is built with this scheme in
# the cache directory : ./%Format/%Collection/%Artist/
- self.cache_dir = os.path.join(self.cache_dir,'cache')
+ #self.cache_dir = os.path.join(self.cache_dir,'cache')
#export_dir = os.path.join(self.ext,self.collection,self.artist)
export_dir = os.path.join(self.cache_dir,self.ext)
if not os.path.exists(export_dir):
export_dir_split = export_dir.split(os.sep)
- path = export_dir_split[0]
+ path = os.sep + export_dir_split[0]
for _dir in export_dir_split[1:]:
path = os.path.join(path,_dir)
if not os.path.exists(path):
os.mkdir(path)
else:
- self.dest = export_dir
+ path = export_dir
# Set the target file
#target_file = file_name_wo_ext+'.'+self.ext
target_file = self.item_id+'.'+self.ext
- self.dest = os.path.join(self.dest,target_file)
- return self.dest
+ dest = os.path.join(path,target_file)
+ return dest
- def core_process(self,command,buffer_size,dest):
+ def core_process(self, command, buffer_size, dest):
"""Streams encoded audio data through a generator"""
__chunk = 0
file_out = open(dest,'w')
- proc = subprocess.Popen(command,
- shell = True,
- bufsize = buffer_size,
- stdin = subprocess.PIPE,
- stdout = subprocess.PIPE,
- close_fds = True)
-
- __chunk = proc.stdout.read(buffer_size)
- yield __chunk
- file_out.write(__chunk)
+ try:
+ proc = subprocess.Popen(command,
+ shell = True,
+ bufsize = buffer_size,
+ stdin = subprocess.PIPE,
+ stdout = subprocess.PIPE,
+ close_fds = True)
+ except:
+ raise ExportProcessError('Command failure:', command, proc)
+
# Processing
- while __chunk:
+ while True:
__chunk = proc.stdout.read(buffer_size)
+ status = proc.poll()
+ if status != None and status != 0:
+ raise ExportProcessError('Command failure:', command, proc)
+ if len(__chunk) == 0:
+ break
yield __chunk
file_out.write(__chunk)
def post_process(self, item_id, source, metadata, ext,
cache_dir, options=None):
""" Post processing : write tags, print infos, etc..."""
- self.write_tags()
+ #self.write_tags()
if not options is None:
if 'verbose' in self.options and self.options['verbose'] != '0':
print self.dest
'| flac '+self.args+' -c -'
# Pre-proccessing
- #try:
self.dest = self.pre_process(self.item_id,
self.source,
self.metadata,
self.ext,
self.cache_dir,
self.options)
- #except:
- #raise 'ExporterError [3]: pre_process'
# Processing (streaming + cache writing)
- try:
- stream = self.core_process(self.command,self.buffer_size,self.dest)
- for chunk in stream:
- yield chunk
- except:
- raise 'ExporterError: core_process'
+ stream = self.core_process(self.command,self.buffer_size,self.dest)
+ for chunk in stream:
+ yield chunk
# Post-proccessing
- try:
- self.post_process(self.item_id,
+ self.post_process(self.item_id,
self.source,
self.metadata,
self.ext,
self.cache_dir,
self.options)
- except:
- raise 'ExporterError: post_process'
-
# Encoding
#os.system('flac '+args+' -o "'+self.dest+'" "'+ \
def get_description(self):
return "FIXME"
+ def set_cache_dir(self,path):
+ self.cache_dir = path
+
def get_file_info(self):
try:
file_out1, file_out2 = os.popen4('mp3info "'+self.dest+'"')
#if tag in self.dub2args_dict.keys():
#arg = self.dub2args_dict[tag]
#value = clean_word(self.metadata[tag])
- #args = args + ' --' + arg + ' "' +value +'" '
+ #args = args.append(' --' + arg + ' "' +value +'" '
return args
self.args = self.get_args(self.metadata,options)
self.ext = self.get_file_extension()
self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \
- '| lame '+self.args+' - -'
+ '| lame '+self.args+' - '
# Pre-proccessing
- try:
- self.dest = self.pre_process(self.item_id,
+ self.dest = self.pre_process(self.item_id,
self.source,
self.metadata,
self.ext,
self.cache_dir,
self.options)
- except:
- raise 'ExporterError [3]: pre_process'
# Processing (streaming + cache writing)
- try:
- stream = self.core_process(self.command,self.buffer_size,self.dest)
- for chunk in stream:
- yield chunk
- except:
- raise 'ExporterError: core_process'
-
- # Post-proccessing
- try:
- self.post_process(self.item_id,
+ # FIXME return stream
+ stream = self.core_process(self.command,self.buffer_size,self.dest)
+ for chunk in stream:
+ yield chunk
+
+ # Post-proccessing
+ self.post_process(self.item_id,
self.source,
self.metadata,
self.ext,
self.cache_dir,
self.options)
- except:
- raise 'ExporterError: post_process'
-
+
# Encoding
# os.system('lame '+args+' --tc "default" "'+self.source+
# '" "'+self.dest+'"')
except IOError:
return 'Exporter error [1]: file does not exist.'
- #def set_cache_dir(self,path):
- # self.cache_dir = path
+ def set_cache_dir(self,path):
+ self.cache_dir = path
def decode(self):
try:
'| oggenc '+self.args+' -'
# Pre-proccessing
- try:
- self.dest = self.pre_process(self.item_id,
- self.source,
- self.metadata,
- self.ext,
- self.cache_dir,
- self.options)
- except:
- raise 'ExporterError [3]: pre_process'
+
+ self.dest = self.pre_process(self.item_id,
+ self.source,
+ self.metadata,
+ self.ext,
+ self.cache_dir,
+ self.options)
# Processing (streaming + cache writing)
- try:
- stream = self.core_process(self.command,self.buffer_size,self.dest)
- for chunk in stream:
- yield chunk
- except:
- raise 'ExporterError: core_process'
+ stream = self.core_process(self.command,self.buffer_size,self.dest)
+ for chunk in stream:
+ yield chunk
# Post-proccessing
- try:
- self.post_process(self.item_id,
- self.source,
- self.metadata,
- self.ext,
- self.cache_dir,
- self.options)
- except:
- raise 'ExporterError: post_process'
-
+ self.post_process(self.item_id,
+ self.source,
+ self.metadata,
+ self.ext,
+ self.cache_dir,
+ self.options)
# Post-proccessing
#os.system('sox "'+self.source+'" -w -r 44100 -t wav -c2 - \
return 'Exporter error [1]: file does not exist.'
def set_cache_dir(self,path):
- """Set the directory where cached files should be stored. Does nothing
- if the exporter doesn't support caching.
-
- The driver shouldn't assume that this method will always get called. A
- temporary directory should be used if that's not the case.
- """
self.cache_dir = path
def decode(self):
if not options is None:
self.options = options
- try:
- # Pre-proccessing
- self.ext = self.get_file_extension()
- self.dest = self.pre_process(self.item_id,
- self.source,
- self.metadata,
- self.ext,
- self.cache_dir,
- self.options)
-
- # Initializing
- chunk = 0
- file_in = open(self.source,'rb')
- file_out = open(self.dest,'w')
-
+
+ # Pre-proccessing
+ self.ext = self.get_file_extension()
+ self.dest = self.pre_process(self.item_id,
+ self.source,
+ self.metadata,
+ self.ext,
+ self.cache_dir,
+ self.options)
+
+ # Initializing
+ chunk = 0
+ file_in = open(self.source,'rb')
+ file_out = open(self.dest,'w')
+
+ chunk = file_in.read(self.buffer_size)
+ yield chunk
+ file_out.write(chunk)
+
+ # Core Processing
+ while chunk:
chunk = file_in.read(self.buffer_size)
yield chunk
file_out.write(chunk)
-
- # Core Processing
- while chunk:
- chunk = file_in.read(self.buffer_size)
- yield chunk
- file_out.write(chunk)
-
- file_in.close()
- file_out.close()
-
- # Create the md5 key
- #if 'md5' in self.metadata and self.metadata['md5']:
- self.create_md5_key()
-
- # Create the par2 key
- #if 'par2' in self.metadata and self.metadata['par2']:
- self.create_par_key()
-
- # Pre-proccessing
- self.post_process(self.item_id,
- self.source,
- self.metadata,
- self.ext,
- self.cache_dir,
- self.options)
- except IOError:
- raise 'ExporterError [3]: source file does not exist.'
+ file_in.close()
+ file_out.close()
+
+ # Create the md5 key
+ #if 'md5' in self.metadata and self.metadata['md5']:
+ self.create_md5_key()
+
+ # Create the par2 key
+ #if 'par2' in self.metadata and self.metadata['par2']:
+ self.create_par_key()
+
+ # Pre-proccessing
+ self.post_process(self.item_id,
+ self.source,
+ self.metadata,
+ self.ext,
+ self.cache_dir,
+ self.options)