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

Revision 3578, 1.9 kB (checked in by jerome, 5 years ago)

Clarified dependency wrt PIL/Pillow.
Updated copyright years.
Regenerated manual page.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3410]1# -*- coding: utf-8 -*-
[412]2#
3# pkpgcounter : a generic Page Description Language parser
4#
[3578]5# (c) 2003-2019 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.
[3436]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.
[3436]15#
[412]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."""
[3436]28    openmode = "rU"
[555]29    format = "PNM (ascii)"
[3436]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
[3436]35        else :
[414]36            return False
[3436]37
[412]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
[3436]49            # Unfortunately any whitespace is valid,
[553]50            # so we do it the slow way...
[546]51            pagecount += line.split().count(marker)
[3436]52
53        if not (pagecount % divby) :
[551]54            return pagecount // divby
[3436]55        else :
[551]56            return pagecount
Note: See TracBrowser for help on using the browser.