Exemplo n.º 1
0
 def _readProperties(self, pdfproperties):
     try:
         xPDFConfig = self._get_pdf_config_access()
         if exists(pdfproperties):
             pxml = ET.parse(pdfproperties)
         else:
             pxml = None
         for (pname, pvalue) in DEFAULT_PDF_PROPS.iteritems():
             if pxml is not None:
                 try:
                     prop = pxml.xpath("/properties/prop[@name='%s']" %pname)[0]
                     ptype = prop.get('type')
                     pvalue = prop.get('value')
                     if ptype == "int":
                         pvalue = int(pvalue)
                     elif ptype == "boolean":
                         pvalue = pvalue == "true"
                 except IndexError:
                     pass
                 except:
                     dbgexc()
             xPDFConfig.setPropertyValue(pname,pvalue)
         xPDFConfig.commitChanges()
     except:
         pass
Exemplo n.º 2
0
 def filter_pivot(self,pivot):
     try:
         xslfile=os.path.join(self.publisher.model.projectpath,'design','publication',self._plugin,'config','filter.xsl')
         xsldoc = ET.parse(xslfile)
         xslt = ET.XSLT(xsldoc)
         filtered_pivot=xslt(pivot)
     except:
         dbgexc()
         return pivot
     return filtered_pivot
Exemplo n.º 3
0
        try:
            if self.params.has_key('zip') and self.params['zip']=='1':
                #produire un zip
                zipname="%s%s.zip" %(self.publisher.profilename, self.label)
                zf=os.path.join(pubpath,zipname)
                zippy = self.publisher.model.get_zip_object()
                zippy.open(zf,"w")
                top=os.path.join(pubpath,label)
                for root, dirs, files in os.walk(top):
                    for name in files:
                        rt=root[len(top) + 1:]
                        zippy.write(str(os.path.join(root, name)),arcname=str(os.path.join(rt, name)))
                zippy.close()
                yield(self.publisher.view.publink('Zip', self.label, '%s/%s' %(linkurl, zipname)))
        except:
            dbgexc()
            yield(self.publisher.view.error(self.publisher.setmessage(u"[0064]Problème lors de la création de l'archive zip")))

    def index(self,pivot):
        idx=ac_index.indexer()
        b=pivot.xpath("/h:html/h:body",namespaces={'h':htmlns})[0]
        for e in b.iterdescendants():
            modid=self.getmodid(e)
            if modid and e.text:
                for word in ac_index.lexer(e.text):
                    idx.addword(word,modid)
            if modid and e.tail:
                for word in ac_index.lexer(e.tail):
                    idx.addword(word,modid)
        return idx.writewords()
