Beispiel #1
0
    def __init__(self, controller, destination='/tmp/n', views=None, max_depth=3, progress_callback=None, video_url=None):
        self.controller=controller

        # Directory creation/checks
        self.destination=destination
        self.imgdir=os.path.join(self.destination, 'imagecache')

        if os.path.exists(self.destination) and not os.path.isdir(self.destination):
            self.log(_("%s exists but is not a directory. Cancelling website export") % self.destination)
            return
        elif not os.path.exists(self.destination):
            helper.recursive_mkdir(self.destination)

        if not os.path.isdir(self.destination):
            self.log(_("%s does not exist") % self.destination)
            return

        if views is None:
            views=[ v
                    for v in self.controller.package.views
                    if (not v.id.startswith('_')
                        and v.matchFilter['class'] == 'package'
                        and helper.get_view_type(v) == 'static')
                    ]
        self.views=views
        self.max_depth=max_depth
        if progress_callback is not None:
            # Override dummy progress_callback
            self.progress_callback=progress_callback
        self.video_url=video_url
        self.video_player=self.find_video_player(video_url)

        self.url_translation={}
Beispiel #2
0
    def save_default_options(self, *p):
        """Save the default options.
        """
        d = config.data.advenefile('defaults', 'settings')
        if not os.path.isdir(d):
            # Create it
            try:
                helper.recursive_mkdir(d)
            except OSError as e:
                self.controller.log(
                    _("Cannot save default options: %s") % str(e))
                return True
        defaults = config.data.advenefile(('defaults', self.view_id + '.xml'),
                                          'settings')

        options, args = self.get_save_arguments()
        # Do not save package-specific arguments.
        root = self.parameters_to_element(options, [])
        stream = open(defaults, 'w', encoding='utf-8')
        helper.indent(root)
        ET.ElementTree(root).write(stream, encoding='unicode')
        stream.close()
        self.controller.log(
            _("Default options saved for view %s") % self.view_name)
        return True
Beispiel #3
0
    def __init__(self, controller, destination='/tmp/n', views=None, max_depth=3, progress_callback=None, video_url=None):
        self.controller=controller

        # Directory creation/checks
        self.destination=destination
        self.imgdir=os.path.join(self.destination, 'imagecache')

        if os.path.exists(self.destination) and not os.path.isdir(self.destination):
            self.log(_("%s exists but is not a directory. Cancelling website export") % self.destination)
            return
        elif not os.path.exists(self.destination):
            helper.recursive_mkdir(self.destination)

        if not os.path.isdir(self.destination):
            self.log(_("%s does not exist") % self.destination)
            return

        if views is None:
            views=[ v
                    for v in self.controller.package.views
                    if (not v.id.startswith('_')
                        and v.matchFilter['class'] == 'package'
                        and helper.get_view_type(v) == 'static')
                    ]
        self.views=views
        self.max_depth=max_depth
        if progress_callback is not None:
            # Override dummy progress_callback
            self.progress_callback=progress_callback
        self.video_url=video_url
        self.video_player=self.find_video_player(video_url)

        self.url_translation={}
Beispiel #4
0
 def save_default_options(self, *p):
     """Save the default options.
     """
     d = config.data.advenefile("defaults", "settings")
     if not os.path.isdir(d):
         # Create it
         try:
             helper.recursive_mkdir(d)
         except OSError, e:
             self.controller.log(_("Cannot save default options: %s") % unicode(e))
             return True
Beispiel #5
0
 def save_default_options(self, *p):
     """Save the default options.
     """
     d = config.data.advenefile('defaults', 'settings')
     if not os.path.isdir(d):
         # Create it
         try:
             helper.recursive_mkdir(d)
         except OSError, e:
             self.controller.log(
                 _("Cannot save default options: %s") % unicode(e))
             return True
Beispiel #6
0
    def save_default_options(self, *p):
        """Save the default options.
        """
        d=config.data.advenefile('defaults', 'settings')
        if not os.path.isdir(d):
            # Create it
            try:
                helper.recursive_mkdir(d)
            except OSError as e:
                self.controller.log(_("Cannot save default options: %s") % str(e))
                return True
        defaults=config.data.advenefile( ('defaults', self.view_id + '.xml'), 'settings')

        options, args=self.get_save_arguments()
        # Do not save package-specific arguments.
        root=self.parameters_to_element(options, [])
        stream=open(defaults, 'w', encoding='utf-8')
        helper.indent(root)
        ET.ElementTree(root).write(stream, encoding='unicode')
        stream.close()
        self.controller.log(_("Default options saved for view %s") % self.view_name)
        return True
