Exemple #1
0
    def export_as_static_view(self, ident=None):
        """Propose to export the view as a static view.

        The as_html() method must be implemented.
        """
        title=None
        if ident is None:
            title, ident=self.controller.package._idgenerator.new_from_title("export " + self._label)
            title, ident=dialog.get_title_id(title=_("HTML export"),
                                      text=_("Specify a name for the export view"),
                                      element_title=title,
                                      element_id=ident)
            if ident is None:
                return True
        if title is None:
            title=ident
        # Create the view
        v=self.controller.package.createView(
            ident=ident,
            author=config.data.userid,
            date=helper.get_timestamp(),
            clazz='*',
            content_mimetype="text/html",
            )
        v.title=title
        v.content.data=self.as_html()
        self.controller.package.views.append(v)
        self.controller.notify('ViewCreate', view=v)
        d=dialog.message_dialog(_("View successfully exported as %s.\nOpen it in the web browser ?") % v.title, icon=Gtk.MessageType.QUESTION)
        if d:
            c=self.controller.build_context(here=v)
            self.controller.open_url(c.evaluateValue('package/view/%s/absolute_url' % ident))
        return True
Exemple #2
0
    def get_interactive_query(self):
        l=helper.get_id(self.controller.package.queries, '_interactive')
        if l:
            q=SimpleQuery()
            q.from_xml(l.content.stream)
            q.container=l
            return l, q
        else:
            # Create the query
            el=self.controller.package.createQuery(ident='_interactive')
            el.author=config.data.userid
            el.date=helper.get_timestamp()
            el.title=_("Interactive query")

            # Create a basic query
            q=SimpleQuery(sources=self.sources,
                          rvalue="element")
            q.add_condition(Condition(lhs="element/content/data",
                                      operator="contains",
                                      rhs="string:a"))

            el.content.mimetype='application/x-advene-simplequery'
            el.content.data=q.xml_repr()

            self.controller.package.queries.append(el)

            self.controller.notify('QueryCreate', query=el)
            q.container=el
            return el, q
Exemple #3
0
    def get_interactive_query(self):
        l=helper.get_id(self.controller.package.queries, '_interactive')
        if l:
            q=SimpleQuery()
            q.from_xml(l.content.stream)
            q.container=l
            return l, q
        else:
            # Create the query
            el=self.controller.package.createQuery(ident='_interactive')
            el.author=config.data.userid
            el.date=helper.get_timestamp()
            el.title=_("Interactive query")

            # Create a basic query
            q=SimpleQuery(sources=self.sources,
                          rvalue="element")
            q.add_condition(Condition(lhs="element/content/data",
                                      operator="contains",
                                      rhs="string:a"))

            el.content.mimetype='application/x-advene-simplequery'
            el.content.data=q.xml_repr()

            self.controller.package.queries.append(el)

            self.controller.notify('QueryCreate', query=el)
            q.container=el
            return el, q
Exemple #4
0
    def copy_annotation(self, s, generate_id=False):
        """Create a new annotation.

        If generate_id is True, then generate a new id. Else, use the
        source id.

        Try to keep track of the occurences of its id, to fix them later on.
        """
        if generate_id or self.destination.get_element_by_id(s.id):
            id_ = self.destination._idgenerator.get_id(Annotation)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id]=id_

        # Find parent, and create it if necessary
        at=helper.get_id(self.destination.annotationTypes, self.translated_ids.get(s.type.id, s.type.id))
        if not at:
            # The annotation type does not exist. Create it.
            at=self.copy_annotation_type(helper.get_id(self.source.annotationTypes,
                                                       s.type.id), generate_id=generate_id)
        el=self.destination.createAnnotation(
            ident=id_,
            type=at,
            author=s.author or self.source.author,
            fragment=s.fragment.clone())
        el.date=s.date or helper.get_timestamp()
        el.content.mimetype=s.content.mimetype
        el.content.data=s.content.data
        el.tags = s.tags
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        self.destination.annotations.append(el)
        return el
Exemple #5
0
    def copy_view(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_=self.destination._idgenerator.get_id(View)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id]=id_

        el=self.destination.createView(
            ident=id_,
            clazz=s.viewableClass,
            author=s.author or self.source.author)
        el.date=s.date or helper.get_timestamp()
        el.title=s.title or id_
        el.matchFilter['class']=s.matchFilter['class']
        if 'type' in s.matchFilter:
            el.matchFilter['type']=s.matchFilter['type']
        # FIXME: ideally, we should try to fix translated_ids in
        # views. Or at least try to signal possible occurrences.
        el.content.mimetype=s.content.mimetype
        el.content.data=s.content.data
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        self.destination.views.append(el)
        return el
Exemple #6
0
    def __init__(self, author=None, package=None, defaulttype=None, controller=None, callback=None, source_type=None):
        """Instanciate the importer.

        Note: some importers can use an existing annotation type as
        source element (for processing annotations, e.g. concept
        extraction). In this case, the annotation_filter attribute
        must be set to True, and an additional source_type
        parameter is provided to __init__.

        @param author: author for imported/created elements
        @type author: string
        @param package: package where elements will be created
        @type package: advene.model.Package
        @param defaulttype: default annotation type for created annotations
        @type defaulltype: advene.model.AnnotationType
        @param controller: controller
        @type controller: advene.core.controller
        @param callback: callback method for progress report
        @type callback: method

        @param source_type: source annotation type (optional - for annotation filters)
        @param source_type: advene.model.AnnotationType
        """
        self.package=package
        if author is None:
            author=config.data.userid
        self.author=author
        self.controller=controller
        self.timestamp=helper.get_timestamp()
        self.defaulttype=defaulttype
        self.source_type=source_type
        self.callback=callback
        # Default offset in ms
        self.offset=0
        # Dictionary holding the number of created elements
        self.statistics={
            'annotation': 0,
            'relation': 0,
            'annotation-type': 0,
            'relation-type' : 0,
            'schema': 0,
            'view': 0,
            'package': 0,
            }
        # Optional output message that can be set by the importer to
        # provide feedback to the user
        self.output_message = ""

        # The convention for OptionParser is to have the "dest"
        # attribute of the same name as the Importer attribute
        # (e.g. here offset)
        self.optionparser = optparse.OptionParser(usage=_("Usage: %prog [options] source-file destination-file"),
                                                  epilog=self.name)
        self.optionparser.add_option("-o", "--offset",
                                     action="store", type="int", dest="offset", default=0,
                                     help=_("Specify the offset in ms"))
