# HG changeset patch # User cavaliet # Date 1360951395 -3600 # Node ID 277ebc746baf9df05eeb113cbf58a5a5ae46e236 # Parent 92e53085020025e886e45162fff3c5f097f39f6b command to export data to xml files. diff -r 92e530850200 -r 277ebc746baf src/egonomy/management/commands/export_to_xml.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/egonomy/management/commands/export_to_xml.py Fri Feb 15 19:03:15 2013 +0100 @@ -0,0 +1,141 @@ +# -*- coding: utf-8 -*- +''' +Created on Jan 31, 2013 + +@author: ymh +''' + +from ..utils import show_progress +from django.core.management.base import BaseCommand, CommandError +from django.conf import settings +from egonomy.models import Image, ImageInfo, ImageMetadata +import os.path + + +class Command(BaseCommand): + ''' + Export data in xml files + ''' + + args = 'path_to_folder' + help = 'Export data to xml in senseetive format. Only one argument is needed : the folder where xml files will be saved.' + + + def __safe_get(self, dict_arg, key, conv = lambda x: x, default= None): + val = dict_arg.get(key, default) + return conv(val) if val else default + + def __safe_decode(self, s): + if not isinstance(s, basestring): + return s + try: + return s.decode('utf8') + except: + try: + return s.decode('latin1') + except: + return s.decode('utf8','replace') + + def handle(self, *args, **options): + """ + + + T + A + KW 1/keyword> + KW 2 + KW 3 + + + name of metadata 1 + value of metadata 1 + + + name of metadata 2 + name of metadata 2 + + + + """ + if len(args) != 1: + raise CommandError("The command has no argument or too much arguments. Only one is needed : the folder where xml files will be saved.") + + path = args[0].rstrip(os.sep) + os.sep + + do_export = True + if (not os.listdir(path)==[]) and not (len(os.listdir(path))==1 and os.listdir(path)[0]=='.DS_Store'): + confirm = raw_input(""" + The folder already contains files or subfolders. The command will create a LOT of folders and xml files. Do you want to continue ? + + Type 'y' to continue, or 'n' to quit: """) + do_export = (confirm == "y") + + if do_export: + print("Beginning...") + writer = None + counter = 0 + print("Getting info from images...") + img_list = Image.objects.all().select_related('info', 'metadata') + nb_img = img_list.count() + print("Creating xml files from %d images..." % nb_img) + writer = show_progress(counter, nb_img, "", 40, writer) + errors = [] + for img in img_list: + counter += 1 + # An img.info.image_file path is like "images/XX/YYY.jpg" so we split and create XX folder and YYY.xml file path + if img.info and img.info.image_file: + file_array = img.info.image_file.name.split("/") + file_folder = path + file_array[-2] + file_name = file_array[-1].split(".")[0] + ".xml" + if not os.path.isdir(file_folder): + os.makedirs(file_folder) + file_path = file_folder + os.sep + file_name + else: + # If image_file is none, the xml file is put in path folder + file_path = path + img.id + ".xml" + errors.append(img.id + ".xml") + + if os.path.exists(file_path): + os.remove(file_path) + + # Open file + fd = os.open(file_path, os.O_RDWR|os.O_CREAT) + # Build xml string + xml_str = u"\n\n\t" + (img.metadata.titre if img.metadata.titre else "") + u"\n\t" + (img.metadata.description if img.metadata.description else "") + u"\n" + # tags + if img.metadata.mots_cles: + tags = img.metadata.mots_cles.split(",") + for t in tags: + xml_str += u"\t" + t.strip() + u"\n" + # metadatas + xml_str += u"\t\n\t\tcliche\n\t\t" + (img.metadata.cliche if img.metadata.cliche else "") + u"\n\t\n" + xml_str += u"\t\n\t\tinventaire\n\t\t" + (img.metadata.inventaire if img.metadata.inventaire else "") + u"\n\t\n" + xml_str += u"\t\n\t\tdate\n\t\t" + str((img.metadata.date if img.metadata.date else "")) + u"\n\t\n" + xml_str += u"\t\n\t\tlongueur\n\t\t" + str((img.metadata.longueur if img.metadata.longueur else "")) + u"\n\t\n" + xml_str += u"\t\n\t\thauteur\n\t\t" + str((img.metadata.hauteur if img.metadata.hauteur else "")) + u"\n\t\n" + xml_str += u"\t\n\t\tprofondeur\n\t\t" + str((img.metadata.profondeur if img.metadata.profondeur else "")) + u"\n\t\n" + xml_str += u"\t\n\t\tdiametre\n\t\t" + str((img.metadata.diametre if img.metadata.diametre else "")) + u"\n\t\n" + xml_str += u"\t\n\t\tphotographe\n\t\t" + (img.metadata.photographe if img.metadata.photographe else "") + u"\n\t\n" + xml_str += u"\t\n\t\tauteur\n\t\t" + (img.metadata.auteur if img.metadata.auteur else "") + u"\n\t\n" + xml_str += u"\t\n\t\tdroits\n\t\t" + (img.metadata.droits if img.metadata.droits else "") + u"\n\t\n" + xml_str += u"\t\n\t\tmentions\n\t\t" + (img.metadata.mentions if img.metadata.mentions else "") + u"\n\t\n" + xml_str += u"\t\n\t\tperiode\n\t\t" + (img.metadata.periode if img.metadata.periode else "") + u"\n\t\n" + xml_str += u"\t\n\t\ttechnique\n\t\t" + (img.metadata.technique if img.metadata.technique else "") + u"\n\t\n" + xml_str += u"\t\n\t\tsite\n\t\t" + (img.metadata.site if img.metadata.site else "") + u"\n\t\n" + xml_str += u"\t\n\t\tlieu\n\t\t" + (img.metadata.lieu if img.metadata.lieu else "") + u"\n\t\n" + xml_str += u"\t\n\t\tlocalisation\n\t\t" + (img.metadata.localisation if img.metadata.localisation else "") + u"\n\t\n" + xml_str += u"" + # Write xml string + os.write(fd, xml_str.encode('utf8')) + # Close opened file + os.close(fd) + # Show progress + writer = show_progress(counter, nb_img, u"%s" % (file_name), 40, writer) + # Show "errors" at the end : img that did not have image_file data and whom xml are put in root path folder. + if len(errors)>0: + print("Some entires had no image file and folder. The following xml files are in root folder %s :" % path) + for e in errors: + print(e) + print("End of export_to_xml command.") + \ No newline at end of file