1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # pkpgcounter : a generic Page Description Language parser |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
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 |
---|
19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | |
---|
24 | import sys |
---|
25 | import re |
---|
26 | |
---|
27 | import pdlparser |
---|
28 | |
---|
29 | class Parser(pdlparser.PDLParser) : |
---|
30 | """A parser for PDF documents.""" |
---|
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 | |
---|
43 | def getJobSize(self) : |
---|
44 | """Counts pages in a PDF document.""" |
---|
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) |
---|
48 | pagecount = 0 |
---|
49 | for line in self.infile.xreadlines() : |
---|
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) |
---|
55 | return pagecount |
---|
56 | |
---|
57 | def test() : |
---|
58 | """Test function.""" |
---|
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 : |
---|
70 | parser = Parser(infile, debug=1) |
---|
71 | totalsize += parser.getJobSize() |
---|
72 | except pdlparser.PDLParserError, msg : |
---|
73 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
74 | sys.stderr.flush() |
---|
75 | if mustclose : |
---|
76 | infile.close() |
---|
77 | print "%s" % totalsize |
---|
78 | |
---|
79 | if __name__ == "__main__" : |
---|
80 | test() |
---|