Exemple #7
0
    def save_query(self, *p):
        """Saves the query in the package.
        """
        l = self.eq.invalid_items()
        if l:
            self.log(
                _("Invalid query.\nThe following fields have an invalid value:\n%s"
                  ) % ", ".join(l))
            return True
        # Update the query
        self.eq.update_value()

        if hasattr(self.eq, 'container'):
            default_id = self.eq.container.id
            default_title = self.eq.container.title
        else:
            default_id = helper.title2id(self._label)
            default_title = self._label

        t, i = dialog.get_title_id(
            title=_("Saving the query..."),
            text=_("Give a title and identifier for saving the query"),
            element_title=default_title,
            element_id=default_id)
        if i is None:
            return True

        q = helper.get_id(self.controller.package.queries, i)
        # Overwriting an existing query
        if q:
            create = False
            self.controller.notify('EditSessionStart',
                                   element=q,
                                   immediate=True)
        else:
            create = True
            # Create the query
            q = self.controller.package.createQuery(ident=i)
            q.author = config.data.userid
            q.date = helper.get_timestamp()
            self.controller.package.queries.append(q)

        q.title = t
        q.content.mimetype = 'application/x-advene-simplequery'

        # Store the query itself in the _interactive query
        q.content.data = self.eq.model.xml_repr()
        if create:
            self.controller.notify('QueryCreate', query=q)
        else:
            self.controller.notify('QueryEditEnd', query=q)
            self.controller.notify('EditSessionEnd', element=q)
        return q
Exemple #8
0
    def save_query(self, *p):
        """Saves the query in the package.
        """
        l=self.eq.invalid_items()
        if l:
            self.log(_("Invalid query.\nThe following fields have an invalid value:\n%s")
                     % ", ".join(l))
            return True
        # Update the query
        self.eq.update_value()

        if hasattr(self.eq, 'container'):
            default_id=self.eq.container.id
            default_title=self.eq.container.title
        else:
            default_id=helper.title2id(self._label)
            default_title=self._label


        t, i = dialog.get_title_id(title=_("Saving the query..."),
                                   text=_("Give a title and identifier for saving the query"),
                                   element_title=default_title,
                                   element_id=default_id)
        if i is None:
            return True

        q=helper.get_id(self.controller.package.queries, i)
        # Overwriting an existing query
        if q:
            create=False
            self.controller.notify('EditSessionStart', element=q, immediate=True)
        else:
            create=True
            # Create the query
            q=self.controller.package.createQuery(ident=i)
            q.author=config.data.userid
            q.date=helper.get_timestamp()
            self.controller.package.queries.append(q)

        q.title=t
        q.content.mimetype='application/x-advene-simplequery'

        # Store the query itself in the _interactive query
        q.content.data = self.eq.model.xml_repr()
        if create:
            self.controller.notify('QueryCreate', query=q)
        else:
            self.controller.notify('QueryEditEnd', query=q)
            self.controller.notify('EditSessionEnd', element=q)
        return q
Exemple #9
0
 def process_file(self, filename):
     if self.package is None:
         self.init_package(filename='event_history.xml', annotationtypeid='event')
     id_="Traces"
     title_="Traces"
     schema=self.package.get_element_by_id(id_)
     if schema is None:
         self.package._idgenerator.add(id_)
         schema=self.package.createSchema(ident=id_)
         schema.author=config.data.userid
         schema.date=helper.get_timestamp()
         schema.title=title_
         self.package.schemas.append(schema)
     self.convert(self.iterator(filename))
     return self.package
Exemple #10
0
    def copy_schema(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_=self.destination._idgenerator.get_id(Schema)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id]=id_

        el=self.destination.createSchema(ident=id_)
        el.author=s.author or self.source.author
        el.date=s.date or helper.get_timestamp()
        el.title=s.title or id_
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        self.destination.schemas.append(el)
        return el
Exemple #11
0
    def copy_schema(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_ = self.destination._idgenerator.get_id(Schema)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id] = id_

        el = self.destination.createSchema(ident=id_)
        el.author = s.author or self.source.author
        el.date = s.date or helper.get_timestamp()
        el.title = s.title or id_
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        self.destination.schemas.append(el)
        return el
Exemple #12
0
 def process_file(self, filename):
     if self.package is None:
         self.init_package(filename='event_history.xml',
                           annotationtypeid='event')
     id_ = "Traces"
     title_ = "Traces"
     schema = self.package.get_element_by_id(id_)
     if schema is None:
         self.package._idgenerator.add(id_)
         schema = self.package.createSchema(ident=id_)
         schema.author = config.data.userid
         schema.date = helper.get_timestamp()
         schema.title = title_
         self.package.schemas.append(schema)
     self.convert(self.iterator(filename))
     return self.package
