root / pkpgcounter / trunk / pdlanalyzer / pdf.py @ 208

Revision 208, 2.3 kB (checked in by jerome, 19 years ago)

Use raw strings now, maybe it will help with the psyco problem

  • Property svn:eol-style set to native
  • Property svn:keywords set to Auth Date Id Rev
Line 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20#
21# $Id$
22#
23
24import sys
25import re
26
27from pdlanalyzer import pdlparser
28
29class PDFParser(pdlparser.PDLParser) :
30    """A parser for PDF documents."""
31    def getJobSize(self) :   
32        """Counts pages in a PDF document."""
33        self.iscolor = None
34        newpageregexp = re.compile(r"(/Type) ?(/Page)[/ \t\r\n]", re.I)
35        colorregexp = re.compile(r"(/ColorSpace) ?(/DeviceRGB|/DeviceCMYK)[/ \t\r\n]", re.I)
36        pagecount = 0
37        for line in self.infile.xreadlines() : 
38            pagecount += len(newpageregexp.findall(line))
39            if colorregexp.match(line) :
40                self.iscolor = 1
41                if self.debug :
42                    sys.stderr.write("ColorSpace : %s\n" % line)
43        return pagecount   
44       
45def test() :       
46    """Test function."""
47    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
48        sys.argv.append("-")
49    totalsize = 0   
50    for arg in sys.argv[1:] :
51        if arg == "-" :
52            infile = sys.stdin
53            mustclose = 0
54        else :   
55            infile = open(arg, "rb")
56            mustclose = 1
57        try :
58            parser = PDFParser(infile, debug=1)
59            totalsize += parser.getJobSize()
60        except pdlparser.PDLParserError, msg :   
61            sys.stderr.write("ERROR: %s\n" % msg)
62            sys.stderr.flush()
63        if mustclose :   
64            infile.close()
65    print "%s" % totalsize
66   
67if __name__ == "__main__" :   
68    test()
Note: See TracBrowser for help on using the browser.