root / pkpgcounter / trunk / pkpgpdls / pnmascii.py @ 557

Revision 555, 2.0 kB (checked in by jerome, 17 years ago)

Each parser now has a 'format' attribute containing its short name.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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, 2006, 2007 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 3 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, see <http://www.gnu.org/licenses/>.
19#
20# $Id$
21#
22
23"""This modules implements a page counter for PNM (ascii) documents."""
24
25import pdlparser
26
27class Parser(pdlparser.PDLParser) :
28    """A parser for PNM (ascii) documents."""
29    openmode = "rU"                 
30    format = "PNM (ascii)"
31    def isValid(self) :   
32        """Returns True if data is ASCII PNM, else False."""
33        if self.firstblock.split()[0] in ("P1", "P2", "P3") :
34            self.marker = self.firstblock[:2]
35            return True
36        else :   
37            return False
38           
39    def getJobSize(self) :
40        """Counts pages in a PNM (ascii) document."""
41        pagecount = 0
42        linecount = 0
43        divby = 1
44        marker = self.marker
45        for line in self.infile :
46            linecount += 1
47            if (linecount == 2) and (line.find("device=pksm") != -1) :
48                # Special case of cmyk map
49                divby = 4
50            # Unfortunately any whitespace is valid,
51            # so we do it the slow way...
52            pagecount += line.split().count(marker)
53           
54        if not (pagecount % divby) :   
55            return pagecount // divby
56        else :   
57            return pagecount
Note: See TracBrowser for help on using the browser.