root / pkpgcounter / trunk / pkpgpdls / mscrap.py @ 3474

Revision 3474, 3.7 kB (checked in by jerome, 15 years ago)

Changed copyright years.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3410]1# -*- coding: utf-8 -*-
[412]2#
3# pkpgcounter : a generic Page Description Language parser
4#
[3474]5# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
[463]6# This program is free software: you can redistribute it and/or modify
[412]7# it under the terms of the GNU General Public License as published by
[463]8# the Free Software Foundation, either version 3 of the License, or
[412]9# (at your option) any later version.
[3436]10#
[412]11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
[3436]15#
[412]16# You should have received a copy of the GNU General Public License
[463]17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[412]18#
19# $Id$
20#
21
[519]22"""This module implements a page counter for Microsoft Word (r) (tm) (c) (etc...) documents"""
[412]23
[519]24import os
[529]25import tempfile
[519]26
[412]27import pdlparser
28import version
29
30class Parser(pdlparser.PDLParser) :
[519]31    """A parser for that MS crap thing."""
[525]32    totiffcommands = [ 'xvfb-run -a abiword --import-extension=.doc --print="| gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r\"%(dpi)i\" -sOutputFile=\"%(outfname)s\" -" "%(infname)s"' ]
[527]33    required = [ "xvfb-run", "xauth", "abiword", "gs" ]
[555]34    format = "Microsoft shitty"
[3436]35    def isValid(self) :
[519]36        """Returns True if data is MS crap, else False.
[3436]37
[519]38           Identifying datas taken from the file command's magic database.
[520]39           IMPORTANT : some magic values are not reused here because they
40           IMPORTANT : seem to be specific to some particular i18n release.
[3436]41        """
[522]42        if self.firstblock.startswith("PO^Q`") \
43           or self.firstblock.startswith("\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1") \
44           or self.firstblock.startswith("\xfe7\x00#") \
45           or self.firstblock.startswith("\xdb\xa5-\x00\x00\x00") \
46           or self.firstblock.startswith("\x31\xbe\x00\x00") \
47           or self.firstblock[2112:].startswith("MSWordDoc") :
[527]48            # Here we do the missing test because all commands will be needed even in page counting mode
49            if self.isMissing(self.required) :
50                return False
[3436]51            else :
[527]52                return True
[3436]53        else :
[414]54            return False
[3436]55
[412]56    def getJobSize(self) :
[529]57        """Counts pages in a Microsoft Word (r) (tm) (c) (etc...) document.
58
59           First we convert from .doc to .ps, then we use the PostScript parser.
60        """
61        doctops = 'xvfb-run -a abiword --import-extension=.doc --print="%(outfname)s" "%(infname)s"'
[3457]62        workfile = tempfile.NamedTemporaryFile(mode="w+b",
63                                               prefix="pkpgcounter_",
64                                               suffix=".ps",
65                                               dir=os.environ.get("PYKOTADIRECTORY") or tempfile.gettempdir())
[529]66        try :
67            outfname = workfile.name
68            infname = self.filename
69            status = os.system(doctops % locals())
70            if status or not os.stat(outfname).st_size :
71                raise pdlparser.PDLParserError, "Impossible to convert input document %(infname)s to PostScript" % locals()
72            psinputfile = open(outfname, "rb")
73            try :
74                (first, last) = self.parent.readFirstAndLastBlocks(psinputfile)
75                import postscript
[3436]76                return postscript.Parser(self.parent,
77                                         outfname,
[529]78                                         (first, last)).getJobSize()
79            finally :
80                psinputfile.close()
[3436]81        finally :
[529]82            workfile.close()
83        raise pdlparser.PDLParserError, "Impossible to count pages in %(infname)s" % locals()
Note: See TracBrowser for help on using the browser.