Beispiel #7
0
    def write_data(self, url, content, used_snapshots, used_overlays,
                   used_resources):
        """Write the converted content as well as associated data.
        """
        # Write the content.
        output = self.url_translation[url]
        f = open(os.path.join(self.destination, output), 'w', encoding='utf-8')
        f.write(content)
        f.close()

        # Copy snapshots
        for t in used_snapshots:
            # FIXME: not robust wrt. multiple packages/videos
            if not os.path.isdir(self.imgdir):
                helper.recursive_mkdir(self.imgdir)
            with open(os.path.join(self.imgdir, '%s.png' % t), 'wb') as f:
                f.write(bytes(self.controller.package.imagecache[t]))

        # Copy overlays
        for (ident, tales) in used_overlays:
            # FIXME: not robust wrt. multiple packages/videos
            a = self.controller.package.get_element_by_id(ident)
            if not a:
                logger.error("Cannot find annotation %s for overlaying", ident)
                continue
            name = ident + tales.replace('/', '_')
            if not os.path.isdir(self.imgdir):
                helper.recursive_mkdir(self.imgdir)
            with open(os.path.join(self.imgdir, 'overlay_%s.png' % name),
                      'wb') as f:
                if tales:
                    # There is a TALES expression
                    ctx = self.controller.build_context(here=a)
                    data = ctx.evaluateValue('here' + tales)
                else:
                    data = a.content.data
                f.write(
                    self.controller.gui.overlay(
                        self.controller.package.imagecache[a.fragment.begin],
                        data))

        # Copy resources
        for path in used_resources:
            dest = os.path.join(self.destination, 'resources', path)

            d = os.path.dirname(dest)
            if not os.path.isdir(d):
                helper.recursive_mkdir(d)

            r = self.controller.package.resources
            for element in path.split('/'):
                r = r[element]
            with open(dest, 'wb') as output:
                data = r.data
                if isinstance(data, str):
                    data = data.encode('utf-8')
                output.write(data)
Beispiel #8
0
    def write_data(self, url, content, used_snapshots, used_overlays, used_resources):
        """Write the converted content as well as associated data.
        """
        # Write the content.
        output=self.url_translation[url]
        f=open(os.path.join(self.destination, output), 'w', encoding='utf-8')
        f.write(content)
        f.close()

        # Copy snapshots
        for t in used_snapshots:
            # FIXME: not robust wrt. multiple packages/videos
            if not os.path.isdir(self.imgdir):
                helper.recursive_mkdir(self.imgdir)
            with open(os.path.join(self.imgdir, '%s.png' % t), 'wb') as f:
                f.write(bytes(self.controller.package.imagecache[t]))

        # Copy overlays
        for (ident, tales) in used_overlays:
            # FIXME: not robust wrt. multiple packages/videos
            a=self.controller.package.get_element_by_id(ident)
            if not a:
                logger.error("Cannot find annotation %s for overlaying", ident)
                continue
            name=ident+tales.replace('/', '_')
            if not os.path.isdir(self.imgdir):
                helper.recursive_mkdir(self.imgdir)
            with open(os.path.join(self.imgdir, 'overlay_%s.png' % name), 'wb') as f:
                if tales:
                    # There is a TALES expression
                    ctx=self.controller.build_context(here=a)
                    data=ctx.evaluateValue('here' + tales)
                else:
                    data=a.content.data
                f.write(self.controller.gui.overlay(self.controller.package.imagecache[a.fragment.begin], data))

        # Copy resources
        for path in used_resources:
            dest=os.path.join(self.destination, 'resources', path)

            d=os.path.dirname(dest)
            if not os.path.isdir(d):
                helper.recursive_mkdir(d)

            r=self.controller.package.resources
            for element in path.split('/'):
                r=r[element]
            with open(dest, 'wb') as output:
                data = r.data
                if isinstance(data, str):
                    data = data.encode('utf-8')
                output.write(data)
