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

Revision 491, 2.5 kB (checked in by jerome, 17 years ago)

Major code cleaning. Now clearer, although probably a bit slower since
a file can be opened several times.
Now universal line opening mode is only used when needed (PS, PDF and plain
text), and binary opening mode is used for the other formats.
This mean we will be able to remove mmap calls wherever possible, finally.

  • Property svn:keywords set to Author Id Date Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
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
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# $Id$
21#
22
23"""This modules implements a page counter for Brother HBP documents."""
24
25import sys
26import os
27import mmap
28from struct import unpack
29
30import pdlparser
31
32class Parser(pdlparser.PDLParser) :
33    """A parser for HBP documents."""
34    def isValid(self) :       
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 :   
40            return False
41           
42    def getJobSize(self) :
43        """Counts pages in a HBP document.
44       
45           Algorithm by Jerome Alet.
46           
47           The documentation used for this was :
48         
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.
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
58       
59        formfeed = "@G" + chr(0) + chr(0) + chr(1) + chr(0xff) + "@F"
60        fflen = len(formfeed)
61        pos = 0
62        try :
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
75        return pagecount
Note: See TracBrowser for help on using the browser.