web/ldt/ldt_utils/contentindexer.py
author ymh <ymh.work@gmail.com>
Thu, 10 Jun 2010 18:56:59 +0200
changeset 16 b3692a42ac79
parent 10 84e31387a741
permissions -rw-r--r--
version + add indent and callback
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import tempfile
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import os
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import os.path
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import shutil
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
from ldt.utils import zipfileext
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
import urllib
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
# import ldt.utils.log
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import ldt.utils.xml
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from django.conf import settings
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from models import Content
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import xml
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
import xml.dom
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import xml.dom.minidom
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
import xml.dom.ext
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
import xml.xpath
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
import fnmatch
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
import Ft
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
import uuid
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
import shutil
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
import lucene
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
from ldt.ldt_utils import STORE
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
from ldt.ldt_utils import ANALYZER
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
def Property(func):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    return property(**func()) 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
class ContentIndexer(object):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        def __init__(self, contentList, writer, decoupage_blackList = settings.DECOUPAGE_BLACKLIST):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
                self.__contentList = contentList
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
                self.__decoupage_blacklist = decoupage_blackList
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
                self.__writer = writer
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
                    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        @Property
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        def decoupage_blacklist(): #@NoSelf
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
            doc = """get blacklist""" #@UnusedVariable
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
            def fget(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
                if self.__decoupage_blacklist is None:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
                    self.__decoupage_blacklist = ()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
                return self.__decoupage_blacklist
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
               
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            def fset(self, value):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
                self.__decoupage_blacklist = value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
               
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            def fdel(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
                del self.__decoupage_blacklist
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
               
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
            return locals()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
                   
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        def index_all(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            for content in self.__contentList:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
                self.index_content(content)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        def index_content(self, content):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            url =content.iri_url()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            filepath = urllib.urlopen(url)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
            doc = xml.dom.minidom.parse(filepath)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            doc = Ft.Xml.Domlette.ConvertDocument(doc)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
                                   
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            self.__writer.deleteDocuments(lucene.Term("iri_id", content.iri_id))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            con = xml.xpath.Context.Context(doc, 1, 1, None)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble", context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            for ensemble in res:
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    68
                ensembleId = ensemble.getAttributeNS(None,u"id")
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
                for decoupageNode in ensemble.childNodes:
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    71
                    if decoupageNode.nodeType != xml.dom.Node.ELEMENT_NODE or decoupageNode.tagName != "decoupage"  or decoupageNode.getAttributeNS(None,u"id") in self.decoupage_blacklist:
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
                        continue
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
                    
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    74
                    decoupId = decoupageNode.getAttributeNS(None,u"id")
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
                    res = xml.xpath.Evaluate("elements/element", decoupageNode)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
                    for elementNode in res:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
                        doc = lucene.Document()
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    78
                        elementId = elementNode.getAttributeNS(None,u"id")
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    79
                        tags = elementNode.getAttributeNS(None,u"tags")
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
                        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
                        if tags is not None:                            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                            tags.replace(",", ";")
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
                        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
                        if tags is None or len(tags) == 0:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
                            tags = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
                            restagnode = xml.xpath.Evaluate("tag/text()", elementNode)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
                            for tagnode in restagnode:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
                                tags = tags + " ; " + tagnode.data
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
                                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
                        if tags is None or len(tags) == 0:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
                            tags = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
                            restagnode = xml.xpath.Evaluate("tags/tag/text()", elementNode)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
                            for tagnode in restagnode:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
                                tags = tags + " ; " + tagnode.data                            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                        title = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
                        for txtRes in xml.xpath.Evaluate("title/text()", elementNode): 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                            title = title + txtRes.data 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                        abstract = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
                        for txtRes in xml.xpath.Evaluate("abstract/text()", elementNode): 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
                            abstract = abstract + txtRes.data 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
                        doc.add(lucene.Field("iri_id", content.iri_id, lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
                        doc.add(lucene.Field("ensemble_id", ensembleId, lucene.Field.Store.YES, lucene.Field.Index.NO))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                        doc.add(lucene.Field("decoupage_id", decoupId, lucene.Field.Store.YES, lucene.Field.Index.NO))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
                        doc.add(lucene.Field("element_id", elementId, lucene.Field.Store.YES, lucene.Field.Index.NO))                        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
                        doc.add(lucene.Field("tags", tags, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
                        doc.add(lucene.Field("title", title, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
                        doc.add(lucene.Field("abstract", abstract, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
                        doc.add(lucene.Field("all", " ".join([tags, title, abstract]), lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
                        seg = Segment(content=content,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
                                      iri_id=content.iri_id,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
                                      ensemble_id=ensembleId,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
                                      cutting_id=decoupId,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
                                      element_id=elementId,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
                                      tags=tags,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
                                      title=title,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
                                      abstract=abstract,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
                                      duration=duration,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
                                      author=author,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
                                      start_ts=start_ts,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
                                      date=date_str)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
                        seg.save()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                        self.__writer.addDocument(doc)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
            self.__writer.commit()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
class ProjectIndexer(object):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
        def __init__(self, projectList, writer, decoupage_blackList = settings.DECOUPAGE_BLACKLIST):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
                self.__projectList = projectList
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
                self.__decoupage_blacklist = decoupage_blackList
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
                self.__writer = writer
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        @Property
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
        def decoupage_blacklist(): #@NoSelf
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
            doc = """get blacklist""" #@UnusedVariable
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
           
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
            def fget(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
                if self.__decoupage_blacklist is None:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
                    self.__decoupage_blacklist = ()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
                return self.__decoupage_blacklist
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
               
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
            def fset(self, value):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
                self.__decoupage_blacklist = value
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
               
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
            def fdel(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
                del self.__decoupage_blacklist
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
               
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
            return locals()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
                   
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
        def index_all(self):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
            for project in self.__projectList:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
                self.index_project(project)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        def index_project(self, project):
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
            # pocketfilms.utils.log.debug("Indexing project : "+str(project.iri_id))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
            doc = xml.dom.minidom.parseString(project.ldt)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
            doc = Ft.Xml.Domlette.ConvertDocument(doc) 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
            self.__writer.deleteDocuments(lucene.Term("iri_id", project.iri_id))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
            con = xml.xpath.Context.Context(doc, 1, 1, None)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
            res = xml.xpath.Evaluate("/iri/annotations/content", context=con)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
            for content in res:
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   173
                contentId = content.getAttributeNS(None,u"id")
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
                ensembleId = "ens_perso"
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
                for decoupageNode in content.childNodes:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
                    # pocketfilms.utils.log.debug("Indexing content decoupage : "+ repr(decoupageNode.nodeType) + " in " + repr(self.decoupage_blacklist))
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   179
                    if decoupageNode.nodeType != xml.dom.Node.ELEMENT_NODE or decoupageNode.tagName != "decoupage"  or decoupageNode.getAttributeNS(None,"id") in self.decoupage_blacklist:
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
                        continue
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
                    
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   182
                    decoupId = decoupageNode.getAttributeNS(None,u"id")
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
                    res = xml.xpath.Evaluate("elements/element", decoupageNode)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
                    for elementNode in res:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
                        doc = lucene.Document()
10
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   186
                        elementId = elementNode.getAttributeNS(None,u"id")
84e31387a741 correct serialization
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   187
                        tags = elementNode.getAttributeNS(None,u"tags")
5
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
                        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
                        if tags is not None:                            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
                            tags.replace(",", ";")
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
                        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
                        if tags is None or len(tags) == 0:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
                            tags = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
                            restagnode = xml.xpath.Evaluate("tag/text()", elementNode)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
                            for tagnode in restagnode:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
                                tags = tags + " ; " + tagnode.data
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
                                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
                        if tags is None or len(tags) == 0:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
                            tags = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
                            restagnode = xml.xpath.Evaluate("tags/tag/text()", elementNode)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
                            for tagnode in restagnode:
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
                                tags = tags + " ; " + tagnode.data                            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
                        title = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
                        for txtRes in xml.xpath.Evaluate("title/text()", elementNode): 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
                            title = title + txtRes.data 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
                        abstract = ""
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
                        for txtRes in xml.xpath.Evaluate("abstract/text()", elementNode): 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
                            abstract = abstract + txtRes.data 
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
                
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
                        doc.add(lucene.Field("project_id", project.iri_id, lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED))              
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
                        doc.add(lucene.Field("iri_id", contentId, lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
                        doc.add(lucene.Field("ensemble_id", ensembleId, lucene.Field.Store.YES, lucene.Field.Index.NO))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
                        doc.add(lucene.Field("decoupage_id", decoupId, lucene.Field.Store.YES, lucene.Field.Index.NO))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
                        doc.add(lucene.Field("element_id", elementId, lucene.Field.Store.YES, lucene.Field.Index.NO))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
                        doc.add(lucene.Field("tags", tags, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
                        doc.add(lucene.Field("title", title, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
                        doc.add(lucene.Field("abstract", abstract, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
                        doc.add(lucene.Field("all", " ".join([tags, title, abstract]), lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
                        
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
                        seg = Segment(content=content,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
                                      iri_id=content.iri_id,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
                                      ensemble_id=ensembleId,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
                                      cutting_id=decoupId,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
                                      element_id=elementId,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
                                      tags=tags,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
                                      title=title,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
                                      abstract=abstract,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
                                      duration=duration,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
                                      author=author,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
                                      start_ts=start_ts,
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
                                      date=date_str)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
                        seg.save()
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
                                    
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
                        self.__writer.addDocument(doc)
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
            
ae8593287883 correct error changing ldt.ldt to ldt.ldt_utils
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
            self.__writer.commit()