try:
os.system('normalize-audio '+args+' "'+self.source+'"')
return self.source
- except IOError:
- return 'Exporter error: Cannot normalize, path does not exist.'
+ except:
+ raise IOError('ExporterError: cannot normalize, path does not exist.')
def check_md5_key(self):
""" Check if the md5 key is OK and return a boolean """
'" "'+self.dest+'.md5"')
return 'OK' in md5_log.split(':')
except IOError:
- return 'Exporter error: Cannot check the md5 key...'
+ raise IOError('ExporterError: cannot check the md5 key.')
def get_file_info(self):
""" Return the list of informations of the dest """
line_split = line.split(':')
value = int(int(line_split[1])/(4*44100))
return value
- except IOError:
- return 'Exporter error: Cannot get the wav length...'
+ except:
+ raise IOError('ExporterError: cannot get the wav length.')
def compare_md5_key(self, source, dest):
""" Compare source and dest files wih md5 method """
close_fds = True)
except:
raise ExportProcessError('Command failure:', command, proc)
-
# Core processing
while True:
for type in types.keys():
if isinstance(value, type) :
return types[type]
- raise TypeError, str(value) + ' has an unsupported type'
+ raise TypeError(str(value) + ' has an unsupported type')
def get_cast(value, type) :
""" Return value, casted into type """
return int(value)
elif type == 'str' :
return str(value)
- raise TypeError, type + ' is an unsupported type'
+ raise TypeError(type + ' is an unsupported type')
def get_file_mime_type(path):
""" Return the mime type of a file """
line_split = line.split(': ')
mime = line_split[len(line_split)-1]
return mime[:len(mime)-1]
- except IOError:
- return 'Exporter error [1]: path does not exist.'
+ except:
+ raise IOError('ExporterError: path does not exist.')
def get_file_type_desc(path):
""" Return the type of a file given by the 'file' command """
description = line.split(': ')
description = description[1].split(', ')
return description
- except IOError:
- return 'Exporter error [1]: path does not exist.'
+ except:
+ raise IOError('ExporterError: path does not exist.')
def iswav(path):
""" Tell if path is a WAV """
try:
mime = get_file_mime_type(path)
return mime == 'audio/x-wav'
- except IOError:
- return 'Exporter error [1]: path does not exist.'
+ except:
+ raise IOError('ExporterError: path does not exist.')
def iswav16(path):
""" Tell if path is a 16 bit WAV """
try:
file_type_desc = get_file_type_desc(path)
return iswav(path) and '16 bit' in file_type_desc
- except IOError:
- return 'Exporter error [1]: path does not exist.'
+ except:
+ raise IOError('ExporterError: path does not exist.')
def get_file_name(path):
""" Return the file name targeted in the path """
""" Return main file name and its extension """
try:
return os.path.splitext(file)
- except IOError:
- return 'Exporter error [1]: path does not exist.'
+ except:
+ raise IOError('ExporterError: path does not exist.')
def clean_word(word) :
""" Return the word without excessive blank spaces, underscores and
info.append(clean_word(line[:-1]))
self.info = info
return self.info
- except IOError:
- return 'Exporter error [1]: file does not exist.'
+ except:
+ raise IOError('ExporterError: metaflac is not installed or ' + \
+ 'file does not exist.')
def set_cache_dir(self,path):
"""Set the directory where cached files should be stored. Does nothing
os.system('flac -d -o "'+dest+'" "'+self.source+'"')
self.source = dest
return dest
- except IOError:
- return 'ExporterError [2]: decoder not compatible.'
+ except:
+ raise IOError('ExporterError: decoder is not compatible.')
def write_tags(self, file):
media = FLAC(file)
media['DESCRIPTION'] = str(self.metadata[tag])
else:
media[tag] = str(self.metadata[tag])
- media.save()
-
+ try:
+ media.save()
+ except:
+ raise IOError('ExporterError: cannot write tags.')
+
def get_args(self,options=None):
"""Get process options and return arguments for the encoder"""
args = []
#args.append('-c %s="%s"' % (arg, value))
return args
-
+
def process(self, item_id, source, metadata, options=None):
self.item_id = item_id
self.source = source
self.args = self.get_args(options)
self.ext = self.get_file_extension()
self.args = ' '.join(self.args)
- self.command = 'sox "%s" -s -q -r 44100 -t wav -c2 - | flac -c %s - ' \
- % (self.source, self.args)
-
+ self.command = 'sox "%s" -s -q -r 44100 -t wav -c2 - | flac -c %s - ' % (self.source, self.args)
+
# Pre-proccessing
self.dest = self.pre_process(self.item_id,
self.source,
info.append(clean_word(line[:-1]))
self.info = info
return self.info
- except IOError:
- return 'Exporter error [1]: file does not exist.'
+ except:
+ raise IOError('ExporterError: file does not exist.')
def decode(self):
try:
os.system('sox "'+self.source+'" -s -q -r 44100 -t wav "' \
+self.cache_dir+os.sep+self.item_id+'"')
return self.cache_dir+os.sep+self.item_id+'.wav'
- except IOError:
- return 'ExporterError [2]: decoder not compatible.'
+ except:
+ raise IOError('ExporterError: decoder is not compatible.')
def write_tags(self):
"""Write all ID3v2.4 tags by mapping dub2id3_dict dictionnary with the
frame_text = self.dub2id3_dict[tag]
value = self.metadata[tag]
frame = mutagen.id3.Frames[frame_text](3,value)
- id3.add(frame)
- id3.save()
+ try:
+ id3.add(frame)
+ except:
+ raise IOError('ExporterError: cannot tag "'+tag+'"')
+ try:
+ id3.save()
+ except:
+ raise IOError('ExporterError: cannot write tags')
def get_args(self, options=None):
"""Get process options and return arguments for the encoder"""
info.append(clean_word(line[:-1]))
self.info = info
return self.info
- except IOError:
- return 'Exporter error [1]: file does not exist.'
+ except:
+ raise IOError('ExporterError: file does not exist.')
def set_cache_dir(self,path):
self.cache_dir = path
os.system('oggdec -o "'+self.cache_dir+os.sep+self.item_id+
'.wav" "'+self.source+'"')
return self.cache_dir+os.sep+self.item_id+'.wav'
- except IOError:
- return 'ExporterError [2]: decoder not compatible.'
+ except:
+ raise IOError('ExporterError: decoder is not compatible.')
def write_tags(self):
media = OggVorbis(self.dest)
args.append('-c %s="%s"' % (arg, value))
return args
-
- def process(self, item_id, source, metadata, options=None):
+
+ def process(self, item_id, source, metadata, options=None):
self.item_id = item_id
self.source = source
self.metadata = metadata
self.ext = self.get_file_extension()
self.args = ' '.join(self.args)
self.command = 'sox "%s" -s -q -r 44100 -t wav -c2 - | oggenc %s -' % (self.source, self.args)
-
+
# Pre-proccessing
self.dest = self.pre_process(self.item_id,
self.source,
self.options)
# Processing (streaming + cache writing)
- stream = self.core_process(self.command,self.buffer_size,self.dest)
+ stream = self.core_process(self.command, self.buffer_size, self.dest)
for chunk in stream:
yield chunk
self.dest = ''
self.options = {}
self.buffer_size = 0xFFFF
-
+
def get_format(self):
return 'WAV'
info.append(clean_word(line[:-1]))
self.info = info
return self.info
- except IOError:
- return 'Exporter error [1]: file does not exist.'
+ except:
+ raise IOError('ExporterError: wavinfo id not installed or file does not exist.')
def set_cache_dir(self,path):
self.cache_dir = path
dest+'.wav"')
self.source = dest
return dest
- except IOError:
- return 'ExporterError [2]: decoder not compatible.'
+ except:
+ raise IOError('ExporterError: decoder is not compatible.')
def write_tags(self):
# Create metadata XML file !
""" Create the md5 keys of the dest """
try:
os.system('md5sum -b "'+self.dest+'" >"'+self.dest+'.md5"')
- except IOError:
- return 'Exporter error: Cannot create the md5 key...'
+ except:
+ raise IOError('ExporterError: cannot create the md5 key.')
def create_par_key(self):
""" Create the par2 keys of the dest """
try:
os.system('par2 '+args+' "'+self.dest+'"')
- except IOError:
- return 'Exporter error: Cannot create the par2 key...'
+ except:
+ raise IOError('ExporterError: cannot create the par2 key.')
def process(self, item_id, source, metadata, options=None):
self.item_id = item_id
self.source = source
self.metadata = metadata
self.options = {}
-
+
if not options is None:
self.options = options
-
+
# Pre-proccessing
self.ext = self.get_file_extension()
self.dest = self.pre_process(self.item_id,
#if self.compare_md5_key():
#os.system('cp -a "'+self.source+'" "'+ self.dest+'"')
#print 'COPIED'
-
+
a img { border: none; }\r
\r
h3 {\r
- font-size: 1.1em;\r
+ font-size: 1.2em;\r
font-weight: bold;\r
color: #353535;\r
}\r
\r
/* Navigation (borrowed from Trac) */\r
.nav h2, .nav hr { display: none }\r
-.nav ul { font-size: 10px; list-style: none; margin: 0; text-align: right }\r
+.nav ul { font-size: 10px; list-style: none; margin: 0; text-align: left }\r
.nav li {\r
border-right: 1px solid #d7d7d7;\r
display: inline;\r
padding: 0 .75em;\r
white-space: nowrap;\r
}\r
-.nav li.last { border-right: none }\r
+.nav li.last { border-right: 1px solid #000000; }\r
\r
/* Main navigation bar (borrowed from Trac) */\r
#menu {\r
padding: .2em 0;\r
}\r
#menu li { border-right: none; padding: .25em 0 }\r
+#menu li.last { border-right: 1px solid #000 }\r
#menu :link, #menu :visited {\r
background: url(../images/dots.gif) 0 0 no-repeat;\r
border-right: 1px solid #fff;\r
\r
{% if item %}\r
{% block submenu %}\r
- <h3>Item: {{ item.title }}</h3>\r
+ <h3>Item : {{ item.title }}</h3>\r
<div><a href="{% url telemeta-item-dublincore item.id|urlencode %}">Dublin Core</a></div>\r
{% endblock %}\r
\r
</div>\r
<div class="extraInfos">\r
{% block musical_performances %}\r
- <div class="folded">\r
+ <div>\r
<h4><a href="#">Formation musicale</a></h4>\r
<dl class="listing">\r
{% if item.form_genr_style %}<dt>Forme / genre vernaculaire</dt><dd>{{ item.form_genr_style }}</dd>{% endif %}\r
</div>\r
<div class="extraInfos">\r
{% block document_identification %}\r
- <div class="folded">\r
+ <div>\r
<h4><a href="#">Identification du document</a></h4>\r
<dl class="listing">\r
{% if item.format %}<dt>Format</dt><dd>{{ item.format }}</dd>{% endif %}\r
</div>\r
<div class="extraInfos">\r
{% block document_data %}\r
- <div class="folded">\r
+ <div>\r
<h4><a href="#">Informations documentaires</a></h4>\r
<dl class="listing">\r
{% if item.choixcollecteur %}<dt>choixcollecteur</dt><dd>{{ item.choixcollecteur }}</dd>{% endif %}\r
# >>> id_string = 'BM.2006.002.001--25__01-10'
# >>> source_dir = '/home/momo/music/wav/CNRSMH_2006_002_GUYANE'
# >>> source_file = 'CNRSMH_2006_002_001_10.wav'
-# >>> t = TelemetaWavImport(id_string, source_dir, source_file, source_file)
+# >>> t = TelemetaWavImport(id_string, source_dir, source_dir, source_file)
# >>> t.main()
import sys
import shutil
import datetime
-from telemeta.models import MediaItem
-from django.conf import settings
+from django.core.management import setup_environ
class TelemetaWavImport:
def __init__(self, id_string, source_dir, source_file=None):
+ from telemeta.models import MediaItem
self.item_media_root_dir = settings.MEDIA_ROOT + os.sep + 'items' + os.sep
+ #self.item_media_relative_dir = 'items' + os.sep
self.source_dir = source_dir
self.source_files = os.listdir(self.source_dir)
self.source_file = source_file
self.item_list = MediaItem.objects.filter(id__startswith=id_string)
self.collection_id = self.item_list[0].collection_id
self.year = datetime.datetime.now().strftime("%Y")
+ self.buffer_size = 0xFFFF
+ #self.dest_relative_dir = self.item_media_relative_dir + self.year + os.sep + self.collection_id + os.sep
self.dest_dir = self.item_media_root_dir + self.year + os.sep + self.collection_id + os.sep
-
- def copy_files(self):
+
+ def copy_files(self)
+ print self.dest_dir
if not os.path.exists(self.dest_dir):
os.makedirs(self.dest_dir)
for file in self.source_files:
- if not os.path.exists(self.dest_dir + file):
- shutil.copy(self.source_dir + os.sep + file, self.dest_dir)
+ if not os.path.exists(self.dest_root_dir + file):
+ shutil.copy(self.source_dir + os.sep + file, self.dest_root_dir)
def wav_import(self):
if not self.source_file:
self.files.sort()
else:
self.files = [self.source_file]
-
print self.files
+
i = 0
if len(self.files) >= len(self.item_list):
for item in self.item_list:
#item = MediaItem.objects.get(id=object.id)
print item.id + " : " + item.title + " : "
- item.file._name = unicode(self.dest_dir + self.files[i])
+ f = open(self.files[i], 'r')
+ for chunk in f.read(self.buffer_size):
+ if len(chunk) == 0:
+ break
+ else:
+ item.file.write(chunk)
item.save()
- print item.file._name
+ print item.file.name
+ #item.file.write = unicode(self.dest_dir + self.files[i])
i += 1
def main(self):
- self.copy_files()
+ #self.copy_files()
self.wav_import()
+
+
+def print_usage(tool_name):
+ print "Usage: "+tool_name+" <project_dir> <id_string> <source_dir> [<source_file>]"
+ print " project_dir: the directory of the Django project which hosts Telemeta"
+ print " id_string: the string to filter in the id table"
+ print " source_dir: the directory containing the wav files to include"
+ print " source_file: the wav file to include (optional)"
+
+def run():
+ if len(sys.argv) < 3:
+ print_usage(os.path.basename(sys.argv[0]))
+ sys.exit(1)
+ else:
+ project_dir = sys.argv[1]
+ id_string = sys.argv[2]
+ source_dir = sys.argv[3]
+ source_file = None
+ if len(sys.argv) == 5:
+ source_file = sys.argv[4]
+ #from django.conf import settings
+ import settings
+ sys.path.append(project_dir)
+ setup_environ(settings)
+ t = TelemetaWavImport(id_string, source_dir, source_file)
+ t.main()
-
\ No newline at end of file
+if __name__ == '__main__':
+ run()
+
\ No newline at end of file