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

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