Exemple #13
0
    def save_view(self, *p):
        title, ident = self.controller.package._idgenerator.new_from_title(
            self._label)
        title, ident = dialog.get_title_id(
            title=_("Saving %s" % self.view_name),
            element_title=title,
            element_id=ident,
            text=_("Enter a view name to save this parametered view"))
        if ident is not None:
            if not re.match(r'^[a-zA-Z0-9_]+$', ident):
                dialog.message_dialog(
                    _("Error: the identifier %s contains invalid characters.")
                    % ident)
                return True

            options, arguments = self.get_save_arguments()
            if options is None and arguments is None:
                # Cancel view saving
                return True

            v = helper.get_id(self.controller.package.views, ident)
            if v is None:
                create = True
                v = self.controller.package.createView(ident=ident,
                                                       clazz='package')
            else:
                # Existing view. Check that it is already an adhoc-view
                if v.content.mimetype != 'application/x-advene-adhoc-view':
                    dialog.message_dialog(
                        _("Error: the view %s is not an adhoc view.") % ident)
                    return True
                create = False
                self.controller.notify('EditSessionStart',
                                       element=v,
                                       immediate=True)
            v.title = title
            v.author = config.data.userid
            v.date = helper.get_timestamp()

            self.save_parameters(v.content, options, arguments)
            if create:
                self.controller.package.views.append(v)
                self.controller.notify("ViewCreate", view=v)
            else:
                self.controller.notify("ViewEditEnd", view=v)
                self.controller.notify('EditSessionEnd', element=v)
        return True
Exemple #14
0
    def copy_query(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_ = self.destination._idgenerator.get_id(Query)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id] = id_

        el = self.destination.createQuery(ident=id_,
                                          author=s.author
                                          or self.source.author)
        el.data = s.date or helper.get_timestamp()
        el.title = s.title or id_
        el.content.mimetype = s.content.mimetype
        el.content.data = s.content.data
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        self.destination.queries.append(el)
        return el
Exemple #15
0
    def copy_query(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_=self.destination._idgenerator.get_id(Query)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id]=id_

        el=self.destination.createQuery(
            ident=id_,
            author=s.author or self.source.author)
        el.data=s.date or helper.get_timestamp()
        el.title=s.title or id_
        el.content.mimetype=s.content.mimetype
        el.content.data=s.content.data
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        self.destination.queries.append(el)
        return el
Exemple #16
0
    def save_query(self, *p):
        """Saves the query in the package.
        """
        if hasattr(self.query, 'container'):
            default_id = self.query.container.id
            default_title = self.query.container.title
        else:
            default_id = helper.title2id(self._label)
            default_title = self._label

        t, i = dialog.get_title_id(
            title=_("Saving the query..."),
            text=_("Give a title and identifier for saving the query"),
            element_title=default_title,
            element_id=default_id)
        if i is None:
            return True

        q = helper.get_id(self.controller.package.queries, i)
        # Overwriting an existing query
        if q:
            create = False
        else:
            create = True
            # Create the query
            q = self.controller.package.createQuery(ident=i)
            q.author = config.data.userid
            q.date = helper.get_timestamp()
            self.controller.package.queries.append(q)

        q.title = t
        if isinstance(self.query, SimpleQuery):
            q.content.mimetype = 'application/x-advene-simplequery'
        elif isinstance(self.query, Quicksearch):
            q.content.mimetype = 'application/x-advene-quicksearch'
        q.content.data = self.query.xml_repr()
        if create:
            self.controller.notify('QueryCreate', query=q)
        else:
            self.controller.notify('QueryEditEnd', query=q)
        return q
Exemple #17
0
    def copy_relation(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_ = self.destination._idgenerator.get_id(Relation)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id] = id_

        rt = helper.get_id(self.destination.relationTypes,
                           self.translated_ids.get(s.type.id, s.type.id))
        if not rt:
            # The annotation type does not exist. Create it.
            rt = self.copy_relation_type(helper.get_id(
                self.source.relationTypes, s.type.id),
                                         generate_id=generate_id)
        # Ensure that annotations exist
        members = []
        for sa in s.members:
            # check translated_ids
            i = sa.id
            if i in self.translated_ids:
                i = self.translated_ids[i]

            a = helper.get_id(self.destination.annotations, i)
            if not a:
                a = self.copy_annotation(sa)
            members.append(a)
        el = self.destination.createRelation(ident=id_,
                                             type=rt,
                                             author=s.author
                                             or self.source.author
                                             or "unknown",
                                             members=members)
        el.date = s.date or helper.get_timestamp()
        el.content.data = s.content.data
        el.tags = s.tags
        self.destination.relations.append(el)
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        #el.title=s.title or ''
        return el
Exemple #18
0
    def create_follow_dynamic_view(self, rt):
        """Create a dynamic view for the given relation-type.
        """
        p = self.controller.package
        ident = 'v_follow_%s' % rt.id
        if p.get_element_by_id(ident) is not None:
            dialog.message_dialog(
                _("A follow dynamic view for %s already seems to exist.") %
                self.get_title(rt))
            return True
        v = p.createView(ident=ident,
                         author=config.data.userid,
                         date=helper.get_timestamp(),
                         clazz='package',
                         content_mimetype='application/x-advene-ruleset')
        v.title = _("Follow %s relation-type") % self.get_title(rt)

        # Build the ruleset
        r = RuleSet()
        catalog = self.controller.event_handler.catalog

        ra = catalog.get_action("PlayerGoto")
        action = Action(registeredaction=ra, catalog=catalog)
        action.add_parameter(
            'position',
            'annotation/typedRelatedOut/%s/first/fragment/begin' % rt.id)
        rule = Rule(name=_("Follow the relation"),
                    event=Event("AnnotationEnd"),
                    condition=Condition(lhs='annotation/typedRelatedOut/%s' %
                                        rt.id,
                                        operator='value'),
                    action=action)
        r.add_rule(rule)

        v.content.data = r.xml_repr()

        p.views.append(v)
        self.controller.notify('ViewCreate', view=v)
        self.controller.activate_stbv(v)
        return True
