root / pkpgcounter / tags / 1.57 / pkpgpdls / pdlparser.py

Revision 220, 2.8 kB (checked in by jerome, 19 years ago)

Big improvements on readability + maintainability

  • 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
23import psyco
24
25KILOBYTE = 1024   
26MEGABYTE = 1024 * KILOBYTE   
27FIRSTBLOCKSIZE = 16 * KILOBYTE
28LASTBLOCKSIZE = int(KILOBYTE / 4)
29
30class PDLParserError(Exception):
31    """An exception for PDLParser related stuff."""
32    def __init__(self, message = ""):
33        self.message = message
34        Exception.__init__(self, message)
35    def __repr__(self):
36        return self.message
37    __str__ = __repr__
38       
39class PDLParser :
40    """Generic PDL parser."""
41    def __init__(self, infile, debug=0, firstblock=None, lastblock=None) :
42        """Initialize the generic parser."""
43        self.infile = infile
44        self.debug = debug
45        if firstblock is None :
46            self.infile.seek(0)
47            firstblock = self.infile.read(FIRSTBLOCKSIZE)
48            try :
49                self.infile.seek(-LASTBLOCKSIZE, 2)
50                lastblock = self.infile.read(LASTBLOCKSIZE)
51            except IOError :   
52                lastblock = ""
53            self.infile.seek(0)
54        self.firstblock = firstblock
55        self.lastblock = lastblock
56        if not self.isValid() :
57            raise PDLParserError, "Invalid file format !"
58        try :
59            import psyco 
60        except ImportError :   
61            sys.stderr.write("WARN: you should install psyco if possible, this would greatly speedup parsing.\n")
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 isValid(self) :   
73        """Returns 1 if data is in the expected format, else 0."""
74        raise RuntimeError, "Not implemented !"
75       
76    def getJobSize(self) :   
77        """Counts pages in a document."""
78        raise RuntimeError, "Not implemented !"
Note: See TracBrowser for help on using the browser.