diff -r a6c4ea119b80 -r eac14c3ae625 web/ldt/ldt_utils/fileimport.py --- a/web/ldt/ldt_utils/fileimport.py Fri Aug 06 17:19:37 2010 +0200 +++ b/web/ldt/ldt_utils/fileimport.py Thu Aug 19 12:15:27 2010 +0200 @@ -1,16 +1,15 @@ -import tempfile -import os.path -import shutil +from copy import deepcopy +from django.conf import settings from django.core.exceptions import ObjectDoesNotExist from ldt.utils import zipfileext -from django.conf import settings from models import Content -import xml.dom.minidom -import xml.dom.ext #@UnresolvedImport -import xml.xpath #@UnresolvedImport import fnmatch +import lxml.etree +import os.path +import shutil +import tempfile +import urllib import uuid -import urllib class FileImportError(Exception): def __init__(self, value): @@ -48,68 +47,84 @@ def processIri(self): # for just import a file ldt and get the title for every media if 'http' in self.src: - url = urllib.urlopen(self.src) - doc = xml.dom.minidom.parse(url) + #url = urllib.urlopen(self.src) + path = url + #doc = xml.dom.minidom.parse(url) #for import a zip, get title and copy file .iri in the media directory else: path = os.path.join(self.basepath, self.src) - doc = xml.dom.minidom.parse(path) + #doc = xml.dom.minidom.parse(path) + + doc = lxml.etree.parse(path) - doc = Ft.Xml.Domlette.ConvertDocument(doc) - con = xml.xpath.Context.Context(doc, 1, 1, None) + #doc = Ft.Xml.Domlette.ConvertDocument(doc) + #con = xml.xpath.Context.Context(doc, 1, 1, None) - res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con) - self.title = res[0].value + res = doc.xpath("/iri/head/meta[@name='title']/@content") + #res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con) + #self.title = res[0].value + self.title = res[0] - res = xml.xpath.Evaluate("/iri/body/ensembles",context=con) + #res = xml.xpath.Evaluate("/iri/body/ensembles",context=con) + res = doc.xpath("/iri/body/ensembles") ensemblesnode = res[0] ensembleids = [] - for node in ensemblesnode.childNodes: - if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "ensemble": - id = node.getAttributeNS(None,u"id") + for node in ensemblesnode: #ensemblesnode.childNodes: + #if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "ensemble": + if node.tag == "ensemble": + #id = node.getAttributeNS(None,u"id") + id = node.attrib["id"] if id not in ensembleids: ensembleids.append(id) if self.annotations is not None: newEnsemble = None - for cnode in self.annotations.childNodes: - if cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "decoupage": + #for cnode in self.annotations.childNodes: + for cnode in self.annotations: + #if cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "decoupage": + if cnode.tag == "decoupage": if newEnsemble is None: - newensemble = doc.createElement('ensemble') + #newensemble = doc.createElementNS(None,'ensemble') ensembleid = self.id+"_"+str(uuid.uuid1()) - newensemble.setAttributeNS(None,'id',ensembleid) - - newensemble.setAttributeNS(None,'title', self.annotations.getAttribute('title')) - newensemble.setAttributeNS(None,'author', self.annotations.getAttribute('author')) - newensemble.setAttributeNS(None,'date', self.annotations.getAttribute('date')) - newensemble.setAttributeNS(None,'abstract', self.annotations.getAttribute('abstract')) - ensemblesnode.appendChild(newensemble) + newensemble = lxml.etree.SubElement(ensemblesnode, + 'ensemble', + {'id' : ensembleid, + 'title' : self.annotations.get('title') or "", + 'author' : self.annotations.get('author') or "", + 'date' : self.annotations.get('date') or "", + 'abstract' : self.annotations.get('abstract') or "" + } + ) ensembleids.append(ensembleid) - newDecoupageNode = cnode.cloneNode(True) - newensemble.appendChild(newDecoupageNode) - elif cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "ensemble": - ensembleid = cnode.getAttribute(u"id") - cloneNode = cnode.cloneNode(True) + newDecoupageNode = deepcopy(cnode) + newensemble.append(newDecoupageNode) + #elif cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "ensemble": + elif cnode.tag == "ensemble": + #ensembleid = cnode.getAttribute(u"id") + ensembleid = cnode.attrib['id'] + cloneNode = deepcopy(cnode) if ensembleid in ensembleids: ensembleid = self.id+"_"+str(uuid.uuid1()) - cloneNode.setAttribute(u"id", ensembleid) + cloneNode.set(u"id", ensembleid) ensembleids.append(ensembleid) - ensemblesnode.appendChild(cloneNode) + ensemblesnode.append(cloneNode) - res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con) + res = doc.xpath("/iri/body/medias/media[@id='video']/video") if self.flatten: - src_video = res[0].getAttributeNS(None,'src') + src_video = res[0].get('src') self.videourl = os.path.basename(src_video) - res[0].setAttributeNS(None,'src', self.videourl) - self.duration = res[0].getAttributeNS(None, u'dur') + res[0].set('src', self.videourl) + self.duration = res[0].get(u'dur') f = open(path, "w") try: - xml.dom.ext.Print(doc, stream=f) + #etree = lxml.etree.ElementTree(doc) + doc.write(f, encoding="UTF-8", pretty_print=True, xml_declaration=True) +# xml.dom.ext.Print(doc, stream=f) finally: f.close() @@ -127,19 +142,19 @@ # url = self.src #else: # url = self.id + u"/" + os.path.basename(self.src) - content, self.created = Content.objects.get_or_create(iri_id=self.id, defaults = {'src':self.videourl, 'iriurl': self.src, 'title':self.title, 'description':self.desc, 'videopath': self.videopath}) + content, self.created = Content.objects.get_or_create(iri_id=self.id, defaults = {'src':unicode(self.videourl), 'iriurl': unicode(self.src), 'title':unicode(self.title), 'description':unicode(self.desc), 'videopath': unicode(self.videopath)}) if not self.created: - content.iriurl = self.src - content.title = self.title - content.description = self.desc + content.iriurl = unicode(self.src) + content.title = unicode(self.title) + content.description = unicode(self.desc) content.save() - content.iriurl = self.src - content.videopath = self.videopath.rstrip("/") + "/" + content.iriurl = unicode(self.src) + content.videopath = unicode(self.videopath.rstrip("/") + "/") - content.iri = self.id + u"/" + os.path.basename(self.src) - content.title = self.title - content.description = self.desc + content.iri = unicode(self.id + u"/" + os.path.basename(self.src)) + content.title = unicode(self.title) + content.description = unicode(self.desc) content.duration = int(self.duration) content.save() @@ -253,26 +268,32 @@ # copy iri in folder # create or update content contents = {} - if ldtpath: - doc = xml.dom.minidom.parse(ldtpath) - else: - doc = xml.dom.minidom.parse(self.filepath) + filepath = ldtpath if ldtpath else self.filepath + doc = lxml.etree.parse(filepath) + #if ldtpath: + #doc = xml.dom.minidom.parse(ldtpath) + # doc = lxml.etree.parse(ldtpath) + #else: + #doc = xml.dom.minidom.parse(self.filepath) - con = xml.xpath.Context.Context(doc, 1, 1, None) + #con = xml.xpath.Context.Context(doc, 1, 1, None) #get author from file ldt - result = xml.xpath.Evaluate("/iri/project", context=con) + #result = xml.xpath.Evaluate("/iri/project", context=con) + result = doc.xpath("/iri/project") for pnode in result: - author = pnode.getAttributeNS(None,u"user") + #author = pnode.getAttributeNS(None,u"user") + author = pnode.attrib[u"user"] if author: self.author = unicode(author) break - result = xml.xpath.Evaluate("/iri/medias/media", context=con) + result = doc.xpath("/iri/medias/media") for i, medianode in enumerate(result): # get iri file's id from file ldt - id = medianode.attributes['id'].value + #id = medianode.attributes['id'].value + id = medianode.attrib['id'] if self.checkExistingMedia: try: Content.objects.get(iri_id=id) @@ -289,9 +310,10 @@ else: contents[id] = IriInfo(id, i, "", flatten=self.flatten) # Get iri file's url from ldt. This url can be relative path or absolute path. - contents[id].src = medianode.attributes['src'].value - if medianode.attributes['video'].value !="": - contents[id].videopath = medianode.attributes['video'].value + #contents[id].src = medianode.attributes['src'].value + contents[id].src = medianode.attrib['src'] + if medianode.attrib['video'] !="": + contents[id].videopath = medianode.attrib['video'] elif self.videopath !="" or self.videopath: contents[id].videopath = self.videopath else: @@ -299,14 +321,15 @@ #get annotation of file ldt - result = xml.xpath.Evaluate("/iri/annotations/content", context=con) + #result = xml.xpath.Evaluate("/iri/annotations/content", context=con) + result = doc.xpath("/iri/annotations/content") for contentnode in result: - id = contentnode.attributes['id'].value + id = contentnode.attrib['id'] # pocketfilms.utils.log.debug("ID : " + str(id)) if contents.has_key(id): if self.author: - contentnode.setAttributeNS(None,"author", unicode(self.author)) + contentnode.set("author", unicode(self.author)) contents[id].annotations = contentnode #go throught values