Exemple #19
0
    def save_query(self, *p):
        """Saves the query in the package.
        """
        if hasattr(self.query, 'container'):
            default_id=self.query.container.id
            default_title=self.query.container.title
        else:
            default_id=helper.title2id(self._label)
            default_title=self._label

        t, i = dialog.get_title_id(title=_("Saving the query..."),
                                   text=_("Give a title and identifier for saving the query"),
                                   element_title=default_title,
                                   element_id=default_id)
        if i is None:
            return True

        q=helper.get_id(self.controller.package.queries, i)
        # Overwriting an existing query
        if q:
            create=False
        else:
            create=True
            # Create the query
            q=self.controller.package.createQuery(ident=i)
            q.author=config.data.userid
            q.date=helper.get_timestamp()
            self.controller.package.queries.append(q)

        q.title=t
        if isinstance(self.query, SimpleQuery):
            q.content.mimetype='application/x-advene-simplequery'
        elif isinstance(self.query, Quicksearch):
            q.content.mimetype='application/x-advene-quicksearch'
        q.content.data = self.query.xml_repr()
        if create:
            self.controller.notify('QueryCreate', query=q)
        else:
            self.controller.notify('QueryEditEnd', query=q)
        return q
Exemple #20
0
    def create_dynamic_view(self, at):
        """Create a caption dynamic view for the given annotation-type.
        """
        p = self.controller.package
        ident = 'v_caption_%s' % at.id
        if p.get_element_by_id(ident) is not None:
            dialog.message_dialog(
                _("A caption dynamic view for %s already seems to exist.") %
                self.get_title(at))
            return True
        v = p.createView(ident=ident,
                         author=config.data.userid,
                         date=helper.get_timestamp(),
                         clazz='package',
                         content_mimetype='application/x-advene-ruleset')
        v.title = _("Caption %s annotations") % self.get_title(at)

        # Build the ruleset
        r = RuleSet()
        catalog = self.controller.event_handler.catalog

        ra = catalog.get_action("AnnotationCaption")
        action = Action(registeredaction=ra, catalog=catalog)
        action.add_parameter('message', 'annotation/content/data')

        rule = Rule(name=_("Caption the annotation"),
                    event=Event("AnnotationBegin"),
                    condition=Condition(lhs='annotation/type/id',
                                        operator='equals',
                                        rhs='string:%s' % at.id),
                    action=action)
        r.add_rule(rule)

        v.content.data = r.xml_repr()

        p.views.append(v)
        self.controller.notify('ViewCreate', view=v)
        self.controller.activate_stbv(v)
        return True
Exemple #21
0
    def create_dynamic_view(self, at):
        """Create a caption dynamic view for the given annotation-type.
        """
        p=self.controller.package
        ident='v_caption_%s' % at.id
        if p.get_element_by_id(ident) is not None:
            dialog.message_dialog(_("A caption dynamic view for %s already seems to exist.") % self.get_title(at))
            return True
        v=p.createView(
            ident=ident,
            author=config.data.userid,
            date=helper.get_timestamp(),
            clazz='package',
            content_mimetype='application/x-advene-ruleset'
            )
        v.title=_("Caption %s annotations") % self.get_title(at)

        # Build the ruleset
        r=RuleSet()
        catalog=self.controller.event_handler.catalog

        ra=catalog.get_action("AnnotationCaption")
        action=Action(registeredaction=ra, catalog=catalog)
        action.add_parameter('message', 'annotation/content/data')

        rule=Rule(name=_("Caption the annotation"),
                  event=Event("AnnotationBegin"),
                  condition=Condition(lhs='annotation/type/id',
                                      operator='equals',
                                      rhs='string:%s' % at.id),
                  action=action)
        r.add_rule(rule)

        v.content.data=r.xml_repr()

        p.views.append(v)
        self.controller.notify('ViewCreate', view=v)
        self.controller.activate_stbv(v)
        return True
Exemple #22
0
    def save_view(self, *p):
        title, ident=self.controller.package._idgenerator.new_from_title(self._label)
        title, ident=dialog.get_title_id(title=_("Saving %s" % self.view_name),
                                                  element_title=title,
                                                  element_id=ident,
                                                  text=_("Enter a view name to save this parametered view"))
        if ident is not None:
            if not re.match(r'^[a-zA-Z0-9_]+$', ident):
                dialog.message_dialog(_("Error: the identifier %s contains invalid characters.") % ident)
                return True

            options, arguments = self.get_save_arguments()
            if options is None and arguments is None:
                # Cancel view saving
                return True

            v=helper.get_id(self.controller.package.views, ident)
            if v is None:
                create=True
                v=self.controller.package.createView(ident=ident, clazz='package')
            else:
                # Existing view. Check that it is already an adhoc-view
                if v.content.mimetype != 'application/x-advene-adhoc-view':
                    dialog.message_dialog(_("Error: the view %s is not an adhoc view.") % ident)
                    return True
                create=False
                self.controller.notify('EditSessionStart', element=v, immediate=True)
            v.title=title
            v.author=config.data.userid
            v.date=helper.get_timestamp()

            self.save_parameters(v.content, options, arguments)
            if create:
                self.controller.package.views.append(v)
                self.controller.notify("ViewCreate", view=v)
            else:
                self.controller.notify("ViewEditEnd", view=v)
                self.controller.notify('EditSessionEnd', element=v)
        return True
