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

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

Removed spaces at EOL.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# -*- coding: utf-8 -*-
2#
3# pkpgcounter : a generic Page Description Language parser
4#
5# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
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.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id$
20#
21
22"""This module implements a page counter for Microsoft Word (r) (tm) (c) (etc...) documents"""
23
24import os
25import tempfile
26
27import pdlparser
28import version
29
30class Parser(pdlparser.PDLParser) :
31    """A parser for that MS crap thing."""
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"' ]
33    required = [ "xvfb-run", "xauth", "abiword", "gs" ]
34    format = "Microsoft shitty"
35    def isValid(self) :
36        """Returns True if data is MS crap, else False.
37
38           Identifying datas taken from the file command's magic database.
39           IMPORTANT : some magic values are not reused here because they
40           IMPORTANT : seem to be specific to some particular i18n release.
41        """
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") :
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
51            else :
52                return True
53        else :
54            return False
55
56    def getJobSize(self) :
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"'
62        workfile = tempfile.NamedTemporaryFile(mode="w+b")
63        try :
64            outfname = workfile.name
65            infname = self.filename
66            status = os.system(doctops % locals())
67            if status or not os.stat(outfname).st_size :
68                raise pdlparser.PDLParserError, "Impossible to convert input document %(infname)s to PostScript" % locals()
69            psinputfile = open(outfname, "rb")
70            try :
71                (first, last) = self.parent.readFirstAndLastBlocks(psinputfile)
72                import postscript
73                return postscript.Parser(self.parent,
74                                         outfname,
75                                         (first, last)).getJobSize()
76            finally :
77                psinputfile.close()
78        finally :
79            workfile.close()
80        raise pdlparser.PDLParserError, "Impossible to count pages in %(infname)s" % locals()
Note: See TracBrowser for help on using the browser.