From: yomguy Date: Sun, 4 Apr 2010 01:06:42 +0000 (+0000) Subject: - adapt wav_import to multiple items from CSV files X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=f6b03053a61348f7e7e192a91e862cd7acbf5fc9;p=telemeta-data.git - adapt wav_import to multiple items from CSV files - fix file writing with new django objects - update new codes of the imported media items git-svn-id: http://svn.parisson.org/svn/crem@167 3bf09e05-f825-4182-b9bc-eedd7160adf0 --- diff --git a/trunk/import/audio_import/wav_import.py b/trunk/import/audio_import/wav_import.py index 8333b67..5dfec85 100644 --- a/trunk/import/audio_import/wav_import.py +++ b/trunk/import/audio_import/wav_import.py @@ -1,6 +1,7 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # -# Copyright (C) 2008 Parisson +# Copyright (C) 2010 Guillaume Pellerin # All rights reserved. # # This software is licensed as described in the file COPYING, which @@ -9,110 +10,88 @@ # # Author: Guillaume Pellerin # -# usage example : -# -# $ python manage.py shell -# >>> from wav_import import TelemetaWavImport -# >>> id_string = 'BM.2006.002.001--25__01-0' -# >>> source_dir = '/home/momo/music/wav/CNRSMH_2006_002_GUYANE' -# >>> t = TelemetaWavImport(id_string, source_dir) -# >>> t.main() -# -# or : -# -# $ python manage.py shell -# >>> from wav_import import TelemetaWavImport -# >>> 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_dir, source_file) -# >>> t.main() - import os import sys -import shutil +import csv +import logging import datetime from django.core.management import setup_environ +from django.core.files.base import ContentFile + +class Logger: + + def __init__(self, file): + self.logger = logging.getLogger('myapp') + self.hdlr = logging.FileHandler(file) + self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') + self.hdlr.setFormatter(self.formatter) + self.logger.addHandler(self.hdlr) + self.logger.setLevel(logging.INFO) + + def write_info(self, prefix, message): + self.logger.info(' ' + prefix + ' : ' + message.decode('utf8')) + + def write_error(self, prefix, message): + self.logger.error(prefix + ' : ' + message.decode('utf8')) + 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 + def __init__(self, source_dir, log_file): + self.logger = Logger(log_file) 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) - 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_root_dir + file): - shutil.copy(self.source_dir + os.sep + file, self.dest_root_dir) - + self.collections = os.listdir(self.source_dir) + self.buffer_size = 0x1000 + def wav_import(self): - if not self.source_file: - self.files = os.listdir(self.dest_dir) - 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 + " : " - 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 - #item.file.write = unicode(self.dest_dir + self.files[i]) - i += 1 - - def main(self): - #self.copy_files() - self.wav_import() + from telemeta.models import MediaItem + for collection in self.collections: + self.collection_name = collection.split(os.sep)[-1] + msg = '************************ ' + collection + ' ******************************' + self.logger.write_info(collection, msg[:70]) + + collection_files = os.listdir(self.source_dir + os.sep + collection) + if not collection + '.csv' in collection_files: + msg = 'Le fichier CSV est mal nommé ou inexistant' + self.logger.write_error(collection.dir, msg) + else: + c = csv.reader(open(self.source_dir + os.sep + collection + os.sep + collection + '.csv'), delimiter=';') + for row in c: + old_ref = row[0] + new_ref = row[1] + filename = new_ref + '.wav' + wav_file = self.source_dir + os.sep + collection + os.sep + filename + item = MediaItem.objects.filter(old_code=old_ref)[0] + print item.old_code + ' : id = ' + str(item.id) + " : title = " + item.title + print item.file.path + ' : ' + item.file.name + + f = open(wav_file, 'r') + file_content = ContentFile(f.read()) + item.file.save(filename, file_content) + f.close() + item.code = new_ref + item.save() def print_usage(tool_name): - print "Usage: "+tool_name+" []" + print "Usage: "+tool_name+" " 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 + project_dir = sys.argv[-3] + source_dir = sys.argv[-2] + log_file = sys.argv[-1] sys.path.append(project_dir) + import settings setup_environ(settings) - t = TelemetaWavImport(id_string, source_dir, source_file) - t.main() - + t = TelemetaWavImport(source_dir, log_file) + t.wav_import() + if __name__ == '__main__': run() - \ No newline at end of file