root / pkpgcounter / trunk / pkpgpdls / pdlparser.py @ 271

Revision 271, 2.9 kB (checked in by jerome, 18 years ago)

Now warns about psyco missing only during setup, and stays quiet while running.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Auth Date Id Rev
Line 
1#
2# pkpgcounter : a generic Page Description Language parser
3#
4# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18#
19# $Id$
20#
21
22import sys
23
24KILOBYTE = 1024   
25MEGABYTE = 1024 * KILOBYTE   
26FIRSTBLOCKSIZE = 16 * KILOBYTE
27LASTBLOCKSIZE = int(KILOBYTE / 4)
28
29class PDLParserError(Exception):
30    """An exception for PDLParser related stuff."""
31    def __init__(self, message = ""):
32        self.message = message
33        Exception.__init__(self, message)
34    def __repr__(self):
35        return self.message
36    __str__ = __repr__
37       
38class PDLParser :
39    """Generic PDL parser."""
40    def __init__(self, infile, debug=0, firstblock=None, lastblock=None) :
41        """Initialize the generic parser."""
42        self.infile = infile
43        self.debug = debug
44        if firstblock is None :
45            self.infile.seek(0)
46            firstblock = self.infile.read(FIRSTBLOCKSIZE)
47            try :
48                self.infile.seek(-LASTBLOCKSIZE, 2)
49                lastblock = self.infile.read(LASTBLOCKSIZE)
50            except IOError :   
51                lastblock = ""
52            self.infile.seek(0)
53        self.firstblock = firstblock
54        self.lastblock = lastblock
55        if not self.isValid() :
56            raise PDLParserError, "Invalid file format !"
57        try :
58            import psyco 
59        except ImportError :   
60            pass # Psyco is not installed
61        else :   
62            # Psyco is installed, tell it to compile
63            # the CPU intensive methods : PCL and PCLXL
64            # parsing will greatly benefit from this,
65            # for PostScript and PDF the difference is
66            # barely noticeable since they are already
67            # almost optimal, and much more speedy anyway.
68            psyco.bind(self.getJobSize)
69           
70    def logdebug(self, message) :       
71        """Logs a debug message if needed."""
72        if self.debug :
73            sys.stderr.write("%s\n" % message)
74           
75    def isValid(self) :   
76        """Returns 1 if data is in the expected format, else 0."""
77        raise RuntimeError, "Not implemented !"
78       
79    def getJobSize(self) :   
80        """Counts pages in a document."""
81        raise RuntimeError, "Not implemented !"
Note: See TracBrowser for help on using the browser.