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

Revision 360, 3.0 kB (checked in by jerome, 18 years ago)

Added a module documentation string.

  • 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, 2006 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
22"""This module defines the base class for all Page Description Language parsers."""
23
24import sys
25
26KILOBYTE = 1024   
27MEGABYTE = 1024 * KILOBYTE   
28FIRSTBLOCKSIZE = 16 * KILOBYTE
29LASTBLOCKSIZE = int(KILOBYTE / 4)
30
31class PDLParserError(Exception):
32    """An exception for PDLParser related stuff."""
33    def __init__(self, message = ""):
34        self.message = message
35        Exception.__init__(self, message)
36    def __repr__(self):
37        return self.message
38    __str__ = __repr__
39       
40class PDLParser :
41    """Generic PDL parser."""
42    def __init__(self, infile, debug=0, firstblock=None, lastblock=None) :
43        """Initialize the generic parser."""
44        self.infile = infile
45        self.debug = debug
46        if firstblock is None :
47            self.infile.seek(0)
48            firstblock = self.infile.read(FIRSTBLOCKSIZE)
49            try :
50                self.infile.seek(-LASTBLOCKSIZE, 2)
51                lastblock = self.infile.read(LASTBLOCKSIZE)
52            except IOError :   
53                lastblock = ""
54            self.infile.seek(0)
55        self.firstblock = firstblock
56        self.lastblock = lastblock
57        if not self.isValid() :
58            raise PDLParserError, "Invalid file format !"
59        try :
60            import psyco 
61        except ImportError :   
62            pass # Psyco is not installed
63        else :   
64            # Psyco is installed, tell it to compile
65            # the CPU intensive methods : PCL and PCLXL
66            # parsing will greatly benefit from this,
67            # for PostScript and PDF the difference is
68            # barely noticeable since they are already
69            # almost optimal, and much more speedy anyway.
70            psyco.bind(self.getJobSize)
71           
72    def logdebug(self, message) :       
73        """Logs a debug message if needed."""
74        if self.debug :
75            sys.stderr.write("%s\n" % message)
76           
77    def isValid(self) :   
78        """Returns 1 if data is in the expected format, else 0."""
79        raise RuntimeError, "Not implemented !"
80       
81    def getJobSize(self) :   
82        """Counts pages in a document."""
83        raise RuntimeError, "Not implemented !"
Note: See TracBrowser for help on using the browser.