[564] | 1 | # -*- coding: UTF-8 -*- |
---|
[412] | 2 | # |
---|
| 3 | # pkpgcounter : a generic Page Description Language parser |
---|
| 4 | # |
---|
[564] | 5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 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. |
---|
[463] | 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. |
---|
| 15 | # |
---|
| 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] | 24 | import os |
---|
[529] | 25 | import tempfile |
---|
[519] | 26 | |
---|
[412] | 27 | import pdlparser |
---|
| 28 | import version |
---|
| 29 | |
---|
| 30 | class 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" |
---|
[412] | 35 | def isValid(self) : |
---|
[519] | 36 | """Returns True if data is MS crap, else False. |
---|
[414] | 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. |
---|
[414] | 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 |
---|
| 51 | else : |
---|
| 52 | return True |
---|
[414] | 53 | else : |
---|
| 54 | return False |
---|
[412] | 55 | |
---|
| 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"' |
---|
| 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() |
---|