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