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

Revision 3457, 3.7 kB (checked in by jerome, 16 years ago)

Fixes #31 (will require a fix in PyKota as well, which references #32)
Added some stuff so we can be sure if the temporary files where created
by pkpgcounter and when/where.

  • 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                                               prefix="pkpgcounter_",
64                                               suffix=".ps",
65                                               dir=os.environ.get("PYKOTADIRECTORY") or tempfile.gettempdir())
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
76                return postscript.Parser(self.parent,
77                                         outfname,
78                                         (first, last)).getJobSize()
79            finally :
80                psinputfile.close()
81        finally :
82            workfile.close()
83        raise pdlparser.PDLParserError, "Impossible to count pages in %(infname)s" % locals()
Note: See TracBrowser for help on using the browser.