[193] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[191] | 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
[211] | 6 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
[191] | 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 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 |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
[211] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[191] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[193] | 23 | |
---|
| 24 | import sys |
---|
| 25 | import re |
---|
| 26 | |
---|
[200] | 27 | from pdlanalyzer import pdlparser |
---|
[193] | 28 | |
---|
[220] | 29 | class Parser(pdlparser.PDLParser) : |
---|
[193] | 30 | """A parser for PDF documents.""" |
---|
[220] | 31 | def isValid(self) : |
---|
| 32 | """Returns 1 if data is PDF, else 0.""" |
---|
| 33 | if self.firstblock.startswith("%PDF-") or \ |
---|
| 34 | self.firstblock.startswith("\033%-12345X%PDF-") or \ |
---|
| 35 | ((self.firstblock[:128].find("\033%-12345X") != -1) and (self.firstblock.upper().find("LANGUAGE=PDF") != -1)) or \ |
---|
| 36 | (self.firstblock.find("%PDF-") != -1) : |
---|
| 37 | if self.debug : |
---|
| 38 | sys.stderr.write("DEBUG: Input file is in the PDF format.\n") |
---|
| 39 | return 1 |
---|
| 40 | else : |
---|
| 41 | return 0 |
---|
| 42 | |
---|
[193] | 43 | def getJobSize(self) : |
---|
| 44 | """Counts pages in a PDF document.""" |
---|
[208] | 45 | self.iscolor = None |
---|
| 46 | newpageregexp = re.compile(r"(/Type) ?(/Page)[/ \t\r\n]", re.I) |
---|
| 47 | colorregexp = re.compile(r"(/ColorSpace) ?(/DeviceRGB|/DeviceCMYK)[/ \t\r\n]", re.I) |
---|
[193] | 48 | pagecount = 0 |
---|
| 49 | for line in self.infile.xreadlines() : |
---|
[208] | 50 | pagecount += len(newpageregexp.findall(line)) |
---|
| 51 | if colorregexp.match(line) : |
---|
| 52 | self.iscolor = 1 |
---|
| 53 | if self.debug : |
---|
| 54 | sys.stderr.write("ColorSpace : %s\n" % line) |
---|
[193] | 55 | return pagecount |
---|
| 56 | |
---|
| 57 | def test() : |
---|
| 58 | """Test function.""" |
---|
[196] | 59 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
| 60 | sys.argv.append("-") |
---|
| 61 | totalsize = 0 |
---|
| 62 | for arg in sys.argv[1:] : |
---|
| 63 | if arg == "-" : |
---|
| 64 | infile = sys.stdin |
---|
| 65 | mustclose = 0 |
---|
| 66 | else : |
---|
| 67 | infile = open(arg, "rb") |
---|
| 68 | mustclose = 1 |
---|
| 69 | try : |
---|
[220] | 70 | parser = Parser(infile, debug=1) |
---|
[196] | 71 | totalsize += parser.getJobSize() |
---|
[200] | 72 | except pdlparser.PDLParserError, msg : |
---|
[196] | 73 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 74 | sys.stderr.flush() |
---|
| 75 | if mustclose : |
---|
| 76 | infile.close() |
---|
| 77 | print "%s" % totalsize |
---|
[193] | 78 | |
---|
| 79 | if __name__ == "__main__" : |
---|
| 80 | test() |
---|