web/ldt/ldt_utils/projectserializer.py
changeset 49 55f91a1f9df8
parent 26 5442e8569ff0
child 85 3b70d84e661a
equal deleted inserted replaced
48:7b78d3911aa1 49:55f91a1f9df8
     1 import xml.dom
     1 import lxml.etree
     2 import Ft.Xml.Domlette
       
     3 import xml.xpath
       
     4 import uuid
     2 import uuid
     5 from datetime import datetime
     3 from datetime import datetime
     6 from ldt.ldt_utils.models import Content, Project
     4 from ldt.ldt_utils.models import Content, Project
     7 
     5 
     8 DATE_FORMATS = ["%d/%m/%Y","%Y-%m-%d"]
     6 DATE_FORMATS = ["%d/%m/%Y","%Y-%m-%d"]
    23         self.lists = []
    21         self.lists = []
    24         
    22         
    25     
    23     
    26     def __parse_ensemble(self, ensemble_node, content):
    24     def __parse_ensemble(self, ensemble_node, content):
    27         
    25         
    28         ensemble_id = ensemble_node.getAttributeNS(None,u"id")
    26         ensemble_id = ensemble_node.attrib[u"id"]
    29         ensemble_author = ensemble_node.getAttributeNS(None,u"author")
    27         ensemble_author = ensemble_node.attrib[u"author"]
    30         ensemble_title = ensemble_node.getAttributeNS(None,u"title")
    28         ensemble_title = ensemble_node.attrib[u"title"]
    31         ensemble_description = ensemble_node.getAttributeNS(None,u"abstract")
    29         ensemble_description = ensemble_node.attrib[u"abstract"]
    32         ensemble_created = datetime.utcnow().isoformat()
    30         ensemble_created = datetime.utcnow().isoformat()
    33         ensemble_modified = ensemble_created 
    31         ensemble_modified = ensemble_created 
    34         
    32         
    35         list_items = []
    33         list_items = []
    36         new_list = {
    34         new_list = {
    47                 "editable":"false"
    45                 "editable":"false"
    48             }
    46             }
    49         }
    47         }
    50         
    48         
    51         
    49         
    52         for decoupage_node in ensemble_node.childNodes:
    50         for decoupage_node in ensemble_node:
    53             if decoupage_node.nodeType != xml.dom.Node.ELEMENT_NODE or decoupage_node.tagName != "decoupage" :
    51             if decoupage_node.tag != "decoupage" :
    54                 continue
    52                 continue
    55             
    53             
    56             decoupage_id = decoupage_node.getAttributeNS(None, u"id")
    54             decoupage_id = decoupage_node.attrib[ u"id"]
    57             decoupage_creator = decoupage_node.getAttributeNS(None,u"author")
    55             decoupage_creator = decoupage_node.attrib[u"author"]
    58             if not decoupage_creator:
    56             if not decoupage_creator:
    59                 decoupage_creator = "IRI"
    57                 decoupage_creator = "IRI"
    60             decoupage_contributor = decoupage_creator
    58             decoupage_contributor = decoupage_creator
    61             date_str = decoupage_node.getAttributeNS(None,u"date")
    59             date_str = decoupage_node.get(u"date")
    62             decoupage_created = None
    60             decoupage_created = None
    63             if date_str :
    61             if date_str :
    64                 for date_format in DATE_FORMATS:
    62                 for date_format in DATE_FORMATS:
    65                     try:
    63                     try:
    66                         decoupage_created = datetime.strptime(date_str,date_format).isoformat()
    64                         decoupage_created = datetime.strptime(date_str,date_format).isoformat()
    70             if decoupage_created is None:
    68             if decoupage_created is None:
    71                 decoupage_created = datetime.utcnow().isoformat()
    69                 decoupage_created = datetime.utcnow().isoformat()
    72             decoupage_modified = decoupage_created
    70             decoupage_modified = decoupage_created
    73             
    71             
    74             decoupage_title = ""
    72             decoupage_title = ""
    75             for txtRes in xml.xpath.Evaluate("title/text()", decoupage_node): 
    73             for txtRes in decoupage_node.xpath("title/text()", smart_strings=False): 
    76                     decoupage_title += txtRes.data
    74                     decoupage_title += txtRes
    77 
    75 
    78             decoupage_description = ""
    76             decoupage_description = ""
    79             for txtRes in xml.xpath.Evaluate("abstract/text()", decoupage_node): 
    77             for txtRes in decoupage_node.xpath("abstract/text()", smart_strings=False): 
    80                     decoupage_description += txtRes.data
    78                     decoupage_description += txtRes
    81             
    79             
    82 
    80 
    83             
    81             
    84             list_items.append({"id-ref":decoupage_id})
    82             list_items.append({"id-ref":decoupage_id})
    85             
    83             
    93                 "dc:description":decoupage_description
    91                 "dc:description":decoupage_description
    94             }
    92             }
    95             
    93             
    96             self.annotation_types.append(new_annotation_types)            
    94             self.annotation_types.append(new_annotation_types)            
    97                         
    95                         
    98             res = xml.xpath.Evaluate("elements/element", decoupage_node)
    96             res = decoupage_node.xpath("elements/element")
    99             for element_node in res:
    97             for element_node in res:
   100                 
    98                 
   101                 element_id = element_node.getAttributeNS(None,u"id")
    99                 element_id = element_node.attrib[u"id"]
   102                 element_begin = element_node.getAttributeNS(None,u"begin")
   100                 element_begin = element_node.attrib[u"begin"]
   103                 element_duration = element_node.getAttributeNS(None,u"dur")
   101                 element_duration = element_node.attrib[u"dur"]
   104                 element_media = content.iri_id
   102                 element_media = content.iri_id
   105                 element_color = element_node.getAttributeNS(None,u"color")
   103                 element_color = element_node.attrib[u"color"]
   106                 
   104                 
   107                 element_title = ""
   105                 element_title = ""
   108                 for txtRes in xml.xpath.Evaluate("title/text()", element_node): 
   106                 for txtRes in element_node.xpath("title/text()", smart_strings=False): 
   109                     element_title += txtRes.data 
   107                     element_title += txtRes
   110         
   108         
   111                 element_description = ""
   109                 element_description = ""
   112                 for txtRes in xml.xpath.Evaluate("abstract/text()", element_node): 
   110                 for txtRes in element_node.xpath("abstract/text()", smart_strings=False): 
   113                     element_description += txtRes.data 
   111                     element_description += txtRes
   114                 
   112                 
   115                 element_audio_src = ""
   113                 element_audio_src = ""
   116                 element_audio_href = ""
   114                 element_audio_href = ""
   117                 res = xml.xpath.Evaluate("audio", element_node)
   115                 res = element_node.xpath("audio")
   118                 if len(res) > 0:
   116                 if len(res) > 0:
   119                     element_audio_src = res[0].getAttributeNS(None, u"source")
   117                     element_audio_src = res[0].get(u"source",u"")
   120                     ltext = []
   118                     element_audio_href =  res[0].text                
   121                     for n in res[0].childNodes:
       
   122                         if n.nodeType in (dom.Node.TEXT_NODE, dom.Node.CDATA_SECTION_NODE):
       
   123                             ltext.append(n.data)
       
   124                     element_audio_href = ''.join(ltext)
       
   125                 
       
   126                 
   119                 
   127                 element_tags = []
   120                 element_tags = []
   128                 
   121                 
   129                 tags = element_node.getAttributeNS(None,u"tags")
   122                 tags = element_node.get(u"tags",u"")
   130                 
   123                 
   131                 tags_list = map(lambda s:s.strip(),tags.split(","))
   124                 tags_list = map(lambda s:s.strip(),tags.split(","))
   132 
   125 
   133                 #tags                                
   126                 #tags                                
   134                 if tags is None or len(tags) == 0:
   127                 if tags is None or len(tags) == 0:
   135                     tags_list = []
   128                     tags_list = []
   136                     restagnode = xml.xpath.Evaluate("tag/text()", element_node)
   129                     restagnode = element_node.xpath("tag/text()", smart_strings=False)
   137                     for tagnode in restagnode:
   130                     for tagnode in restagnode:
   138                         tags_list.append(tagnode.data)
   131                         tags_list.append(tagnode)
   139                         
   132                         
   140                 if tags_list is None or len(tags_list) == 0:
   133                 if tags_list is None or len(tags_list) == 0:
   141                     tags_list = []
   134                     tags_list = []
   142                     restagnode = xml.xpath.Evaluate("tags/tag/text()", element_node)
   135                     restagnode = element_node.xpath("tags/tag/text()", smart_strings=False)
   143                     for tagnode in restagnode:
   136                     for tagnode in restagnode:
   144                         tags_list.append(tagnode.data)
   137                         tags_list.append(tagnode)
   145                 
   138                 
   146                 tag_date = datetime.utcnow().isoformat()
   139                 tag_date = datetime.utcnow().isoformat()
   147                 for tag_title in tags_list:
   140                 for tag_title in tags_list:
   148                     if tag_title not in self.tags:
   141                     if tag_title not in self.tags:
   149                         tag_id = unicode(uuid.uuid1())
   142                         tag_id = unicode(uuid.uuid1())
   196         self.lists.append(new_list)
   189         self.lists.append(new_list)
   197 
   190 
   198 
   191 
   199     def __parse_ldt(self):
   192     def __parse_ldt(self):
   200         
   193         
   201         doc = xml.dom.minidom.parseString(self.project.ldt.encode("utf-8"))
   194         self.ldt_doc = lxml.etree.fromstring(self.project.ldt.encode("utf-8"))
   202         self.ldt_doc = Ft.Xml.Domlette.ConvertDocument(doc)
   195         
   203         con = xml.xpath.Context.Context(doc, 1, 1, None)
   196         res = self.ldt_doc.xpath("/iri/medias/media")
   204         
       
   205         res = xml.xpath.Evaluate("/iri/medias/media", context=con)
       
   206         for mediaNode in res:
   197         for mediaNode in res:
   207             iri_id = mediaNode.getAttributeNS(None,u"id")
   198             iri_id = mediaNode.attrib[u"id"]
   208             content = Content.objects.get(iri_id=iri_id)
   199             content = Content.objects.get(iri_id=iri_id)
   209             self.__parse_content(content)
   200             self.__parse_content(content)
   210             
   201             
   211         res = xml.xpath.Evaluate("/iri/annotations/content",context=con)               
   202         res = self.ldt_doc.xpath("/iri/annotations/content")               
   212         
   203         
   213         for content_node in res:
   204         for content_node in res:
   214             content_id = content_node.getAttributeNS(None, u"id")
   205             content_id = content_node.attrib[u"id"]
   215             content = Content.objects.get(iri_id=content_id)
   206             content = Content.objects.get(iri_id=content_id)
   216             for ensemble_node in content_node.childNodes:
   207             for ensemble_node in content_node:
   217                 if ensemble_node.nodeType != xml.dom.Node.ELEMENT_NODE or ensemble_node.tagName != "ensemble" :
   208                 if ensemble_node.tag != "ensemble" :
   218                     continue
   209                     continue
   219                 self.__parse_ensemble(ensemble_node, content)
   210                 self.__parse_ensemble(ensemble_node, content)            
   220             
       
   221         #res = xml.xpath.Evaluate("/iri/displays/display",context=con)
       
   222         
       
   223         #for display_node in res:
       
   224             
       
   225     
   211     
   226     
   212     
   227     def __parse_content(self, content):
   213     def __parse_content(self, content):
   228         
   214         
   229         doc = Ft.Xml.Domlette.ConvertDocument(xml.dom.minidom.parse(content.iri_file_path()))
   215         doc = lxml.etree.parse(content.iri_file_path())
   230         con = xml.xpath.Context.Context(doc, 1, 1, None)
       
   231         
   216         
   232         authors = content.authors.all()
   217         authors = content.authors.all()
   233         
   218         
   234         if len(authors) > 0 :
   219         if len(authors) > 0 :
   235             author = authors[0].handle
   220             author = authors[0].handle
   241         else :
   226         else :
   242             contributor = author
   227             contributor = author
   243         
   228         
   244         content_author = ""
   229         content_author = ""
   245         
   230         
   246         res = xml.xpath.Evaluate("/iri/head/meta[@name='author']/@content", context=con)
   231         res = doc.xpath("/iri/head/meta[@name='author']/@content")
   247         if len(res) > 0:
   232         if len(res) > 0:
   248             content_author = res[0].value
   233             content_author = res[0]
   249         
   234         
   250         
   235         
   251         content_date = ""
   236         content_date = ""
   252         
   237         
   253         res = xml.xpath.Evaluate("/iri/head/meta[@name='date']/@content", context=con)
   238         res = doc.xpath("/iri/head/meta[@name='date']/@content")
   254         if len(res) > 0:
   239         if len(res) > 0:
   255             content_date = res[0].value
   240             content_date = res[0]
   256 
   241 
   257         
   242         
   258         new_media = {
   243         new_media = {
   259              "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0",
   244              "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0",
   260              "id" : content.iri_id,
   245              "id" : content.iri_id,
   279         }
   264         }
   280         
   265         
   281         self.medias.append(new_media)
   266         self.medias.append(new_media)
   282         
   267         
   283         
   268         
   284         res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble",context=con)
   269         res = doc.xpath("/iri/body/ensembles/ensemble")
   285         
   270         
   286         for ensemble_node in res:
   271         for ensemble_node in res:
   287             self.__parse_ensemble(ensemble_node, content)
   272             self.__parse_ensemble(ensemble_node, content)
   288 
   273 
   289     
   274