annot-server/utils.py
author rougeronj
Thu, 22 Jan 2015 09:26:43 +0100
changeset 111 a7b72620d227
parent 67 7db9c7ec691a
permissions -rw-r--r--
Add variable "wait". When this variable set, the annotsroll wait ignore some annotations, and wait before printing an otherone so there is no superposition. Can be passed as an options
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
#
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
# See LICENCE for detail
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
# Copyright (c) 2014 IRI
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
#
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
     7
import bisect
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
     8
import datetime
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
     9
import json
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    10
import logging
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    11
import re
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    12
import uuid
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    13
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    14
from dateutil.parser import parse as parse_date_raw
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    15
from dateutil.tz import tzutc
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    16
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    17
from lxml import etree
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    18
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    19
import requests
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    20
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    21
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    22
logger = logging.getLogger(__name__)
0
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
PIANOROLL_CHANNEL = 'PIANOROLL'
e1d4d7a8255a First shareable version
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
ANNOTATION_CHANNEL = 'ANNOT'
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    26
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    27
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    28
class AnnotationsSynchronizer(object):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    29
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    30
    LDT_CONTENT_REST_API_PATH = "api/ldt/1.0/contents/"
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    31
    LDT_PROJECT_REST_API_PATH = "api/ldt/1.0/projects/"
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    32
    DEFAULT_ANNOTATION_CHANNEL = 'ANNOT'
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    33
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    34
    def parse_date(self, datestr):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    35
        res = parse_date_raw(datestr)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    36
        if res.tzinfo is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    37
            res = res.replace(tzinfo=tzutc())
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    38
        return res
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    39
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    40
    def find_delta(self, ts):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    41
        if self.deltas:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    42
            i = bisect.bisect_right(self.deltas, (ts+1,0))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    43
            if i:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    44
                return self.deltas[i-1]
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    45
        return (0,0)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    46
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    47
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    48
    def get_filter(self):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    49
        res = []
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    50
        if self.start_date:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    51
            res.append({'name': 'ts', 'op': ">=", 'val':self.start_date.isoformat() })
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    52
        if self.end_date:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    53
            res.append({'name': 'ts', 'op': "<=", 'val':self.end_date.isoformat() })
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    54
        if self.events:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    55
            res.append({'name': 'event_code', 'op': "in", 'val': self.events })
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    56
        if self.channels:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    57
            res.append({'name': 'channel', 'op': "in", 'val': self.channels })
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    58
        if self.user_whitelist:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    59
            res.append({'name': 'user', 'op': "in", 'val': self.user_whitelist })
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    60
        return res
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    61
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    62
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    63
    def build_annotation_iterator(self, params):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    64
        page = 0
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    65
        page_nb = 1
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    66
        while page < page_nb:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    67
            page += 1
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    68
            params['page'] = page
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    69
            resp = requests.get(self.annot_url, params=params, headers={'Content-Type': 'application/json'})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    70
            if resp.status_code != requests.codes.ok:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    71
                self.logger.debug("build_annotation_iterator : request %s : return code %r " % (resp.request.url, resp.status_code))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    72
                return
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    73
            resp_json = resp.json()
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    74
            page_nb = resp_json.get('total_pages', 1)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    75
            for item in resp_json.get('objects', []):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    76
                #TODO: add progress log
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    77
                yield item
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    78
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    79
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    80
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    81
    def __init__(self, start_date=None, end_date=None, duration=None, content_file=None,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    82
        content_file_write=None, project_id=None,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    83
        channels=[DEFAULT_ANNOTATION_CHANNEL], events=[], annot_url=None,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    84
        user_whitelist=None, post_param={}, deltas=None, base_url="http://ldt.iri.centrepompidou.fr/ldtplatform/",
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    85
        content=None, content_id=None, video=None,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    86
        replace=True, merge=False, name="", batch_size=500,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    87
        filename="project.ldt", color="16763904", logger=logger):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    88
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    89
        self.logger = logger
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    90
        self.base_url = base_url.rstrip("/")+"/" if base_url else base_url
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    91
        self.deltas = deltas
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    92
        self.post_param = {}
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    93
        if isinstance(post_param, basestring):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    94
            self.post_param = json.loads(post_param)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    95
        elif post_param:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    96
            self.post_param = post_param
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    97
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    98
        self.start_date = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    99
        if start_date:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   100
            if isinstance(start_date, datetime.datetime):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   101
                self.start_date = start_date
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   102
            else:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   103
                self.start_date = self.parse_date(str(start_date))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   104
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   105
        self.duration = duration
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   106
        self.end_date = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   107
        if end_date:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   108
            if isinstance(end_date, datetime.datetime):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   109
                self.end_date = end_date
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   110
            else:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   111
                self.end_date= self.parse_date(str(end_date))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   112
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   113
        if end_date:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   114
            if isinstance(end_date, datetime.datetime):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   115
                self.end_date = end_date
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   116
            else:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   117
                self.end_date= self.parse_date(str(end_date))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   118
        elif self.start_date and self.duration:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   119
            self.end_date = self.start_date + datetime.timedelta(seconds=self.duration)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   120
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   121
        self.content_file = content_file
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   122
        self.project_id = project_id
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   123
        if self.project_id is not None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   124
            self.content_file = self.base_url + AnnotationsSynchronizer.LDT_PROJECT_REST_API_PATH + self.project_id + "/?format=json"
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   125
        self.content_file_write = content_file_write
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   126
        if self.content_file_write is None and self.project_id:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   127
            self.content_file_write = self.content_file
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   128
        self.channels = list(set(channels))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   129
        self.annot_url = annot_url
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   130
        self.events = list(set(events))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   131
        self.user_whitelist_file = user_whitelist
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   132
        if self.user_whitelist_file:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   133
            with open(user_whitelist_file, 'r+') as f:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   134
                self.user_whitelist = list(set([s.strip() for s in f]))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   135
        else:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   136
            self.user_whitelist = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   137
        self.content = content
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   138
        self.content_id = content_id
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   139
        self.video = video
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   140
        self.replace = replace
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   141
        self.merge = merge
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   142
        self.name = name
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   143
        self.batch_size = batch_size
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   144
        self.filename = filename
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   145
        self.color = color
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   146
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   147
    def export_annotations(self):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   148
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   149
        root = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   150
        ensemble_parent = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   151
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   152
        #to do : analyse situation ldt or iri ? filename set or not ?
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   153
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   154
        if self.content_file and self.content_file.find("http://") == 0:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   155
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   156
            self.logger.debug("url : " + self.content_file) #@UndefinedVariable
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   157
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   158
            r = requests.get(self.content_file, params=self.post_param)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   159
            self.logger.debug("url response " + repr(r) + " content " + repr(r.text)) #@UndefinedVariable
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   160
            project = r.json()
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   161
            text_match = re.match(r"\<\?\s*xml.*?\?\>(.*)", project['ldt'], re.I|re.S)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   162
            root = etree.fromstring(text_match.group(1) if text_match else project['ldt'])
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   163
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   164
        elif self.content_file and os.path.exists(self.content_file):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   165
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   166
            doc = etree.parse(self.content_file)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   167
            root = doc.getroot()
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   168
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   169
        content_id = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   170
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   171
        if root is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   172
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   173
            root = etree.Element(u"iri")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   174
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   175
            project = etree.SubElement(root, u"project", {u"abstract":u"Annotations",u"title":u"Annotations", u"user":u"IRI Web", u"id":unicode(uuid.uuid4())})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   176
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   177
            medias = etree.SubElement(root, u"medias")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   178
            media = etree.SubElement(medias, u"media", {u"pict":u"", u"src":unicode(self.content), u"video":unicode(self.video), u"id":unicode(self.content_id), u"extra":u""})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   179
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   180
            annotations = etree.SubElement(root, u"annotations")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   181
            content = etree.SubElement(annotations, u"content", {u"id":unicode(self.content_id)})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   182
            ensemble_parent = content
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   183
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   184
            content_id = self.content_id
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   185
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   186
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   187
        if ensemble_parent is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   188
            file_type = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   189
            for node in root:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   190
                if node.tag == "project":
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   191
                    file_type = "ldt"
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   192
                    break
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   193
                elif node.tag == "head":
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   194
                    file_type = "iri"
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   195
                    break
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   196
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   197
            if file_type == "ldt":
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   198
                media_nodes = root.xpath("//media")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   199
                if len(media_nodes) > 0:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   200
                    media = media_nodes[0]
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   201
                annotations_node = root.find(u"annotations")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   202
                if annotations_node is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   203
                    annotations_node = etree.SubElement(root, u"annotations")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   204
                content_node = annotations_node.find(u"content")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   205
                if content_node is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   206
                    content_node = etree.SubElement(annotations_node,u"content", id=media.get(u"id"))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   207
                ensemble_parent = content_node
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   208
                content_id = content_node.get(u"id")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   209
                display_nodes = root.xpath("//displays/display/content[@id='%s']" % content_id)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   210
                if len(display_nodes) == 0:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   211
                    self.logger.info("No display node found. Will not update display")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   212
                    display_content_node = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   213
                else:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   214
                    display_content_node = display_nodes[0]
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   215
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   216
            elif file_type == "iri":
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   217
                body_node = root.find(u"body")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   218
                if body_node is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   219
                    body_node = etree.SubElement(root, u"body")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   220
                ensembles_node = body_node.find(u"ensembles")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   221
                if ensembles_node is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   222
                    ensembles_node = etree.SubElement(body_node, u"ensembles")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   223
                ensemble_parent = ensembles_node
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   224
                content_id = root.xpath("head/meta[@name='id']/@content")[0]
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   225
                display_content_node = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   226
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   227
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   228
        if ensemble_parent is None:
67
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   229
            self.logger.error("Ensemble parent is None - Can not process file") #@UndefinedVariable
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   230
            raise Exception("Ensemble parent is None - can not process file")
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   231
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   232
        if self.replace:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   233
            for ens in ensemble_parent.iterchildren(tag=u"ensemble"):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   234
                ens_id = ens.get("id","")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   235
                if ens_id.startswith("annot_"):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   236
                    ensemble_parent.remove(ens)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   237
                    # remove in display nodes
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   238
                    if display_content_node is not None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   239
                        for cut_display in display_content_node.iterchildren():
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   240
                            if cut_display.get('idens','') == ens_id:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   241
                                display_content_node.remove(cut_display)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   242
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   243
        ensemble = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   244
        elements = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   245
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   246
        if self.merge:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   247
            for ens in ensemble_parent.findall(u"ensemble"):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   248
                if ens.get('id',"").startswith("annot_"):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   249
                    ensemble = ens
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   250
                    break
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   251
            if ensemble is not None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   252
                elements = ensemble.find(u".//elements")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   253
                decoupage = ensemble.find(u"decoupage")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   254
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   255
        if ensemble is None or elements is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   256
            ensemble = etree.SubElement(ensemble_parent, u"ensemble", {u"id":u"annot_" + unicode(uuid.uuid4()), u"title":u"Ensemble Annotation", u"author":u"IRI Web", u"abstract":u"Ensemble Annotation"})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   257
            decoupage = etree.SubElement(ensemble, u"decoupage", {u"id": unicode(uuid.uuid4()), u"author": u"IRI Web"})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   258
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   259
            etree.SubElement(decoupage, u"title").text = unicode(self.name)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   260
            etree.SubElement(decoupage, u"abstract").text = unicode(self.name)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   261
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   262
            elements = etree.SubElement(decoupage, u"elements")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   263
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   264
        ensemble_id = ensemble.get('id', '')
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   265
        decoupage_id = decoupage.get('id', '') if decoupage is not None else None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   266
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   267
67
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   268
        if self.end_date is None and self.start_date and self.base_url:
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   269
            # get duration from api
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   270
            content_url = self.base_url + AnnotationsSynchronizer.LDT_CONTENT_REST_API_PATH + content_id + "/?format=json"
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   271
            self.logger.debug("get duration " + content_url) #@UndefinedVariable
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   272
            r = requests.get(content_url, params=self.post_param)
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   273
            self.logger.debug("get duration resp " + repr(r)) #@UndefinedVariable
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   274
            if r.status_code == requests.codes.ok:
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   275
                self.duration = int(r.json()['duration'])
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   276
            else:
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   277
                self.logger.error("Can not get duration form content : %r " % r)
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   278
                r.raise_for_status()
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   279
            self.logger.debug("get duration " + repr(self.duration)) #@UndefinedVariable
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   280
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   281
            self.end_date = self.start_date + datetime.timedelta(seconds=int(self.duration/1000))
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   282
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   283
        if self.end_date and self.deltas:
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   284
            self.end_date = self.end_date + datetime.timedelta(milliseconds=self.deltas[-1][1])
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   285
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   286
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   287
        filters = self.get_filter()
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   288
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   289
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   290
        params = { 'q':json.dumps({'filters':filters}), 'results_per_page': self.batch_size}
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   291
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   292
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   293
        for annot in self.build_annotation_iterator(params):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   294
            annot_ts = self.parse_date(annot['ts'])
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   295
            if self.start_date is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   296
                star_date = annot_ts
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   297
            annot_ts_rel = annot_ts-self.start_date
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   298
            annot_ts_rel_milli = int(round(annot_ts_rel.total_seconds()*1000))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   299
            d = self.find_delta(annot_ts_rel_milli)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   300
            if d[1] < 0:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   301
                continue
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   302
            else :
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   303
                annot_ts_rel_milli -= d[1]
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   304
            annot_content = annot.get('content',{'category':'', 'user':None})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   305
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   306
            username = annot_content.get('user', 'anon.') or 'anon.'
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   307
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   308
            category = annot_content.get('category', None)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   309
            if category is None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   310
                continue
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   311
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   312
            element = etree.SubElement(elements, u"element" , {u"id":annot.get('uuid', uuid.uuid4()), u"color":unicode(self.color), u"author":unicode(username), u"date":unicode(annot_ts.strftime("%Y/%m/%d")), u"begin": unicode(annot_ts_rel_milli), u"dur":u"0"})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   313
            etree.SubElement(element, u"title").text = unicode(username) + u": " + unicode(category.get('label', category.get('code', '')))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   314
            etree.SubElement(element, u"abstract").text = unicode(category.get('label', category.get('code', '')))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   315
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   316
            tags_node = etree.SubElement(element, u"tags")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   317
            etree.SubElement(tags_node,u"tag").text = category.get('code', '')
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   318
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   319
            meta_element = etree.SubElement(element, u'meta')
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   320
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   321
            polemics_element = etree.Element(u'polemics')
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   322
            etree.SubElement(polemics_element, u'polemic').text = category.get('code', '')
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   323
            meta_element.append(polemics_element)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   324
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   325
            etree.SubElement(meta_element, u"source", attrib={"url":self.annot_url + "/" + annot['uuid'], "mimetype":u"application/json"}).text = etree.CDATA(json.dumps(annot))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   326
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   327
        # sort by tc in
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   328
        if self.merge :
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   329
            # remove all elements and put them in a array
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   330
            # sort them with tc
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   331
            #put them back
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   332
            elements[:] = sorted(elements,key=lambda n: int(n.get('begin')))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   333
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   334
        #add to display node
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   335
        if display_content_node is not None:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   336
            display_dec = None
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   337
            for dec in display_content_node.iterchildren(tag=u"decoupage"):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   338
                if dec.get('idens','') == ensemble_id and dec.get('id', '') == decoupage_id:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   339
                    display_dec = dec
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   340
                    break
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   341
            if display_dec is None and ensemble_id and decoupage_id:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   342
                etree.SubElement(display_content_node, u"decoupage", attrib={'idens': ensemble_id, 'id': decoupage_id, 'tagsSelect':''})
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   343
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   344
        output_data = etree.tostring(root, encoding="utf-8", method="xml", pretty_print=False, xml_declaration=True)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   345
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   346
        if self.content_file_write and self.content_file_write.find("http://") == 0:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   347
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   348
            project["ldt"] = output_data
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   349
            project['owner'] = project['owner'].replace('%7E','~')
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   350
            project['contents'] = [c_url.replace('%7E','~') for c_url in project['contents']]
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   351
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   352
            self.logger.debug("write http " + self.content_file_write) #@UndefinedVariable
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   353
            self.logger.debug("write http " + repr(self.post_param)) #@UndefinedVariable
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   354
            self.logger.debug("write http " + repr(project)) #@UndefinedVariable
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   355
            r = requests.put(self.content_file_write, data=json.dumps(project), headers={'content-type':'application/json'}, params=self.post_param);
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   356
            self.logger.debug("write http " + repr(r) + " content " + r.text) #@UndefinedVariable
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   357
            if r.status_code != requests.codes.ok:  # @UndefinedVariable
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   358
                r.raise_for_status()
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   359
        else:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   360
            if self.content_file_write and os.path.exists(self.content_file_write):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   361
                dest_file_name = self.content_file_write
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   362
            else:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   363
                dest_file_name = self.filename
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   364
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   365
            self.logger.debug("WRITE : " + dest_file_name) #@UndefinedVariable
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   366
            output = open(dest_file_name, "w")
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   367
            output.write(output_data)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   368
            output.flush()
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   369
            output.close()