Exemple #23
0
    def iterator(self, f):
        start = f[0]['timestamp']
        end = start
        id_ = "Traces"
        schema = self.package.get_element_by_id(id_)
        for e in f:
            typename = e['event_name']
            type_ = self.package.get_element_by_id(typename)
            if type_ is None:
                #Annotation type creation
                self.package._idgenerator.add(typename)
                type_ = schema.createAnnotationType(ident=typename)
                type_.author = config.data.userid
                type_.date = helper.get_timestamp()
                type_.title = typename
                type_.mimetype = 'application/x-advene-structured'
                type_.setMetaData(config.data.namespace, 'color',
                                  next(self.package._color_palette))
                type_.setMetaData(config.data.namespace, 'item_color',
                                  'here/tag_color')
                schema.annotationTypes.append(type_)

            d = {
                'type': type_,
                'begin': e['timestamp'] - start,
                'duration': 50,
                'timestamp': e['timestamp'],
                'content': '',
            }
            if 'content' in e:
                d['content'] = e['content'] + '\nposition=' + str(
                    e['movietime']) + '\n'
            else:
                d['content'] = 'position=' + str(e['movietime']) + '\n'
            if end < e['timestamp'] + 50:
                end = e['timestamp'] + 50
            yield d
        #fix package duration
        self.package.cached_duration = end - start
Exemple #24
0
    def copy_relation(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_=self.destination._idgenerator.get_id(Relation)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id]=id_

        rt=helper.get_id(self.destination.relationTypes, self.translated_ids.get(s.type.id, s.type.id))
        if not rt:
            # The annotation type does not exist. Create it.
            rt=self.copy_relation_type(helper.get_id(self.source.relationTypes,
                                                     s.type.id), generate_id=generate_id)
        # Ensure that annotations exist
        members=[]
        for sa in s.members:
            # check translated_ids
            i=sa.id
            if i in self.translated_ids:
                i=self.translated_ids[i]

            a=helper.get_id(self.destination.annotations, i)
            if not a:
                a=self.copy_annotation(sa)
            members.append(a)
        el=self.destination.createRelation(
            ident=id_,
            type=rt,
            author=s.author or self.source.author or "unknown",
            members=members)
        el.date=s.date or helper.get_timestamp()
        el.content.data=s.content.data
        el.tags = s.tags
        self.destination.relations.append(el)
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        #el.title=s.title or ''
        return el
Exemple #25
0
    def copy_relation_type(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_=self.destination._idgenerator.get_id(RelationType)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id]=id_

        # Find parent, and create it if necessary
        sch=helper.get_id(self.destination.schemas, s.schema.id)
        if not sch:
            # Create it
            sch=helper.get_id(self.source.schemas, s.schema.id)
            sch=self.copy_schema(sch)
        el=sch.createRelationType(ident=id_)
        el.author=s.author or self.source.author
        el.date=s.date or helper.get_timestamp()
        el.title=s.title or id_
        el.mimetype=s.mimetype
        sch.relationTypes.append(el)
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        # Handle membertypes, ensure that annotation types are defined
        for m in s.hackedMemberTypes:
            if m == '':
                # Any type, no import necessary
                continue
            if not m.startswith('#'):
                logger.error("Cannot handle non-fragment membertypes %s", m)
                continue
            at=helper.get_id(self.destination.annotationTypes, m[1:])
            if not at:
                # The annotation type does not exist. Create it.
                at=helper.get_id(self.source.annotationTypes, m[1:])
                at=self.copy_annotation_type(at)
        # Now we can set member types
        el.setHackedMemberTypes(s.getHackedMemberTypes())
        return el
Exemple #26
0
    def copy_relation_type(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_ = self.destination._idgenerator.get_id(RelationType)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id] = id_

        # Find parent, and create it if necessary
        sch = helper.get_id(self.destination.schemas, s.schema.id)
        if not sch:
            # Create it
            sch = helper.get_id(self.source.schemas, s.schema.id)
            sch = self.copy_schema(sch)
        el = sch.createRelationType(ident=id_)
        el.author = s.author or self.source.author
        el.date = s.date or helper.get_timestamp()
        el.title = s.title or id_
        el.mimetype = s.mimetype
        sch.relationTypes.append(el)
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        # Handle membertypes, ensure that annotation types are defined
        for m in s.hackedMemberTypes:
            if m == '':
                # Any type, no import necessary
                continue
            if not m.startswith('#'):
                logger.error("Cannot handle non-fragment membertypes %s", m)
                continue
            at = helper.get_id(self.destination.annotationTypes, m[1:])
            if not at:
                # The annotation type does not exist. Create it.
                at = helper.get_id(self.source.annotationTypes, m[1:])
                at = self.copy_annotation_type(at)
        # Now we can set member types
        el.setHackedMemberTypes(s.getHackedMemberTypes())
        return el
Exemple #27
0
    def copy_annotation_type(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_ = self.destination._idgenerator.get_id(AnnotationType)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id] = id_

        # Find parent, and create it if necessary
        sch = helper.get_id(self.destination.schemas, s.schema.id)
        if not sch:
            # Create it
            sch = helper.get_id(self.source.schemas, s.schema.id)
            sch = self.copy_schema(sch)
        el = sch.createAnnotationType(ident=id_)
        el.author = s.author or self.source.author
        el.date = s.date or helper.get_timestamp()
        el.title = s.title or id_
        el.mimetype = s.mimetype
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        sch.annotationTypes.append(el)
        return el
