web/ldt/ldt_utils/fileimport.py
author wakimd
Fri, 15 Oct 2010 12:36:43 +0200
changeset 94 9927a619d2b5
parent 62 39b2dab4f939
child 89 30c6e597a7de
permissions -rw-r--r--
Merge and corrections due to merge
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
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
     5
from models import Content, Media
0
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)
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   145
        defaults_media = {'src':unicode(self.videourl), 'title':unicode(self.title), 'description':unicode(self.desc), 'videopath': unicode(self.videopath.rstrip("/") + "/")}
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   146
        media, media_created = Media.objects.get_or_create(src=unicode(self.videourl), defaults = defaults_media)
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   147
        if not media_created:
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   148
            for key, value in defaults_media.items():
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   149
                setattr(media, key, value)
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   150
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   151
        media.save()
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   153
        defaults_content = { 
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   154
            'iriurl': unicode(self.src), 
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   155
            'title':unicode(self.title),
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   156
            'description':unicode(self.desc),
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   157
            'media':media,
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   158
            'iri':unicode(self.id + u"/" + os.path.basename(self.src)),
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   159
            'duration':int(self.duration)
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   160
        }
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   161
        content, self.created = Content.objects.get_or_create(iri_id=self.id, defaults = defaults_content)
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   162
        if not self.created:
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   163
            for key, value in defaults_content.items():
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   164
                setattr(content, key, value)
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   165
        
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
        content.save()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
        self.content = content
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
    def process(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        self.processIri()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        self.saveContent()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
class BaseFileImport(object):
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 __init__(self, filepath, videopath):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
        self.__filepath = filepath
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
        self.__tempdir = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        self.__videopath = videopath
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
        self.__author = None
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
    def filepath(): #@NoSelf
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
        doc = """Docstring""" #@UnusedVariable
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        def fget(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
            return self.__filepath
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
        def fset(self, value):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
            self.__filepath = value
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 fdel(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
            del self.__filepath
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
        return locals()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
    filepath = property(**filepath())
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
    def videopath(): #@NoSelf
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
        doc = """Docstring""" #@UnusedVariable
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
        def fget(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
            return self.__videopath
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
        def fset(self, value):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
            self.__videopath = value
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 fdel(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
            del self.__videopath
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
        return locals()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
    videopath = property(**videopath())
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
    def author(): #@NoSelf
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
        doc = """Docstring""" #@UnusedVariable
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
        def fget(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
            return self.__author
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
        def fset(self, value):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
            self.__author = value
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
        def fdel(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
            del self.__author
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
        return locals()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
    author = property(**author())
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
class FileImport(BaseFileImport):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
    def __init__(self, filepath, videopath, flatten):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
        BaseFileImport.__init__(self, filepath, videopath)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        self.__checkExistingMedia = False
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
        self.__flatten = flatten
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
    def checkExistingMedia(): #@NoSelf
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
        doc = """Docstring""" #@UnusedVariable
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
        def fget(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
            return self.__checkExistingMedia
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        def fset(self, value):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
            self.__checkExistingMedia = value
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 fdel(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
            del self.__checkExistingMedia
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        return locals()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
    checkExistingMedia = property(**checkExistingMedia())    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
    def flatten(): #@NoSelf
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
        doc = """Docstring""" #@UnusedVariable
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        def fget(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
            return self.__flatten
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
        def fset(self, value):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
            self.__flatten = value
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        def fdel(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
            del self.__flatten
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
        return locals()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
    flatten = property(**flatten())
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
    def processLdt(self, ldtpath=None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
        # list iri
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
        # see if there is some comments
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
        # inject comment in iri
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        # copy iri in folder
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
        # create or update content
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
        contents = {}
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   278
        filepath = ldtpath if ldtpath else self.filepath
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   279
        doc = lxml.etree.parse(filepath)
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   280
        #if ldtpath:
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   281
            #doc = xml.dom.minidom.parse(ldtpath)
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   282
        #    doc = lxml.etree.parse(ldtpath)
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   283
        #else:
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   284
            #doc = xml.dom.minidom.parse(self.filepath)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
        
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   286
        #con = xml.xpath.Context.Context(doc, 1, 1, None)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        #get author from file ldt
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   289
        #result = xml.xpath.Evaluate("/iri/project", context=con)
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   290
        result = doc.xpath("/iri/project")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
        for pnode in result:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   292
            #author = pnode.getAttributeNS(None,u"user")
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   293
            author = pnode.attrib[u"user"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
            if author:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
                self.author = unicode(author)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
                break 
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   298
        result = doc.xpath("/iri/medias/media")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
        for i, medianode in  enumerate(result):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
        # 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
   302
            #id = medianode.attributes['id'].value
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   303
            id = medianode.attrib['id']
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
            if self.checkExistingMedia:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
                try:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
                    Content.objects.get(iri_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
                    do_pass = True
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
                except ObjectDoesNotExist: #Content.DoesNotExist
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
                    do_pass = False
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
                    do_pass = False
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
            if not do_pass:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
                if not (contents.has_key(id)):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
                    # Create instance iriInfo(id, order, titledesc, basepath="", videopath=settings.STREAM_URL)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
                    if ldtpath:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
                        contents[id] = IriInfo(id, i, "", os.path.dirname(ldtpath), flatten=self.flatten)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
                    else: 
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
                        contents[id] = IriInfo(id, i, "", flatten=self.flatten)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
                    # 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
   320
                #contents[id].src = medianode.attributes['src'].value
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   321
                contents[id].src = medianode.attrib['src']
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   322
                if medianode.attrib['video'] !="":
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   323
                    contents[id].videopath = medianode.attrib['video']
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
                elif self.videopath !="" or self.videopath:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
                    contents[id].videopath = self.videopath
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
                else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
                    contents[id].videopath =settings.STREAM_URL
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
                    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
        #get annotation of file ldt
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   331
        result = doc.xpath("/iri/annotations/content")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
        for contentnode in result:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   334
            id = contentnode.attrib['id']
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
            if contents.has_key(id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
                if self.author:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   337
                    contentnode.set("author", unicode(self.author))
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
                contents[id].annotations = contentnode
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
        #go throught values
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
        for iriinfo in contents.values():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
            iriinfo.process()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
            # if yes update
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
            # if no create
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
            # move iri file to the proper place
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
            #return list of iriInfo
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
    def processFile(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
        if self.filepath.name.endswith(".ldt"):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
            self.processLdt()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
        elif self.filepath.name.endswith(".zip"):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
            self.processZip()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
        else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
            raise FileImportError("Bad file type")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
    def processZip(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
    # """ extraire .zip, pass to method processLdt"""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
        # create temp directory
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
        self.__tempdir = tempfile.mkdtemp()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
        openfiles = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
        flvfiles = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
        processedids = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
        try:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
            zipfile = zipfileext.ZipFileExt(self.filepath)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
            zipfile.unzip_into_dir(self.__tempdir)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
            #load ldt
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
            foldersToProcess = [self.__tempdir]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
            while len(foldersToProcess):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
                currentFolder = foldersToProcess.pop()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
                for entry in os.listdir(currentFolder):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
                    if entry in settings.ZIP_BLACKLIST:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
                        continue
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
                    entryPath = os.path.join(currentFolder, entry)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
                    if(os.path.isdir(entryPath)):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
                        foldersToProcess.append(entryPath)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
                    elif fnmatch.fnmatch(entry, "*.ldt"):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
                        ldtid = self.processLdt(entryPath)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
                        processedids.append(ldtid)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
                    elif fnmatch.fnmatch(entry, "*.flv"):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
                        flvfiles.append(entryPath)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
             
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
            if settings.CONTENT_ROOT and os.path.exists(settings.CONTENT_ROOT): 
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
                for entry in flvfiles:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
                    shutil.copy(entry, settings.CONTENT_ROOT)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
                    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
        finally:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
            for f in openfiles:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
                f.close()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
            shutil.rmtree(self.__tempdir)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
        # delete directory
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
        return processedids
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
    # def processFileLdt(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
        # processedids = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
        # ldtid = self.processLdt(self.filepath)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
        # processedids.append(ldtid)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
        # return processedids