1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # pkpgcounter : a generic Page Description Language parser |
---|
4 | # |
---|
5 | # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | # $Id$ |
---|
20 | # |
---|
21 | |
---|
22 | """This module defines the base class for all Page Description Language parsers.""" |
---|
23 | |
---|
24 | import sys |
---|
25 | import os |
---|
26 | |
---|
27 | KILOBYTE = 1024 |
---|
28 | MEGABYTE = 1024 * KILOBYTE |
---|
29 | FIRSTBLOCKSIZE = 16 * KILOBYTE |
---|
30 | LASTBLOCKSIZE = int(KILOBYTE / 4) |
---|
31 | |
---|
32 | class PDLParserError(Exception): |
---|
33 | """An exception for PDLParser related stuff.""" |
---|
34 | def __init__(self, message = ""): |
---|
35 | self.message = message |
---|
36 | Exception.__init__(self, message) |
---|
37 | def __repr__(self): |
---|
38 | return self.message |
---|
39 | __str__ = __repr__ |
---|
40 | |
---|
41 | class PDLParser : |
---|
42 | """Generic PDL parser.""" |
---|
43 | totiffcommands = None # Default command to convert to TIFF |
---|
44 | required = [] # Default list of required commands |
---|
45 | openmode = "rb" # Default file opening mode |
---|
46 | format = "Unknown" # Default file format |
---|
47 | def __init__(self, parent, filename, (firstblock, lastblock)) : |
---|
48 | """Initialize the generic parser.""" |
---|
49 | self.parent = parent |
---|
50 | # We need some copies for later inclusion of parsers which |
---|
51 | # would modify the parent's values |
---|
52 | self.filename = filename[:] |
---|
53 | self.firstblock = firstblock[:] |
---|
54 | self.lastblock = lastblock[:] |
---|
55 | self.infile = None |
---|
56 | if not self.isValid() : |
---|
57 | raise PDLParserError, "Invalid file format !" |
---|
58 | else : |
---|
59 | self.logdebug("Input file is in the '%s' file format." % self.format) |
---|
60 | try : |
---|
61 | import psyco |
---|
62 | except ImportError : |
---|
63 | pass # Psyco is not installed |
---|
64 | else : |
---|
65 | # Psyco is installed, tell it to compile |
---|
66 | # the CPU intensive methods : PCL and PCLXL |
---|
67 | # parsing will greatly benefit from this. |
---|
68 | psyco.bind(self.getJobSize) |
---|
69 | self.infile = open(self.filename, self.openmode) |
---|
70 | # self.logdebug("Opened %s in '%s' mode." % (self.filename, self.openmode)) |
---|
71 | |
---|
72 | def __del__(self) : |
---|
73 | """Ensures the input file gets closed.""" |
---|
74 | if self.infile : |
---|
75 | self.infile.close() |
---|
76 | |
---|
77 | def findExecutable(self, command) : |
---|
78 | """Finds an executable in the PATH and returns True if found else False.""" |
---|
79 | for cmd in [p.strip() for p in command.split("|")] : # | can separate alternatives for similar commands (e.g. a2ps|enscript) |
---|
80 | for path in os.environ.get("PATH", "").split(":") : |
---|
81 | fullname = os.path.abspath(os.path.join(os.path.expanduser(path), cmd)) |
---|
82 | if os.path.isfile(fullname) and os.access(fullname, os.X_OK) : |
---|
83 | return True |
---|
84 | return False |
---|
85 | |
---|
86 | def isMissing(self, commands) : |
---|
87 | """Returns True if some required commands are missing, else False.""" |
---|
88 | howmanythere = 0 |
---|
89 | for command in commands : |
---|
90 | if not self.findExecutable(command) : |
---|
91 | 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()) |
---|
92 | sys.stderr.flush() |
---|
93 | else : |
---|
94 | howmanythere += 1 |
---|
95 | if howmanythere == len(commands) : |
---|
96 | return False |
---|
97 | else : |
---|
98 | return True |
---|
99 | |
---|
100 | def logdebug(self, message) : |
---|
101 | """Logs a debug message if needed.""" |
---|
102 | if self.parent.options.debug : |
---|
103 | sys.stderr.write("%s\n" % message) |
---|
104 | |
---|
105 | def isValid(self) : |
---|
106 | """Returns True if data is in the expected format, else False.""" |
---|
107 | raise RuntimeError, "Not implemented !" |
---|
108 | |
---|
109 | def getJobSize(self) : |
---|
110 | """Counts pages in a document.""" |
---|
111 | raise RuntimeError, "Not implemented !" |
---|
112 | |
---|
113 | def convertToTiffMultiPage24NC(self, outfname, dpi) : |
---|
114 | """Converts the input file to TIFF format, X dpi, 24 bits per pixel, uncompressed. |
---|
115 | Writes TIFF datas to the file named by outfname. |
---|
116 | """ |
---|
117 | if self.totiffcommands : |
---|
118 | if self.isMissing(self.required) : |
---|
119 | 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) |
---|
120 | infname = self.filename |
---|
121 | for totiffcommand in self.totiffcommands : |
---|
122 | error = False |
---|
123 | commandline = totiffcommand % locals() |
---|
124 | # self.logdebug("Executing '%s'" % commandline) |
---|
125 | status = os.system(commandline) |
---|
126 | if os.WIFEXITED(status) : |
---|
127 | if os.WEXITSTATUS(status) : |
---|
128 | error = True |
---|
129 | else : |
---|
130 | error = True |
---|
131 | if not os.path.exists(outfname) : |
---|
132 | error = True |
---|
133 | elif not os.stat(outfname).st_size : |
---|
134 | error = True |
---|
135 | else : |
---|
136 | break # Conversion worked fine it seems. |
---|
137 | sys.stderr.write("Command failed : %s\n" % repr(commandline)) |
---|
138 | if error : |
---|
139 | raise PDLParserError, "Problem during conversion to TIFF." |
---|
140 | else : |
---|
141 | raise PDLParserError, "Impossible to compute ink coverage for this file format." |
---|