root / pkpgcounter / trunk / pkpgpdls / hbp.py @ 489

Revision 489, 2.6 kB (checked in by jerome, 16 years ago)

Improved code robustness.

  • 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
[480]23"""This modules implements a page counter for Brother HBP documents."""
[356]24
[216]25import sys
26import os
27import mmap
28from struct import unpack
29
[235]30import pdlparser
[216]31
[220]32class Parser(pdlparser.PDLParser) :
[480]33    """A parser for HBP documents."""
[220]34    def isValid(self) :       
[480]35        """Returns True if data is HBP, else False."""
36        if self.firstblock.find("@PJL ENTER LANGUAGE = HBP\n") != -1 :
37            self.logdebug("DEBUG: Input file is in the HBP format.")
38            return True
39        else :   
[387]40            return False
[220]41           
[216]42    def getJobSize(self) :
[480]43        """Counts pages in a HBP document.
[216]44       
45           Algorithm by Jerome Alet.
46           
47           The documentation used for this was :
48         
[480]49           http://sf.net/projects/hbp-for-brother/
50           
51           IMPORTANT : this may not work since @F should be sufficient,
52           but the documentation really is unclear and I don't know
53           how to skip raster data blocks for now.
[216]54        """
55        infileno = self.infile.fileno()
56        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
57        pagecount = 0
[480]58       
59        formfeed = "@G" + chr(0) + chr(0) + chr(1) + chr(0xff) + "@F"
60        fflen = len(formfeed)
61        pos = 0
[216]62        try :
[489]63            try :
64                while True :
65                    if (minfile[pos] == "@") \
66                       and (minfile[pos:pos+fflen] == formfeed) :
67                        pagecount += 1
68                        pos += fflen
69                    else :       
70                        pos += 1
71            except IndexError : # EOF ?
72                pass
73        finally :       
74            minfile.close() # reached EOF
[216]75        return pagecount
76       
77if __name__ == "__main__" :   
[415]78    pdlparser.test(Parser)
Note: See TracBrowser for help on using the browser.