Exemplo n.º 4
0
    def postpub(self):
        # get some context
        pubpath = self.publisher.model.pubpath
        pivfile=os.path.join(pubpath,"%s.xml"%self.publisher.model.abstractIO.unicode2local(self.publisher.pivname))
        tplpath = self._META_.get("templates")
        dfile=StringIO()
        #dfile=os.path.join(pubpath,"%s.odt"%self.label)
        theme = self.params.get('template','default')

        # get the theme elements for publishing

        tfile=os.path.join(self.publisher.model.projectpath,tplpath,"%s.ott" %theme)
        mapfile=os.path.join(self.publisher.model.projectpath,tplpath,theme,"mapping.xml")
        #coverfile=os.path.join(self.publisher.model.projectpath,tplpath,theme,"cover.xsl")
        filterfile=os.path.join(self.publisher.model.projectpath,tplpath,theme,"filter.xsl")

        # copy the template into the publication space
        # shutil.copy(tfile, dfile)

        # uncompress the template
        tz = self.publisher.model.get_zip_object()
        tz.open(tfile,"r")

        # create the destination zipfile
        dz = self.publisher.model.get_zip_object()
        dz.open(dfile,"w")

        # get the template index of files
        mf=ET.XML(tz.read('META-INF/manifest.xml'))
        # apply the pre cover.xsl to the pivot file to generate the cover file
        piv=self.publisher.document
        #try:
        #    filter=ET.parse(coverfile)
        #    f=ET.XSLT(filter)
        #    piv=f(piv)
        #except:
        #    dbgexc()
        #    yield(self.publisher.view.error(self.publisher.setmessage(u"[0067][Odt][%(theme)s]Erreur lors de la génération de la page de couverture %(label)s", {'theme': theme, 'label': self.label.encode('utf-8')})))

            #debug(f.error_log )

        # apply the pre filter.xsl to the pivot file
        try:
            filter=ET.parse(filterfile)
            f=ET.XSLT(filter)
            piv=f(piv)
        except:
            dbgexc()
            yield(self.publisher.view.error(self.publisher.setmessage(u"[0068][Odt][%(theme)s]Erreur lors du filtrage de %(label)s", {'theme': theme, 'label': self.label.encode('utf-8')})))

            #debug(f.error_log )

        # handle all media
        odtids={}
        iter = 0
        try:
            for img in piv.xpath('/h:html/h:body//h:img', namespaces={'h':'http://www.w3.org/1999/xhtml'}):
                newsrc = self.publisher.model.url2id(img.get('src'))
                if odtids.get(newsrc, '') == '':
                    # get an uuid for the image
                    odtid = getid(iter)+os.path.splitext(newsrc)[1]
                    odtids.update({newsrc: odtid})
                    iter += 1
                    # copy the media in the zip
                    try:
                        imgdata=self.publisher.model.abstractIO.getFile(newsrc)
                        dz.writestr("Pictures/%s"%str(odtid),imgdata)

                        # registers the image in the manifest
                        ment=ET.SubElement(mf,"{%s}file-entry"%MFNS)
                        ment.set("{%s}media-type"%MFNS,"image/png")
                        ment.set("{%s}full-path"%MFNS,"Pictures/%s"%odtid)
                    except:
                        dbgexc()
                        yield(self.publisher.view.error(self.publisher.setmessage(u"[0069][Odt][%(theme)s]Erreur lors de la copie de l'illustration %(media)s dans le fichier odt %(label)s", 
                                                                                    {'theme': theme, 'media': str(img.get('src')), 'label': self.label.encode('utf-8')})))
                else:
                    odtid = odtids.get(newsrc)
                # inserts the uuid in the pivot for futher references from xslt
                img.set('newimgid',odtid)
                try:
                    # sets the size and def of the image in the pivot for xslt processing
                    im = Image.open(StringIO(self.publisher.model.abstractIO.getFile(newsrc)))
                    (w,h)=im.size
                    img.set('orig_width',str(w))
                    img.set('orig_height',str(h))
                    try:
                        (dw,dh)=im.info['dpi']
                        img.set('orig_resw',str(dw))
                        img.set('orig_resh',str(dh))
                    except:
                        pass
                except:
                    pass
        except:
            dbgexc()
            yield(self.publisher.view.error(self.publisher.setmessage(u"[0070][Odt][%(theme)s]Erreur lors de la copie des illustrations dans le fichier odt %(label)s", 
                                                                      {'theme': theme, 'label': self.label.encode('utf-8')})))

        mmt=mf.xpath('/manifest:manifest/manifest:file-entry[@manifest:full-path]', namespaces={'manifest':MFNS})[0]
        mmt.set('{urn:oasis:names:tc:opendocument:xmlns:manifest:1.0}media-type','application/vnd.oasis.opendocument.text')

        # write back the manifest in the produced odt file
        dz.writestr('META-INF/manifest.xml', bytes=ET.tostring(mf))

        # creates a temporary pivot file (should use an xslt extension for that
        fpiv=open("%s_tmp"%pivfile,'w')
        fpiv.write(ET.tostring(piv))
        fpiv.close()

        # creates a temporary styles file from the template, TODO : use an xslt extension
        fstyles=open("%s_tmpstyles"%pivfile,'w')
        fstyles.write(tz.read('styles.xml'))
        fstyles.close()

        # generates the metadata of the odt file

        tmeta=ET.XML(tz.read('meta.xml'))
        try:
            xslx=ET.parse(os.path.join(self.plugindir,'xsl','generate-meta.xsl'))
            xslt=ET.XSLT(xslx)

            doc=xslt(tmeta,
                     pivot="'%s_tmp'"%self.publisher.model.abstractIO.local2unicode(pivfile))
        except:
            dbgexc()
            yield(self.publisher.view.error(self.publisher.setmessage(u"[0071][Odt][%(theme)s]Erreur lors de la copie des métadonnées de %(label)s", 
                                                                      {'theme': theme, 'label': self.label.encode('utf-8')})))


        #logfile=open(os.path.join(pubpath,"meta.xml"),'w')
        #logfile.write(ET.tostring(doc,pretty_print=True))
        #logfile.close()

        #writeback metadata in the generated file
        dz.writestr('meta.xml', bytes=str(doc))


        # generates the content

        template=ET.XML(tz.read('content.xml'))

        xslx=ET.parse(os.path.join(self.plugindir,'xsl','generate.xsl'))
        xslt=ET.XSLT(xslx)
        try:
            doc=xslt(template,
                     pivot="'%s_tmp'"%urllib.quote(pivfile),
                     styles="'%s_tmpstyles'"%urllib.quote(os.path.join(pubpath,pivfile)),
                     mapping="'%s'"%mapfile)
        except:
            dbgexc()
            yield(self.publisher.view.error(self.publisher.setmessage(u"[0072][Odt][%(theme)s]Erreur lors de la génération du contenu de l'odt %(label)s", 
                                                                      {'theme': theme, 'label': self.label.encode('utf-8')})))


        #logfile=open(os.path.join(pubpath,"content.xml"),'w')
        #logfile.write(ET.tostring(doc,pretty_print=True))
        #logfile.close()

        dz.writestr('content.xml', bytes=str(doc))
        dz.writestr('mimetype', bytes='application/vnd.oasis.opendocument.text')

        # Copy all unhandled files from template to generated doc

        for f in tz.namelist():
            if not dz.namelist().__contains__(f):
                dz.writestr(f, bytes=tz.read(f))

        tz.close()
        dz.close()

        odtfilename = "%s%s" %(self.publisher.profilename, self.suffix)

        # save the generated zip (odt)
        self.publisher.model.putdata(u"%s/%s.odt"%(self.publisher.model.pubdir,odtfilename), dfile.getvalue())

        #removes temporary files
        os.remove("%s_tmp"%pivfile)
        os.remove("%s_tmpstyles"%pivfile)

        # output file name
        yield(self.publisher.view.publink('ODT', self.label, '%s/%s.odt' %(self.publisher.model.local2url(self.publisher.model.pubpath), odtfilename)))

        pdffile = ""
        try:
            epdf=odtpdf.KolektiODTPDF(self.publisher, self.conf)
            # creates a temporary pdf
            odtlocalname = self.publisher.model.abstractIO.unicode2local(u"%s.odt" % odtfilename)
            pdffile = self.publisher.model.abstractIO.unicode2local("%s_tmp.pdf" % odtfilename)
            pdfproperties = os.path.join(self.publisher.model.projectpath,tplpath,theme,"pdf_properties.xml")
            odtpath = os.path.join(self.publisher.model.pubpath, odtlocalname)
            epdf.exportDoc(odtpath,pdffile,pdfproperties)
            pdfpath = os.path.join(pubpath,pdffile)
            pdffd=open(pdfpath)
            self.publisher.model.putdata("%s/%s.pdf"%(self.publisher.model.pubdir,odtfilename),pdffd.read())
            pdffd.close()
            yield(self.publisher.view.publink('PDF', self.label, '%s/%s.pdf' %(self.publisher.model.local2url(self.publisher.model.pubpath), odtfilename)))

            for msg in self.generate_csv(pdfpath):
                yield msg
        except:
            dbgexc()
            yield(self.publisher.view.error(self.publisher.setmessage(u"[0073][Odt][%(theme)s]Erreur lors de la génération du fichier pdf %(label)s", {'theme': theme, 'label': odtfilename.encode('utf-8')})))
