root / pkpgcounter / trunk / pkpgpdls / bj.py @ 555

Revision 555, 2.4 kB (checked in by jerome, 16 years ago)

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

  • Property svn:keywords set to Author Id Date Revision
RevLine 
[216]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
[216]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
[216]10# (at your option) any later version.
[463]11#
[216]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/>.
[216]19#
20# $Id$
21#
22
[545]23"""This modules implements a page counter for Canon BJ documents."""
[356]24
[216]25import os
26import mmap
27
[235]28import pdlparser
[216]29
[220]30class Parser(pdlparser.PDLParser) :
[545]31    """A parser for Canon BJ documents."""
[555]32    format = "Canon BJ/BJC"
[220]33    def isValid(self) :       
[552]34        """Returns True if data is BJ/BJC, else False."""
[545]35        if self.firstblock.startswith("\033[K\002\000") :
36            return True
37        else :   
[387]38            return False
[220]39           
[216]40    def getJobSize(self) :
[545]41        """Counts pages in a Canon BJ document.
[216]42       
43           Algorithm by Jerome Alet.
44           
45           The documentation used for this was :
46         
[545]47           ghostscript-8.60/src/gdevbj*.c
[216]48        """
49        infileno = self.infile.fileno()
50        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
51        pagecount = 0
[545]52        pos = 0
[216]53        try :
[489]54            try :
[545]55                while True :
56                    if minfile[pos] == "\033" :
57                        # Look if we've found an initialization sequence
58                        # through the Set Initial Condition command
59                        pageheader = minfile[pos:pos+7]
60                        if pageheader in ("\033[K\002\000\000\017", 
61                                          "\033[K\002\000\000\044",
62                                          "\033[K\002\000\004\044") :
63                            pagecount += 1
64                            pos += 6
65                    pos += 1
[489]66            except IndexError : # EOF ?
67                pass
68        finally :       
69            minfile.close() # reached EOF
[216]70        return pagecount
Note: See TracBrowser for help on using the browser.