Exemple #28
0
    def copy_annotation_type(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_=self.destination._idgenerator.get_id(AnnotationType)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id]=id_

        # Find parent, and create it if necessary
        sch=helper.get_id(self.destination.schemas, s.schema.id)
        if not sch:
            # Create it
            sch=helper.get_id(self.source.schemas, s.schema.id)
            sch=self.copy_schema(sch)
        el=sch.createAnnotationType(ident=id_)
        el.author=s.author or self.source.author
        el.date=s.date or helper.get_timestamp()
        el.title=s.title or id_
        el.mimetype=s.mimetype
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        sch.annotationTypes.append(el)
        return el
Exemple #29
0
    def create_follow_dynamic_view(self, rt):
        """Create a dynamic view for the given relation-type.
        """
        p = self.controller.package
        ident = 'v_follow_%s' % rt.id
        if p.get_element_by_id(ident) is not None:
            dialog.message_dialog(_("A follow dynamic view for %s already seems to exist.") % self.get_title(rt))
            return True
        v = p.createView(
            ident=ident,
            author=config.data.userid,
            date=helper.get_timestamp(),
            clazz='package',
            content_mimetype='application/x-advene-ruleset'
            )
        v.title = _("Follow %s relation-type") % self.get_title(rt)

        # Build the ruleset
        r = RuleSet()
        catalog = self.controller.event_handler.catalog

        ra = catalog.get_action("PlayerGoto")
        action = Action(registeredaction=ra, catalog=catalog)
        action.add_parameter('position', 'annotation/typedRelatedOut/%s/first/fragment/begin' % rt.id)
        rule=Rule(name=_("Follow the relation"),
                  event=Event("AnnotationEnd"),
                  condition=Condition(lhs='annotation/typedRelatedOut/%s' % rt.id,
                                      operator='value'),
                  action=action)
        r.add_rule(rule)

        v.content.data=r.xml_repr()

        p.views.append(v)
        self.controller.notify('ViewCreate', view=v)
        self.controller.activate_stbv(v)
        return True
Exemple #30
0
    def iterator(self, f):
        start=f[0]['timestamp']
        end=start
        id_="Traces"
        schema=self.package.get_element_by_id(id_)
        for e in f:
            type_ = e['event_name']
            type = self.package.get_element_by_id(type_)
            if (type is None):
                #Annotation type creation
                self.package._idgenerator.add(type_)
                type=schema.createAnnotationType(
                    ident=type_)
                type.author=config.data.userid
                type.date=helper.get_timestamp()
                type.title=type_
                type.mimetype='application/x-advene-structured'
                type.setMetaData(config.data.namespace, 'color', next(self.package._color_palette))
                type.setMetaData(config.data.namespace, 'item_color', 'here/tag_color')
                schema.annotationTypes.append(type)

            d={
                'type': type,
                'begin': e['timestamp'] - start,
                'duration': 50,
                'timestamp': e['timestamp'],
                'content': '',
            }
            if 'content' in e:
                d['content']=e['content']+'\nposition='+str(e['movietime'])+'\n'
            else:
                d['content']='position='+str(e['movietime'])+'\n'
            if end<e['timestamp']+50:
                end=e['timestamp']+50
            yield d
        #fix package duration
        self.package.cached_duration=end-start
Exemple #31
0
    def copy_annotation(self, s, generate_id=False):
        """Create a new annotation.

        If generate_id is True, then generate a new id. Else, use the
        source id.

        Try to keep track of the occurences of its id, to fix them later on.
        """
        if generate_id or self.destination.get_element_by_id(s.id):
            id_ = self.destination._idgenerator.get_id(Annotation)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id] = id_

        # Find parent, and create it if necessary
        at = helper.get_id(self.destination.annotationTypes,
                           self.translated_ids.get(s.type.id, s.type.id))
        if not at:
            # The annotation type does not exist. Create it.
            at = self.copy_annotation_type(helper.get_id(
                self.source.annotationTypes, s.type.id),
                                           generate_id=generate_id)
        el = self.destination.createAnnotation(ident=id_,
                                               type=at,
                                               author=s.author
                                               or self.source.author,
                                               fragment=s.fragment.clone())
        el.date = s.date or helper.get_timestamp()
        el.content.mimetype = s.content.mimetype
        el.content.data = s.content.data
        el.tags = s.tags
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        self.destination.annotations.append(el)
        return el
Exemple #32
0
    def copy_view(self, s, generate_id=False):
        if generate_id or self.destination.get_element_by_id(s.id):
            id_ = self.destination._idgenerator.get_id(View)
        else:
            id_ = s.id
        self.destination._idgenerator.add(id_)
        self.translated_ids[s.id] = id_

        el = self.destination.createView(ident=id_,
                                         clazz=s.viewableClass,
                                         author=s.author or self.source.author)
        el.date = s.date or helper.get_timestamp()
        el.title = s.title or id_
        el.matchFilter['class'] = s.matchFilter['class']
        if 'type' in s.matchFilter:
            el.matchFilter['type'] = s.matchFilter['type']
        # FIXME: ideally, we should try to fix translated_ids in
        # views. Or at least try to signal possible occurrences.
        el.content.mimetype = s.content.mimetype
        el.content.data = s.content.data
        for (namespace, name, value) in s.listMetaData():
            el.setMetaData(namespace, name, value)
        self.destination.views.append(el)
        return el
