root / pkpgcounter / trunk / pkpgpdls / escpages03.py @ 491

Revision 491, 3.7 kB (checked in by jerome, 16 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:eol-style set to native
  • Property svn:keywords set to Id 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 TIFF documents."""
24
25import sys
26import os
27import mmap
28from struct import unpack
29
30import pdlparser
31import pjl
32
33class Parser(pdlparser.PDLParser) :
34    """A parser for ESC/PageS03 documents."""
35    def isValid(self) :       
36        """Returns True if data is TIFF, else False."""
37        if self.firstblock.startswith("\033\1@EJL") and \
38            (self.firstblock.find("=ESC/PAGES03\n") != -1) :
39            self.logdebug("DEBUG: Input file is in the ESC/PageS03 format.")
40            return True
41        else :   
42            return False
43   
44    def getJobSize(self) :
45        """Counts pages in an ESC/PageS03 document.
46       
47           Algorithm by Jerome Alet.
48           Reverse engineered the file format.
49        """
50        infileno = self.infile.fileno()
51        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
52        pagecount = 0
53        marker = "=ESC/PAGES03\n"
54        startpos = minfile.find(marker)
55        startsequence = chr(0x1d)
56        if startpos == -1 :
57            raise pdlparser.PDLParserError, "Invalid ESC/PageS03 file."
58        startpos += len(marker)
59        if minfile[startpos] != startsequence :
60            raise pdlparser.PDLParserError, "Invalid ESC/PageS03 file."
61        endsequence = "eps{I"
62        lgendsequence = len(endsequence)
63        try :
64            try :   
65                while True :
66                    if minfile[startpos] == startsequence :
67                        skiplen = 0
68                        while True :
69                            startpos += 1
70                            c = minfile[startpos]
71                            if not c.isdigit() :
72                                break
73                            else :
74                                skiplen = (skiplen * 10) + int(c)
75                        if minfile[startpos:startpos+lgendsequence] == endsequence :
76                            startpos += (skiplen + lgendsequence)
77                    else :
78                        if minfile[startpos:startpos+6] == "\033\1@EJL" :
79                            # Probably near the end of the file.
80                            # Test suite was too small to be sure.
81                            ejlparser = pjl.EJLParser(minfile[startpos:])
82                            pagecount = ejlparser.environment_variables.get("PAGES", "1")
83                            if pagecount.startswith('"') and pagecount.endswith('"') :
84                                pagecount = pagecount[1:-1]
85                            pagecount = int(pagecount)   
86                            if pagecount <= 0 :
87                                pagecount = 1 # TODO : 0 or 1000000 ??? ;-)
88                            break
89                        startpos += 1   
90            except IndexError :           
91                pass
92        finally :       
93            minfile.close()
94        return pagecount
Note: See TracBrowser for help on using the browser.