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

Revision 551, 2.2 kB (checked in by jerome, 16 years ago)

Improved parser for pksm ghostscript device.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[412]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
[443]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[463]7# This program is free software: you can redistribute it and/or modify
[412]8# it under the terms of the GNU General Public License as published by
[463]9# the Free Software Foundation, either version 3 of the License, or
[412]10# (at your option) any later version.
[463]11#
[412]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
[463]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[412]19#
20# $Id$
21#
22
[546]23"""This modules implements a page counter for PNM (ascii) documents."""
[412]24
25import pdlparser
26import version
27
28class Parser(pdlparser.PDLParser) :
[548]29    """A parser for PNM (ascii) documents."""
[491]30    openmode = "rU"                 
[412]31    def isValid(self) :   
[414]32        """Returns True if data is plain text, else False.
33       
[494]34           It's hard to detect a plain text file, so we just try to
35           extract lines from the first block (sufficiently large).
36           If it's impossible to find one we consider it's not plain text.
[414]37        """   
[551]38        if (self.firstblock.split()[0] in ("P1", "P2", "P3")) :
[546]39            self.logdebug("DEBUG: Input file seems to be in the PNM (ascii) format.")
40            self.marker = self.firstblock[:2]
[414]41            return True
42        else :   
43            return False
[412]44           
45    def getJobSize(self) :
[546]46        """Counts pages in a PNM (ascii) document."""
[412]47        pagecount = 0
[551]48        linecount = 0
49        divby = 1
[546]50        marker = self.marker
[412]51        for line in self.infile :
[551]52            linecount += 1
53            if (linecount == 2) and (line.find("device=pksm") != -1) :
54                # Special case of cmyk map
55                divby = 4
[546]56            pagecount += line.split().count(marker)
[551]57           
58        if not (pagecount % divby) :   
59            return pagecount // divby
60        else :   
61            return pagecount
Note: See TracBrowser for help on using the browser.