Exemple #33
0
 def get_date(self):
     return helper.get_timestamp()
    def convert_transcription_cb(self, button=None):
        if not self.controller.gui:
            self.message(_("Cannot convert the data: no associated package"))
            return True

        d = Gtk.Dialog(title=_("Converting transcription"),
                       parent=self.controller.gui.gui.win,
                       flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
                       buttons=( Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                 Gtk.STOCK_OK, Gtk.ResponseType.OK,
                                 ))
        l=Gtk.Label(label=_("Choose the annotation-type where to create annotations.\n"))
        l.set_line_wrap(True)
        l.show()
        d.vbox.pack_start(l, False, True, 0)

        # Anticipated declaration of some widgets, which need to be
        # updated in the handle_new_type_selection callback.
        new_type_dialog=Gtk.VBox()
        delete_existing_toggle=Gtk.CheckButton(_("Delete existing annotations in this type"))
        delete_existing_toggle.set_active(False)

        ats=list(self.controller.package.annotationTypes)
        newat=helper.TitledElement(value=None,
                                   title=_("Create a new annotation type"))
        ats.append(newat)

        def handle_new_type_selection(combo):
            el=combo.get_current_element()
            if el == newat:
                new_type_dialog.show()
                delete_existing_toggle.set_sensitive(False)
            else:
                new_type_dialog.hide()
                delete_existing_toggle.set_sensitive(True)
            return True

        type_selection=dialog.list_selector_widget(members=[ (a, self.controller.get_title(a), self.controller.get_element_color(a)) for a in ats],
                                                   callback=handle_new_type_selection,
                                                   preselect=self.controller.package.get_element_by_id(self.options['annotation-type-id']))

        hb=Gtk.HBox()
        hb.pack_start(Gtk.Label(_("Select type") + " "), False, False, 0)
        hb.pack_start(type_selection, False, True, 0)
        d.vbox.pack_start(hb, False, True, 0)

        l=Gtk.Label(label=_("You want to create a new type. Please specify its schema and title."))
        l.set_line_wrap(True)
        l.show()
        new_type_dialog.pack_start(l, False, True, 0)

        hb=Gtk.HBox()
        hb.pack_start(Gtk.Label(_("Title") + " "), False, False, 0)
        new_title=Gtk.Entry()
        hb.pack_start(new_title, True, True, 0)
        new_type_dialog.pack_start(hb, False, True, 0)

        hb=Gtk.HBox()
        hb.pack_start(Gtk.Label(_("Containing schema") + " "), False, False, 0)
        schemas=list(self.controller.package.schemas)
        schema_selection=dialog.list_selector_widget(members=[ (s, self.controller.get_title(s)) for s in schemas])
        hb.pack_start(schema_selection, False, True, 0)
        new_type_dialog.pack_start(hb, False, True, 0)

        new_type_dialog.show_all()
        new_type_dialog.set_no_show_all(True)
        new_type_dialog.hide()

        d.vbox.pack_start(new_type_dialog, True, True, 0)

        l=Gtk.Label()
        l.set_markup("<b>" + _("Export options") + "</b>")
        d.vbox.pack_start(l, False, True, 0)

        d.vbox.pack_start(delete_existing_toggle, False, True, 0)

        empty_contents_toggle=Gtk.CheckButton(_("Generate annotations for empty contents"))
        empty_contents_toggle.set_active(self.options['empty-annotations'])
        d.vbox.pack_start(empty_contents_toggle, False, True, 0)

        d.connect('key-press-event', dialog.dialog_keypressed_cb)

        d.show_all()
        dialog.center_on_mouse(d)

        finished=None
        while not finished:
            res=d.run()
            if res == Gtk.ResponseType.OK:
                at=type_selection.get_current_element()
                if at == newat:
                    new_type_title=new_title.get_text()
                    if new_type_title == '':
                        # Empty title. Generate one.
                        id_=self.controller.package._idgenerator.get_id(AnnotationType)
                        new_type_title=id_
                    else:
                        id_=helper.title2id(new_type_title)
                        # Check that the id is available
                        if self.controller.package._idgenerator.exists(id_):
                            dialog.message_dialog(
                                _("The %s identifier already exists. Choose another one.") % id_,
                                icon=Gtk.MessageType.WARNING)
                            at=None
                            continue
                    # Creating a new type
                    s=schema_selection.get_current_element()
                    at=s.createAnnotationType(ident=id_)
                    at.author=config.data.userid
                    at.date=helper.get_timestamp()
                    at.title=new_type_title
                    at.mimetype='text/plain'
                    at.setMetaData(config.data.namespace, 'color', next(s.rootPackage._color_palette))
                    at.setMetaData(config.data.namespace, 'item_color', 'here/tag_color')
                    s.annotationTypes.append(at)
                    self.controller.notify('AnnotationTypeCreate', annotationtype=at)

                if delete_existing_toggle.get_active():
                    # Remove all annotations of at type
                    batch_id=object()
                    for a in at.annotations:
                        self.controller.delete_element(a, batch=batch_id)

                self.options['empty-annotations']=empty_contents_toggle.get_active()
                finished=True
            else:
                at=None
                finished=True
        d.destroy()

        if at is not None:
            self.options['annotation-type-id'] = at.id
            ti=TranscriptionImporter(package=self.controller.package,
                                     controller=self.controller,
                                     defaulttype=at,
                                     transcription_edit=self)
            ti.process_file('transcription')

            self.controller.package._modified=True
            self.controller.notify("PackageActivate", package=ti.package)
            self.message(_('Notes converted'))
            self.log(ti.statistics_formatted())
            # Feedback
            dialog.message_dialog(
                _("Conversion completed.\n%s annotations generated.") % ti.statistics['annotation'])

        return True
