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

Revision 555, 3.5 kB (checked in by jerome, 16 years ago)

Each parser now has a 'format' attribute containing its short name.

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