Beispiel #9
0
    def write_data(self, url, content, used_snapshots, used_overlays, used_resources):
        """Write the converted content as well as associated data.
        """
        # Write the content.
        output = self.url_translation[url]
        f = open(os.path.join(self.destination, output), "w")
        f.write(content)
        f.close()

        # Copy snapshots
        for t in used_snapshots:
            # FIXME: not robust wrt. multiple packages/videos
            if not os.path.isdir(self.imgdir):
                helper.recursive_mkdir(self.imgdir)
            f = open(os.path.join(self.imgdir, "%s.png" % t), "wb")
            f.write(str(self.controller.package.imagecache[t]))
            f.close()

        # Copy overlays
        for (ident, tales) in used_overlays:
            # FIXME: not robust wrt. multiple packages/videos
            a = self.controller.package.get_element_by_id(ident)
            if not a:
                print "Cannot find annotation %s for overlaying"
                continue
            name = ident + tales.replace("/", "_")
            if not os.path.isdir(self.imgdir):
                helper.recursive_mkdir(self.imgdir)
            f = open(os.path.join(self.imgdir, "overlay_%s.png" % name), "wb")
            if tales:
                # There is a TALES expression
                ctx = self.controller.build_context(here=a)
                data = ctx.evaluateValue("here" + tales)
            else:
                data = a.content.data
            f.write(str(self.controller.gui.overlay(self.controller.package.imagecache[a.fragment.begin], data)))
            f.close()

        # Copy resources
        for path in used_resources:
            dest = os.path.join(self.destination, "resources", path)

            d = os.path.dirname(dest)
            if not os.path.isdir(d):
                helper.recursive_mkdir(d)

            r = self.controller.package.resources
            for element in path.split("/"):
                r = r[element]
            output = open(dest, "wb")
            output.write(r.data)
            output.close()
Beispiel #10
0
    def website_export(self):
        main_step=1.0/self.max_depth

        progress=0
        if not self.progress_callback(progress, _("Starting export")):
            return

        view_url={}
        ctx=self.controller.build_context()
        # Pre-seed url translations for base views
        for v in self.views:
            link="/".join( (ctx.globals['options']['package_url'], 'view', v.id) )
            if hasattr(v, 'content'):
                if v.content.mimetype == 'text/plain':
                    ext='.txt'
                else:
                    ext=mimetypes.guess_extension(v.content.mimetype)
                if ext is not None:
                    self.url_translation[link]="%s%s" % (v.id, ext)
                else:
                    self.url_translation[link]=v.id
            else:
                self.url_translation[link]=v.id
            view_url[v]=link

        progress=.01
        depth=1

        links_to_be_processed=list(view_url.values())

        while depth <= self.max_depth:
            max_depth_exceeded = (depth == self.max_depth)
            step=main_step / (len(links_to_be_processed) or 1)
            if not self.progress_callback(progress, _("Depth %d") % depth):
                return
            links=set()
            for url in links_to_be_processed:
                if not self.progress_callback(progress, _("Depth %(depth)d: processing %(url)s") % locals()):
                    return
                progress += step
                content=self.get_contents(url)

                (new_links,
                 used_snapshots,
                 used_overlays,
                 used_resources)=self.translate_links(content,
                                                      url,
                                                      max_depth_exceeded)
                links.update(new_links)

                # Write contents
                self.write_data(url, self.fix_links(content),
                                used_snapshots,
                                used_overlays,
                                used_resources)

            links_to_be_processed=links
            depth += 1

        if not self.progress_callback(0.95, _("Finalizing")):
            return

        # Copy static video player resources
        for (path, dest) in self.video_player.needed_resources():
            dest=os.path.join(self.destination, dest)
            if os.path.isdir(path):
                # Copy tree
                if os.path.exists(dest):
                    # First remove old version
                    if os.path.isdir(dest):
                        shutil.rmtree(dest, True)
                    else:
                        shutil.unlink(dest)
                shutil.copytree(path, dest)
            else:
                # Copy file
                d=os.path.dirname(dest)
                if not os.path.isdir(d):
                    helper.recursive_mkdir(d)
                shutil.copy(path, dest)

        # Generate video helper files if necessary
        self.video_player.finalize()

        # Generate a default index.html
        name="index.html"
        if name in list(self.url_translation.values()):
            name="_index.html"
        f=open(os.path.join(self.destination, name), 'w', encoding='utf-8')
        defaultview=self.controller.package.getMetaData(config.data.namespace, 'default_utbv')
        v=self.controller.package.views.get_by_id(defaultview)
        if defaultview and v:
            default_href=self.url_translation[view_url[v]]
            default=_("""<p><strong>You should probably begin at <a href="%(href)s">%(title)s</a>.</strong></p>""") % { 'href': default_href, 'title': self.controller.get_title(v) }
        else:
            default_href=''
            default=''

        f.write("""<html><head>%(title)s</head>
<body>
<h1>%(title)s views</h1>
%(default)s
<ul>
%(data)s
</ul></body></html>""" % { 'title': self.controller.package.title,
                           'default': default,
                           'data': "\n".join( '<li><a href="%s">%s</a>' % (self.url_translation[view_url[v]],
                                                                           v.title)
                                              for v in self.views ) })
        f.close()

        frame="frame.html"
        if frame in list(self.url_translation.values()):
            frame="_frame.html"
        f=open(os.path.join(self.destination, frame), 'w', encoding='utf-8')
        f.write("""<html>
<head><title>%(title)s</title></head>
<frameset cols="70%%,30%%">
  <frame name="main" src="%(index)s" />
  <frame name="video_player" src="" />
</frameset>
</html>
""" % {
                'title': self.controller.get_title(self.controller.package),
                'index': default_href or name,
                })
        f.close()

        f=open(os.path.join(self.destination, "unconverted.html"), 'w', encoding='utf-8')
        f.write("""<html><head>%(title)s - not converted</head>
<body>
<h1>%(title)s - not converted resource</h1>
<p>Advene was unable to export this resource.</p>
</body></html>""" % { 'title': self.controller.get_title(self.controller.package) })
        f.close()

        self.progress_callback(1.0, _("Export complete"))
