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

Revision 555, 5.8 kB (checked in by jerome, 16 years ago)

Each parser now has a 'format' attribute containing its short name.

  • 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, 2007 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 3 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, see <http://www.gnu.org/licenses/>.
17#
18# $Id$
19#
20
21"""This module defines the base class for all Page Description Language parsers."""
22
23import sys
24import os
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    totiffcommands = None       # Default command to convert to TIFF
43    required = []               # Default list of required commands
44    openmode = "rb"             # Default file opening mode
45    format = "Unknown"          # Default file format
46    def __init__(self, parent, filename, (firstblock, lastblock)) :
47        """Initialize the generic parser."""
48        self.parent = parent
49        # We need some copies for later inclusion of parsers which
50        # would modify the parent's values
51        self.filename = filename[:]
52        self.firstblock = firstblock[:]
53        self.lastblock = lastblock[:]
54        self.infile = None
55        if not self.isValid() :
56            raise PDLParserError, "Invalid file format !"
57        else :   
58            self.logdebug("Input file is in the '%s' file format." % self.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            psyco.bind(self.getJobSize)
68        self.infile = open(self.filename, self.openmode)
69        # self.logdebug("Opened %s in '%s' mode." % (self.filename, self.openmode))
70           
71    def __del__(self) :
72        """Ensures the input file gets closed."""
73        if self.infile :
74            self.infile.close()
75           
76    def findExecutable(self, command) :
77        """Finds an executable in the PATH and returns True if found else False."""
78        for cmd in [p.strip() for p in command.split("|")] : # | can separate alternatives for similar commands (e.g. a2ps|enscript)
79            for path in os.environ.get("PATH", "").split(":") :
80                fullname = os.path.abspath(os.path.join(os.path.expanduser(path), cmd))
81                if os.path.isfile(fullname) and os.access(fullname, os.X_OK) :
82                    return True
83        return False
84       
85    def isMissing(self, commands) :   
86        """Returns True if some required commands are missing, else False.""" 
87        howmanythere = 0
88        for command in commands :
89            if not self.findExecutable(command) :
90                sys.stderr.write("ERROR: %(command)s is missing or not executable. You MUST install it for pkpgcounter to be able to do what you want.\n" % locals())
91                sys.stderr.flush()
92            else :   
93                howmanythere += 1
94        if howmanythere == len(commands) :
95            return False
96        else :   
97            return True
98       
99    def logdebug(self, message) :       
100        """Logs a debug message if needed."""
101        if self.parent.options.debug :
102            sys.stderr.write("%s\n" % message)
103           
104    def isValid(self) :   
105        """Returns True if data is in the expected format, else False."""
106        raise RuntimeError, "Not implemented !"
107       
108    def getJobSize(self) :   
109        """Counts pages in a document."""
110        raise RuntimeError, "Not implemented !"
111       
112    def convertToTiffMultiPage24NC(self, outfname, dpi) :
113        """Converts the input file to TIFF format, X dpi, 24 bits per pixel, uncompressed.
114           Writes TIFF datas to the file named by outfname.
115        """   
116        if self.totiffcommands :
117            if self.isMissing(self.required) :
118                raise PDLParserError, "At least one of the following commands is missing and should be installed for the computation of ink coverage : %s" % repr(self.required)
119            infname = self.filename
120            for totiffcommand in self.totiffcommands :
121                error = False
122                commandline = totiffcommand % locals()
123                # self.logdebug("Executing '%s'" % commandline)
124                status = os.system(commandline)
125                if os.WIFEXITED(status) :
126                    if os.WEXITSTATUS(status) :
127                        error = True
128                else :       
129                    error = True
130                if not os.path.exists(outfname) :
131                    error = True
132                elif not os.stat(outfname).st_size :
133                    error = True
134                else :       
135                    break       # Conversion worked fine it seems.
136                sys.stderr.write("Command failed : %s\n" % repr(commandline))
137            if error :
138                raise PDLParserError, "Problem during conversion to TIFF."
139        else :       
140            raise PDLParserError, "Impossible to compute ink coverage for this file format."
Note: See TracBrowser for help on using the browser.