Exemplo n.º 5
0
    def postpub(self):
        # get some context
        pubpath = self.publisher.model.pubpath
        pivfile = os.path.join(
            pubpath, "%s.xml" % self.publisher.model.abstractIO.unicode2local(
                self.publisher.pivname))
        tplpath = self._META_.get("templates")
        dfile = StringIO()
        #dfile=os.path.join(pubpath,"%s.odt"%self.label)
        theme = self.params.get('template', 'default')

        # get the theme elements for publishing

        tfile = os.path.join(self.publisher.model.projectpath, tplpath,
                             "%s.ott" % theme)
        mapfile = os.path.join(self.publisher.model.projectpath, tplpath,
                               theme, "mapping.xml")
        #coverfile=os.path.join(self.publisher.model.projectpath,tplpath,theme,"cover.xsl")
        filterfile = os.path.join(self.publisher.model.projectpath, tplpath,
                                  theme, "filter.xsl")

        # copy the template into the publication space
        # shutil.copy(tfile, dfile)

        # uncompress the template
        tz = self.publisher.model.get_zip_object()
        tz.open(tfile, "r")

        # create the destination zipfile
        dz = self.publisher.model.get_zip_object()
        dz.open(dfile, "w")

        # get the template index of files
        mf = ET.XML(tz.read('META-INF/manifest.xml'))
        # apply the pre cover.xsl to the pivot file to generate the cover file
        piv = self.publisher.document
        #try:
        #    filter=ET.parse(coverfile)
        #    f=ET.XSLT(filter)
        #    piv=f(piv)
        #except:
        #    dbgexc()
        #    yield(self.publisher.view.error(self.publisher.setmessage(u"[0067][Odt][%(theme)s]Erreur lors de la génération de la page de couverture %(label)s", {'theme': theme, 'label': self.label.encode('utf-8')})))

        #debug(f.error_log )

        # apply the pre filter.xsl to the pivot file
        try:
            filter = ET.parse(filterfile)
            f = ET.XSLT(filter)
            piv = f(piv)
        except:
            dbgexc()
            yield (self.publisher.view.error(
                self.publisher.setmessage(
                    u"[0068][Odt][%(theme)s]Erreur lors du filtrage de %(label)s",
                    {
                        'theme': theme,
                        'label': self.label.encode('utf-8')
                    })))

            #debug(f.error_log )

        # handle all media
        odtids = {}
        iter = 0
        try:
            for img in piv.xpath(
                    '/h:html/h:body//h:img',
                    namespaces={'h': 'http://www.w3.org/1999/xhtml'}):
                newsrc = self.publisher.model.url2id(img.get('src'))
                if odtids.get(newsrc, '') == '':
                    # get an uuid for the image
                    odtid = getid(iter) + os.path.splitext(newsrc)[1]
                    odtids.update({newsrc: odtid})
                    iter += 1
                    # copy the media in the zip
                    try:
                        imgdata = self.publisher.model.abstractIO.getFile(
                            newsrc)
                        dz.writestr("Pictures/%s" % str(odtid), imgdata)

                        # registers the image in the manifest
                        ment = ET.SubElement(mf, "{%s}file-entry" % MFNS)
                        ment.set("{%s}media-type" % MFNS, "image/png")
                        ment.set("{%s}full-path" % MFNS, "Pictures/%s" % odtid)
                    except:
                        dbgexc()
                        yield (self.publisher.view.error(
                            self.publisher.setmessage(
                                u"[0069][Odt][%(theme)s]Erreur lors de la copie de l'illustration %(media)s dans le fichier odt %(label)s",
                                {
                                    'theme': theme,
                                    'media': str(img.get('src')),
                                    'label': self.label.encode('utf-8')
                                })))
                else:
                    odtid = odtids.get(newsrc)
                # inserts the uuid in the pivot for futher references from xslt
                img.set('newimgid', odtid)
                try:
                    # sets the size and def of the image in the pivot for xslt processing
                    im = Image.open(
                        StringIO(
                            self.publisher.model.abstractIO.getFile(newsrc)))
                    (w, h) = im.size
                    img.set('orig_width', str(w))
                    img.set('orig_height', str(h))
                    try:
                        (dw, dh) = im.info['dpi']
                        img.set('orig_resw', str(dw))
                        img.set('orig_resh', str(dh))
                    except:
                        pass
                except:
                    pass
        except:
            dbgexc()
            yield (self.publisher.view.error(
                self.publisher.setmessage(
                    u"[0070][Odt][%(theme)s]Erreur lors de la copie des illustrations dans le fichier odt %(label)s",
                    {
                        'theme': theme,
                        'label': self.label.encode('utf-8')
                    })))

        mmt = mf.xpath(
            '/manifest:manifest/manifest:file-entry[@manifest:full-path]',
            namespaces={'manifest': MFNS})[0]
        mmt.set(
            '{urn:oasis:names:tc:opendocument:xmlns:manifest:1.0}media-type',
            'application/vnd.oasis.opendocument.text')

        # write back the manifest in the produced odt file
        dz.writestr('META-INF/manifest.xml', bytes=ET.tostring(mf))

        # creates a temporary pivot file (should use an xslt extension for that
        fpiv = open("%s_tmp" % pivfile, 'w')
        fpiv.write(ET.tostring(piv))
        fpiv.close()

        # creates a temporary styles file from the template, TODO : use an xslt extension
        fstyles = open("%s_tmpstyles" % pivfile, 'w')
        fstyles.write(tz.read('styles.xml'))
        fstyles.close()

        # generates the metadata of the odt file

        tmeta = ET.XML(tz.read('meta.xml'))
        try:
            xslx = ET.parse(
                os.path.join(self.plugindir, 'xsl', 'generate-meta.xsl'))
            xslt = ET.XSLT(xslx)

            doc = xslt(tmeta,
                       pivot="'%s_tmp'" %
                       self.publisher.model.abstractIO.local2unicode(pivfile))
        except:
            dbgexc()
            yield (self.publisher.view.error(
                self.publisher.setmessage(
                    u"[0071][Odt][%(theme)s]Erreur lors de la copie des métadonnées de %(label)s",
                    {
                        'theme': theme,
                        'label': self.label.encode('utf-8')
                    })))

        #logfile=open(os.path.join(pubpath,"meta.xml"),'w')
        #logfile.write(ET.tostring(doc,pretty_print=True))
        #logfile.close()

        #writeback metadata in the generated file
        dz.writestr('meta.xml', bytes=str(doc))

        # generates the content

        template = ET.XML(tz.read('content.xml'))

        xslx = ET.parse(os.path.join(self.plugindir, 'xsl', 'generate.xsl'))
        xslt = ET.XSLT(xslx)
        try:
            doc = xslt(template,
                       pivot="'%s_tmp'" % urllib.quote(pivfile),
                       styles="'%s_tmpstyles'" %
                       urllib.quote(os.path.join(pubpath, pivfile)),
                       mapping="'%s'" % mapfile)
        except:
            dbgexc()
            yield (self.publisher.view.error(
                self.publisher.setmessage(
                    u"[0072][Odt][%(theme)s]Erreur lors de la génération du contenu de l'odt %(label)s",
                    {
                        'theme': theme,
                        'label': self.label.encode('utf-8')
                    })))

        #logfile=open(os.path.join(pubpath,"content.xml"),'w')
        #logfile.write(ET.tostring(doc,pretty_print=True))
        #logfile.close()

        dz.writestr('content.xml', bytes=str(doc))
        dz.writestr('mimetype',
                    bytes='application/vnd.oasis.opendocument.text')

        # Copy all unhandled files from template to generated doc

        for f in tz.namelist():
            if not dz.namelist().__contains__(f):
                dz.writestr(f, bytes=tz.read(f))

        tz.close()
        dz.close()

        odtfilename = "%s%s" % (self.publisher.profilename, self.suffix)

        # save the generated zip (odt)
        self.publisher.model.putdata(
            u"%s/%s.odt" % (self.publisher.model.pubdir, odtfilename),
            dfile.getvalue())

        #removes temporary files
        os.remove("%s_tmp" % pivfile)
        os.remove("%s_tmpstyles" % pivfile)

        # output file name
        yield (self.publisher.view.publink(
            'ODT', self.label, '%s/%s.odt' % (self.publisher.model.local2url(
                self.publisher.model.pubpath), odtfilename)))

        pdffile = ""
        try:
            epdf = odtpdf.KolektiODTPDF(self.publisher, self.conf)
            # creates a temporary pdf
            odtlocalname = self.publisher.model.abstractIO.unicode2local(
                u"%s.odt" % odtfilename)
            pdffile = self.publisher.model.abstractIO.unicode2local(
                "%s_tmp.pdf" % odtfilename)
            pdfproperties = os.path.join(self.publisher.model.projectpath,
                                         tplpath, theme, "pdf_properties.xml")
            odtpath = os.path.join(self.publisher.model.pubpath, odtlocalname)
            epdf.exportDoc(odtpath, pdffile, pdfproperties)
            pdfpath = os.path.join(pubpath, pdffile)
            pdffd = open(pdfpath)
            self.publisher.model.putdata(
                "%s/%s.pdf" % (self.publisher.model.pubdir, odtfilename),
                pdffd.read())
            pdffd.close()
            yield (self.publisher.view.publink(
                'PDF', self.label,
                '%s/%s.pdf' % (self.publisher.model.local2url(
                    self.publisher.model.pubpath), odtfilename)))

            for msg in self.generate_csv(pdfpath):
                yield msg
        except:
            dbgexc()
            yield (self.publisher.view.error(
                self.publisher.setmessage(
                    u"[0073][Odt][%(theme)s]Erreur lors de la génération du fichier pdf %(label)s",
                    {
                        'theme': theme,
                        'label': odtfilename.encode('utf-8')
                    })))