[192] | 1 | # |
---|
| 2 | # pkpgcounter : a generic Page Description Language parser |
---|
| 3 | # |
---|
[443] | 4 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[463] | 5 | # This program is free software: you can redistribute it and/or modify |
---|
[192] | 6 | # it under the terms of the GNU General Public License as published by |
---|
[463] | 7 | # the Free Software Foundation, either version 3 of the License, or |
---|
[192] | 8 | # (at your option) any later version. |
---|
[463] | 9 | # |
---|
[192] | 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 |
---|
[463] | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[192] | 17 | # |
---|
| 18 | # $Id$ |
---|
| 19 | # |
---|
| 20 | |
---|
[360] | 21 | """This module defines the base class for all Page Description Language parsers.""" |
---|
| 22 | |
---|
[199] | 23 | import sys |
---|
[428] | 24 | import os |
---|
[199] | 25 | |
---|
[220] | 26 | KILOBYTE = 1024 |
---|
| 27 | MEGABYTE = 1024 * KILOBYTE |
---|
| 28 | FIRSTBLOCKSIZE = 16 * KILOBYTE |
---|
| 29 | LASTBLOCKSIZE = int(KILOBYTE / 4) |
---|
| 30 | |
---|
[193] | 31 | class 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 | |
---|
[192] | 40 | class PDLParser : |
---|
| 41 | """Generic PDL parser.""" |
---|
[527] | 42 | totiffcommands = None # Default command to convert to TIFF |
---|
| 43 | required = [] # Default list of required commands |
---|
| 44 | openmode = "rb" # Default file opening mode |
---|
[555] | 45 | format = "Unknown" # Default file format |
---|
[529] | 46 | def __init__(self, parent, filename, (firstblock, lastblock)) : |
---|
[192] | 47 | """Initialize the generic parser.""" |
---|
[520] | 48 | self.parent = parent |
---|
[522] | 49 | # We need some copies for later inclusion of parsers which |
---|
| 50 | # would modify the parent's values |
---|
[529] | 51 | self.filename = filename[:] |
---|
[522] | 52 | self.firstblock = firstblock[:] |
---|
| 53 | self.lastblock = lastblock[:] |
---|
[491] | 54 | self.infile = None |
---|
[220] | 55 | if not self.isValid() : |
---|
| 56 | raise PDLParserError, "Invalid file format !" |
---|
[555] | 57 | else : |
---|
| 58 | self.logdebug("Input file is in the '%s' file format." % self.format) |
---|
[220] | 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 |
---|
[432] | 66 | # parsing will greatly benefit from this. |
---|
[220] | 67 | psyco.bind(self.getJobSize) |
---|
[522] | 68 | self.infile = open(self.filename, self.openmode) |
---|
| 69 | # self.logdebug("Opened %s in '%s' mode." % (self.filename, self.openmode)) |
---|
[220] | 70 | |
---|
[491] | 71 | def __del__(self) : |
---|
| 72 | """Ensures the input file gets closed.""" |
---|
| 73 | if self.infile : |
---|
| 74 | self.infile.close() |
---|
| 75 | |
---|
[527] | 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 | |
---|
[252] | 99 | def logdebug(self, message) : |
---|
| 100 | """Logs a debug message if needed.""" |
---|
[520] | 101 | if self.parent.options.debug : |
---|
[252] | 102 | sys.stderr.write("%s\n" % message) |
---|
| 103 | |
---|
[220] | 104 | def isValid(self) : |
---|
[387] | 105 | """Returns True if data is in the expected format, else False.""" |
---|
[192] | 106 | raise RuntimeError, "Not implemented !" |
---|
[220] | 107 | |
---|
| 108 | def getJobSize(self) : |
---|
| 109 | """Counts pages in a document.""" |
---|
| 110 | raise RuntimeError, "Not implemented !" |
---|
[362] | 111 | |
---|
[492] | 112 | def convertToTiffMultiPage24NC(self, outfname, dpi) : |
---|
[362] | 113 | """Converts the input file to TIFF format, X dpi, 24 bits per pixel, uncompressed. |
---|
[492] | 114 | Writes TIFF datas to the file named by outfname. |
---|
[362] | 115 | """ |
---|
[428] | 116 | if self.totiffcommands : |
---|
[527] | 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) |
---|
[522] | 119 | infname = self.filename |
---|
[492] | 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) : |
---|
[428] | 127 | error = True |
---|
[492] | 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)) |
---|
[428] | 137 | if error : |
---|
| 138 | raise PDLParserError, "Problem during conversion to TIFF." |
---|
[371] | 139 | else : |
---|
| 140 | raise PDLParserError, "Impossible to compute ink coverage for this file format." |
---|