Beispiel #11
0
    def website_export(self):
        main_step=1.0/self.max_depth

        progress=0
        if not self.progress_callback(progress, _("Starting export")):
            return

        view_url={}
        ctx=self.controller.build_context()
        # Pre-seed url translations for base views
        for v in self.views:
            link="/".join( (ctx.globals['options']['package_url'], 'view', v.id) )
            if hasattr(v, 'content'):
                if v.content.mimetype == 'text/plain':
                    ext='.txt'
                else:
                    ext=mimetypes.guess_extension(v.content.mimetype)
                if ext is not None:
                    self.url_translation[link]="%s%s" % (v.id, ext)
                else:
                    self.url_translation[link]=v.id
            else:
                self.url_translation[link]=v.id
            view_url[v]=link

        progress=.01
        depth=1

        links_to_be_processed=view_url.values()

        while depth <= self.max_depth:
            max_depth_exceeded = (depth == self.max_depth)
            step=main_step / (len(links_to_be_processed) or 1)
            if not self.progress_callback(progress, _("Depth %d") % depth):
                return
            links=set()
            for url in links_to_be_processed:
                if not self.progress_callback(progress, _("Depth %(depth)d: processing %(url)s") % locals()):
                    return
                progress += step
                content=self.get_contents(url)

                (new_links,
                 used_snapshots,
                 used_overlays,
                 used_resources)=self.translate_links(content,
                                                      url,
                                                      max_depth_exceeded)
                links.update(new_links)

                # Write contents
                self.write_data(url, self.fix_links(content),
                                used_snapshots,
                                used_overlays,
                                used_resources)

            links_to_be_processed=links
            depth += 1

        if not self.progress_callback(0.95, _("Finalizing")):
            return

        # Copy static video player resources
        for (path, dest) in self.video_player.needed_resources():
            dest=os.path.join(self.destination, dest)
            if os.path.isdir(path):
                # Copy tree
                if os.path.exists(dest):
                    # First remove old version
                    if os.path.isdir(dest):
                        shutil.rmtree(dest, True)
                    else:
                        shutil.unlink(dest)
                shutil.copytree(path, dest)
            else:
                # Copy file
                d=os.path.dirname(dest)
                if not os.path.isdir(d):
                    helper.recursive_mkdir(d)
                shutil.copy(path, dest)

        # Generate video helper files if necessary
        self.video_player.finalize()

        # Generate a default index.html
        name="index.html"
        if name in self.url_translation.values():
            name="_index.html"
        f=open(os.path.join(self.destination, name), 'w')
        defaultview=self.controller.package.getMetaData(config.data.namespace, 'default_utbv')
        v=self.controller.package.views.get_by_id(defaultview)
        if defaultview and v:
            default_href=self.url_translation[view_url[v]]
            default=_("""<p><strong>You should probably begin at <a href="%(href)s">%(title)s</a>.</strong></p>""") % { 'href': default_href, 'title': self.controller.get_title(v) }
        else:
            default_href=''
            default=''

        f.write("""<html><head>%(title)s</head>
<body>
<h1>%(title)s views</h1>
%(default)s
<ul>
%(data)s
</ul></body></html>""" % { 'title': self.controller.package.title,
                           'default': default,
                           'data': "\n".join( '<li><a href="%s">%s</a>' % (self.url_translation[view_url[v]],
                                                                           v.title)
                                              for v in self.views ) })
        f.close()

        frame="frame.html"
        if frame in self.url_translation.values():
            frame="_frame.html"
        f=open(os.path.join(self.destination, frame), 'w')
        f.write("""<html>
<head><title>%(title)s</title></head>
<frameset cols="70%%,30%%">
  <frame name="main" src="%(index)s" />
  <frame name="video_player" src="" />
</frameset>
</html>
""" % {
                'title': self.controller.get_title(self.controller.package),
                'index': default_href or name,
                })
        f.close()

        f=open(os.path.join(self.destination, "unconverted.html"), 'w')
        f.write("""<html><head>%(title)s - not converted</head>
<body>
<h1>%(title)s - not converted resource</h1>
<p>Advene was unable to export this resource.</p>
</body></html>""" % { 'title': self.controller.get_title(self.controller.package) })
        f.close()

        self.progress_callback(1.0, _("Export complete"))
