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

Revision 247, 3.0 kB (checked in by jerome, 19 years ago)

Don't bother the user with installing Psyco if he doesn't have a 32 bits x86 platform.
Thanks to Matt Stegman for the hint.

  • 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            sys.stderr.write("WARN: If you are running on a 32 Bits x86 platform, you should install the Python Psyco module if possible, this would greatly speedup parsing. NB : Psyco doesn't work on other platforms, so don't worry if you're in this case.\n")
61            pass # Psyco is not installed
62        else :   
63            # Psyco is installed, tell it to compile
64            # the CPU intensive methods : PCL and PCLXL
65            # parsing will greatly benefit from this,
66            # for PostScript and PDF the difference is
67            # barely noticeable since they are already
68            # almost optimal, and much more speedy anyway.
69            psyco.bind(self.getJobSize)
70           
71    def isValid(self) :   
72        """Returns 1 if data is in the expected format, else 0."""
73        raise RuntimeError, "Not implemented !"
74       
75    def getJobSize(self) :   
76        """Counts pages in a document."""
77        raise RuntimeError, "Not implemented !"
Note: See TracBrowser for help on using the browser.