web/ldt/ldt_utils/fileimport.py
author ymh <ymh.work@gmail.com>
Thu, 10 Jun 2010 18:56:59 +0200
changeset 16 b3692a42ac79
parent 10 84e31387a741
child 41 73753ea1dcef
child 54 24397932219d
permissions -rw-r--r--
version + add indent and callback
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import tempfile
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import os.path
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import shutil
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from django.core.exceptions import ObjectDoesNotExist
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
from ldt.utils import zipfileext
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from django.conf import settings
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from models import Content
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import xml.dom.minidom
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import xml.dom.ext #@UnresolvedImport
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import xml.xpath #@UnresolvedImport
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import fnmatch
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
import uuid
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import urllib
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class FileImportError(Exception):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    def __init__(self, value):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        self.value = value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    def __str__(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        return repr(self.value)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
def Property(func):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    return property(**func()) 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
class IriInfo(object):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    def __init__(self, id, order, titledesc, basepath="", videopath=settings.STREAM_URL, decoupage_blacklist = settings.DECOUPAGE_BLACKLIST, flatten = True):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        self.id = id
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        self.basepath = basepath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        self.order = order
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        self.src = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        self.annotations = None
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        self.videopath = videopath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        self.videourl = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        self.title = None
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        self.desc = None
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        self.duration = None
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        self.created = False
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        self.content = None
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        self.decoupage_blacklist = decoupage_blacklist
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        if self.decoupage_blacklist is None:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            self.decoupage_blacklist = ()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        self.flatten = flatten
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    def processIri(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        # for just import a file ldt and get the title for every media
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        if 'http' in self.src:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
            url = urllib.urlopen(self.src)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            doc = xml.dom.minidom.parse(url)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            doc = Ft.Xml.Domlette.ConvertDocument(doc)             
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            con = xml.xpath.Context.Context(doc, 1, 1, None)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            #open .iri and get the title
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            self.title = res[0].value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        #for import a zip, get title and copy file .iri in the media directory
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        else:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            path = os.path.join(self.basepath, self.src)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            doc = xml.dom.minidom.parse(path)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            con = xml.xpath.Context.Context(doc, 1, 1, None)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            self.title = res[0].value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        res = xml.xpath.Evaluate("/iri/body/ensembles",context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        ensemblesnode = res[0]
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        ensembleids = []
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        for node in ensemblesnode.childNodes:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "ensemble":
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    76
                id = node.getAttributeNS(None,u"id")
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
                if id not in ensembleids:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
                    ensembleids.append(id)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        if self.annotations is not None:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            newEnsemble = None
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            for cnode in self.annotations.childNodes:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
                if cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "decoupage":
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
                    if newEnsemble is None:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
                        newensemble = doc.createElement('ensemble')
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
                        ensembleid = self.id+"_"+str(uuid.uuid1())
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
                        newensemble.setAttributeNS(None,'id',ensembleid)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
                        newensemble.setAttributeNS(None,'title', self.annotations.getAttribute('title'))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
                        newensemble.setAttributeNS(None,'author', self.annotations.getAttribute('author'))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
                        newensemble.setAttributeNS(None,'date', self.annotations.getAttribute('date'))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
                        newensemble.setAttributeNS(None,'abstract', self.annotations.getAttribute('abstract'))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
                        ensemblesnode.appendChild(newensemble)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
                        ensembleids.append(ensembleid)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
                    newDecoupageNode = cnode.cloneNode(True)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                    newensemble.appendChild(newDecoupageNode)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
                elif cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "ensemble":
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                    ensembleid = cnode.getAttribute(u"id")
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                    cloneNode = cnode.cloneNode(True)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                    if ensembleid in ensembleids:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
                        ensembleid = self.id+"_"+str(uuid.uuid1())
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
                        cloneNode.setAttribute(u"id", ensembleid)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                    ensembleids.append(ensembleid)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
                    ensemblesnode.appendChild(cloneNode)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
        if self.flatten:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
            src_video = res[0].getAttribute('src')
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
            self.videourl = os.path.basename(src_video)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
            res[0].setAttributeNS(None,'src', self.videourl)
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   111
        self.duration = res[0].getAttributeNS(None, u'dur')
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        f = open(path, "w")
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        try:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            xml.dom.ext.Print(doc, stream=f)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        finally:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
            f.close()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
        destPath = os.path.join(os.path.join(os.path.join(settings.MEDIA_ROOT, "media"), "ldt"), self.id);
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        if not os.path.exists(destPath):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            os.makedirs(destPath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        shutil.move(os.path.join(self.basepath, self.src), os.path.join(destPath, os.path.basename(self.src)))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        self.src = self.id + u"/" + os.path.basename(self.src)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    def saveContent(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
        #if 'http' in self.src:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
        #    url = self.src
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        #else:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        #    url = self.id + u"/" + os.path.basename(self.src)
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   133
        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})
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        if not self.created:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
            content.iriurl = self.src
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
            content.title = self.title
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
            content.description = self.desc
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
            content.save()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        content.iriurl = self.src
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
        content.videopath = self.videopath.rstrip("/") + "/"
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
        content.iri = self.id + u"/" + os.path.basename(self.src)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
        content.title = self.title
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
        content.description = self.desc
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
        content.duration = int(self.duration)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
        content.save()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
        self.content = content
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
    def process(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        self.processIri()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
        self.saveContent()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
class BaseFileImport(object):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
    def __init__(self, filepath, videopath):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
        self.__filepath = filepath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
        self.__tempdir = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
        self.__videopath = videopath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        self.__author = None
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
    def filepath(): #@NoSelf
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
        doc = """Docstring""" #@UnusedVariable
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
       
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
        def fget(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
            return self.__filepath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        def fset(self, value):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
            self.__filepath = value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
        def fdel(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
            del self.__filepath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
        return locals()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
       
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
    filepath = property(**filepath())
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
    def videopath(): #@NoSelf
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        doc = """Docstring""" #@UnusedVariable
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
       
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
        def fget(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
            return self.__videopath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        def fset(self, value):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
            self.__videopath = value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
        def fdel(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
            del self.__videopath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        return locals()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
       
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
    videopath = property(**videopath())
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
    def author(): #@NoSelf
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
        doc = """Docstring""" #@UnusedVariable
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
       
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
        def fget(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
            return self.__author
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
        def fset(self, value):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
            self.__author = value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
        def fdel(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
            del self.__author
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
        return locals()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    author = property(**author())
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
       
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
class FileImport(BaseFileImport):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
    def __init__(self, filepath, videopath, flatten):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
        BaseFileImport.__init__(self, filepath, videopath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
        self.__checkExistingMedia = False
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
        self.__flatten = flatten
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    def checkExistingMedia(): #@NoSelf
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
        doc = """Docstring""" #@UnusedVariable
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
       
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        def fget(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
            return self.__checkExistingMedia
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
        def fset(self, value):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
            self.__checkExistingMedia = value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
        def fdel(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
            del self.__checkExistingMedia
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        return locals()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
    checkExistingMedia = property(**checkExistingMedia())    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
    def flatten(): #@NoSelf
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
        doc = """Docstring""" #@UnusedVariable
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
       
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
        def fget(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
            return self.__flatten
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
        def fset(self, value):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
            self.__flatten = value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        def fdel(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
            del self.__flatten
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
        return locals()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
    flatten = property(**flatten())
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
    def processLdt(self, ldtpath=None):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
        # list iri
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
        # see if there is some comments
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
        # inject comment in iri
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
        # copy iri in folder
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
        # create or update content
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        contents = {}
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
        if ldtpath:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
            doc = xml.dom.minidom.parse(ldtpath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
        else:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
            doc = xml.dom.minidom.parse(self.filepath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        con = xml.xpath.Context.Context(doc, 1, 1, None)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        #get author from file ldt
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
        result = xml.xpath.Evaluate("/iri/project", context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
        for pnode in result:
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   269
            author = pnode.getAttributeNS(None,u"user")
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
            if author:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
                self.author = unicode(author)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
                break 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
        result = xml.xpath.Evaluate("/iri/medias/media", context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
        for i, medianode in  enumerate(result):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
        # get iri file's id from file ldt
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
            id = medianode.attributes['id'].value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
            if self.checkExistingMedia:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
                try:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
                    Content.objects.get(iri_id=id)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
                    do_pass = True
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
                except ObjectDoesNotExist: #Content.DoesNotExist
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
                    do_pass = False
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
            else:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
                    do_pass = False
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
            if not do_pass:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
                if not (contents.has_key(id)):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
                    # Create instance iriInfo(id, order, titledesc, basepath="", videopath=settings.STREAM_URL)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
                    if ldtpath:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
                        contents[id] = IriInfo(id, i, "", os.path.dirname(ldtpath), flatten=self.flatten)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
                    else: 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
                        contents[id] = IriInfo(id, i, "", flatten=self.flatten)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
                    # Get iri file's url from ldt. This url can be relative path or absolute path.
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
                contents[id].src = medianode.attributes['src'].value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
                if medianode.attributes['video'].value !="":
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
                    contents[id].videopath = medianode.attributes['video'].value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
                elif self.videopath !="" or self.videopath:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
                    contents[id].videopath = self.videopath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
                else:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
                    contents[id].videopath =settings.STREAM_URL
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
                    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
        #get annotation of file ldt
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
        result = xml.xpath.Evaluate("/iri/annotations/content", context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
        for contentnode in result:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
            id = contentnode.attributes['id'].value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
            # pocketfilms.utils.log.debug("ID : " + str(id))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
            if contents.has_key(id):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
                if self.author:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
                    contentnode.setAttributeNS(None,"author", unicode(self.author))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
                contents[id].annotations = contentnode
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
        #go throught values
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
        for iriinfo in contents.values():
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
            iriinfo.process()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
            # if yes update
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
            # if no create
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
            # move iri file to the proper place
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
            #return list of iriInfo
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
    def processFile(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
        if self.filepath.name.endswith(".ldt"):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
            self.processLdt()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
        elif self.filepath.name.endswith(".zip"):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
            self.processZip()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
        else:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
            raise FileImportError("Bad file type")
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
    def processZip(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
    # """ extraire .zip, pass to method processLdt"""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
        # create temp directory
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
        self.__tempdir = tempfile.mkdtemp()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
        openfiles = []
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
        flvfiles = []
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
        processedids = []
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
        try:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
            zipfile = zipfileext.ZipFileExt(self.filepath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
            zipfile.unzip_into_dir(self.__tempdir)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
            #load ldt
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
            foldersToProcess = [self.__tempdir]
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
            while len(foldersToProcess):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
                # pocketfilms.utils.log.debug("folder stack length : "+ str(len(foldersToProcess)))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
                currentFolder = foldersToProcess.pop()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
                for entry in os.listdir(currentFolder):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
                    if entry in settings.ZIP_BLACKLIST:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
                        continue
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
                    entryPath = os.path.join(currentFolder, entry)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
                    if(os.path.isdir(entryPath)):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
                        # pocketfilms.utils.log.debug("Push folder : " + entryPath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
                        foldersToProcess.append(entryPath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
                    elif fnmatch.fnmatch(entry, "*.ldt"):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
                        # pocketfilms.utils.log.debug("Process file : " + entryPath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
                        ldtid = self.processLdt(entryPath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
                        processedids.append(ldtid)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
                    elif fnmatch.fnmatch(entry, "*.flv"):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
                        flvfiles.append(entryPath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
             
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
            if settings.CONTENT_ROOT and os.path.exists(settings.CONTENT_ROOT): 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
                for entry in flvfiles:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
                    shutil.copy(entry, settings.CONTENT_ROOT)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
                    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
        finally:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
            for f in openfiles:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
                f.close()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
            shutil.rmtree(self.__tempdir)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
        # delete directory
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
        return processedids
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
    # def processFileLdt(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
        # processedids = []
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
        # ldtid = self.processLdt(self.filepath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
        # processedids.append(ldtid)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
        # return processedids