Exemple #35
0
    def convert_transcription_cb(self, button=None):
        if not self.controller.gui:
            self.message(_("Cannot convert the data: no associated package"))
            return True

        d = Gtk.Dialog(title=_("Converting transcription"),
                       parent=self.controller.gui.gui.win,
                       flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
                       buttons=( Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                 Gtk.STOCK_OK, Gtk.ResponseType.OK,
                                 ))
        l=Gtk.Label(label=_("Choose the annotation-type where to create annotations.\n"))
        l.set_line_wrap(True)
        l.show()
        d.vbox.pack_start(l, False, True, 0)

        # Anticipated declaration of some widgets, which need to be
        # updated in the handle_new_type_selection callback.
        new_type_dialog=Gtk.VBox()
        delete_existing_toggle=Gtk.CheckButton(_("Delete existing annotations in this type"))
        delete_existing_toggle.set_active(False)

        ats=list(self.controller.package.annotationTypes)
        newat=helper.TitledElement(value=None,
                                   title=_("Create a new annotation type"))
        ats.append(newat)

        def handle_new_type_selection(combo):
            el=combo.get_current_element()
            if el == newat:
                new_type_dialog.show()
                delete_existing_toggle.set_sensitive(False)
            else:
                new_type_dialog.hide()
                delete_existing_toggle.set_sensitive(True)
            return True

        type_selection=dialog.list_selector_widget(members=[ (a, self.controller.get_title(a), self.controller.get_element_color(a)) for a in ats],
                                                   callback=handle_new_type_selection,
                                                   preselect=self.controller.package.get_element_by_id(self.options['annotation-type-id']))

        hb=Gtk.HBox()
        hb.pack_start(Gtk.Label(_("Select type") + " "), False, False, 0)
        hb.pack_start(type_selection, False, True, 0)
        d.vbox.pack_start(hb, False, True, 0)

        l=Gtk.Label(label=_("You want to create a new type. Please specify its schema and title."))
        l.set_line_wrap(True)
        l.show()
        new_type_dialog.pack_start(l, False, True, 0)

        hb=Gtk.HBox()
        hb.pack_start(Gtk.Label(_("Title") + " "), False, False, 0)
        new_title=Gtk.Entry()
        hb.pack_start(new_title, True, True, 0)
        new_type_dialog.pack_start(hb, False, True, 0)

        hb=Gtk.HBox()
        hb.pack_start(Gtk.Label(_("Containing schema") + " "), False, False, 0)
        schemas=list(self.controller.package.schemas)
        schema_selection=dialog.list_selector_widget(members=[ (s, self.controller.get_title(s)) for s in schemas])
        hb.pack_start(schema_selection, False, True, 0)
        new_type_dialog.pack_start(hb, False, True, 0)

        new_type_dialog.show_all()
        new_type_dialog.set_no_show_all(True)
        new_type_dialog.hide()

        d.vbox.pack_start(new_type_dialog, True, True, 0)

        l=Gtk.Label()
        l.set_markup("<b>" + _("Export options") + "</b>")
        d.vbox.pack_start(l, False, True, 0)

        d.vbox.pack_start(delete_existing_toggle, False, True, 0)

        empty_contents_toggle=Gtk.CheckButton(_("Generate annotations for empty contents"))
        empty_contents_toggle.set_active(self.options['empty-annotations'])
        d.vbox.pack_start(empty_contents_toggle, False, True, 0)

        d.connect('key-press-event', dialog.dialog_keypressed_cb)

        d.show_all()
        dialog.center_on_mouse(d)

        finished=None
        while not finished:
            res=d.run()
            if res == Gtk.ResponseType.OK:
                at=type_selection.get_current_element()
                if at == newat:
                    new_type_title=new_title.get_text()
                    if new_type_title == '':
                        # Empty title. Generate one.
                        id_=self.controller.package._idgenerator.get_id(AnnotationType)
                        new_type_title=id_
                    else:
                        id_=helper.title2id(new_type_title)
                        # Check that the id is available
                        if self.controller.package._idgenerator.exists(id_):
                            dialog.message_dialog(
                                _("The %s identifier already exists. Choose another one.") % id_,
                                icon=Gtk.MessageType.WARNING)
                            at=None
                            continue
                    # Creating a new type
                    s=schema_selection.get_current_element()
                    at=s.createAnnotationType(ident=id_)
                    at.author=config.data.userid
                    at.date=helper.get_timestamp()
                    at.title=new_type_title
                    at.mimetype='text/plain'
                    at.setMetaData(config.data.namespace, 'color', next(s.rootPackage._color_palette))
                    at.setMetaData(config.data.namespace, 'item_color', 'here/tag_color')
                    s.annotationTypes.append(at)
                    self.controller.notify('AnnotationTypeCreate', annotationtype=at)

                if delete_existing_toggle.get_active():
                    # Remove all annotations of at type
                    batch_id=object()
                    for a in at.annotations:
                        self.controller.delete_element(a, batch=batch_id)

                self.options['empty-annotations']=empty_contents_toggle.get_active()
                finished=True
            else:
                at=None
                finished=True
        d.destroy()

        if at is not None:
            self.options['annotation-type-id'] = at.id
            ti=TranscriptionImporter(package=self.controller.package,
                                     controller=self.controller,
                                     defaulttype=at,
                                     transcription_edit=self)
            ti.process_file('transcription')

            self.controller.package._modified=True
            self.controller.notify("PackageActivate", package=ti.package)
            self.message(_('Notes converted'))
            self.log(ti.statistics_formatted())
            # Feedback
            dialog.message_dialog(
                _("Conversion completed.\n%s annotations generated.") % ti.statistics['annotation'])

        return True
Exemple #36
0
 def get_date(self):
     return helper.get_timestamp()