Beispiel #12
0
    def website_export(self):
        main_step = 1.0 / len(self.views)

        progress = 0
        self.progress_callback(progress, _("Starting export"))

        view_url = {}
        ctx = self.controller.build_context()
        # Pre-seed url translations for base views
        for v in self.views:
            link = "/".join((ctx.globals["options"]["package_url"], "view", v.id))
            self.url_translation[link] = v.id
            view_url[v] = link

        progress = 0.1
        for v in self.views:
            link = view_url[v]
            self.progress_callback(progress, _("Exporting ") + v.title)
            self.export_page(link)
            progress += main_step

        self.progress_callback(0.95, _("Copying resources"))

        # Copy used resources
        for path in self.used_resources:
            dest = os.path.join(self.destination, "resources", path)

            d = os.path.dirname(dest)
            if not os.path.isdir(d):
                helper.recursive_mkdir(d)

            r = self.controller.package.resources
            for element in path.split("/"):
                r = r[element]
            output = open(dest, "wb")
            output.write(r.data)
            output.close()

        # Generate video helper files if necessary
        self.video_player.finalize()

        # Generate a default index.html
        f = open(os.path.join(self.destination, "index.html"), "w")
        defaultview = self.controller.package.meta(config.data.namespace + "default_utbv")
        v = self.controller.package.get(defaultview)
        if defaultview and v:
            default = _(
                """<p><strong>You should probably begin at <a href="%(href)s">%(title)s</a>.</strong></p>"""
            ) % {"href": v.id, "title": self.controller.get_title(v)}
        else:
            default = ""
        f.write(
            """<html><head>%(title)s</head>
<body>
<h1>%(title)s views</h1>
%(default)s
<ul>
%(data)s
</ul></body></html>"""
            % {
                "title": self.controller.package.title,
                "default": default,
                "data": "\n".join(
                    '<li><a href="%s">%s</a>' % (self.url_translation[view_url[v]], v.title) for v in self.views
                ),
            }
        )
        f.close()

        f = open(os.path.join(self.destination, "unconverted.html"), "w")
        f.write(
            """<html><head>%(title)s - not converted</head>
<body>
<h1>%(title)s - not converted resource</h1>
<p>Advene was unable to export this resource.</p>
</body></html>"""
            % {"title": self.controller.get_title(self.controller.package)}
        )
        f.close()

        self.progress_callback(1.0, _("Export complete"))