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

Revision 564, 1.9 kB (checked in by jerome, 16 years ago)

Changed copyright years.
Removed unnecessary shebang lines.
Changed default encoding to UTF-8 from ISO-8859-15 (only
ascii is used anyway).

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