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

Revision 522, 4.3 kB (checked in by jerome, 17 years ago)

Finally we need to duplicate some datas, since for some file formats (e.g. mstrash)
a preliminary conversion will have to be done (through wvware for example) and we
would need to overwrite original values, which is not desirable.

  • 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
25import popen2
26
27KILOBYTE = 1024   
28MEGABYTE = 1024 * KILOBYTE   
29FIRSTBLOCKSIZE = 16 * KILOBYTE
30LASTBLOCKSIZE = int(KILOBYTE / 4)
31
32class 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       
41class PDLParser :
42    """Generic PDL parser."""
43    totiffcommands = None        # Default command to convert to TIFF
44    openmode = "rb"              # Default file opening mode
45    def __init__(self, parent, (firstblock, lastblock)) :
46        """Initialize the generic parser."""
47        self.parent = parent
48        # We need some copies for later inclusion of parsers which
49        # would modify the parent's values
50        self.filename = parent.filename[:]
51        self.firstblock = firstblock[:]
52        self.lastblock = lastblock[:]
53        self.infile = None
54        if not self.isValid() :
55            raise PDLParserError, "Invalid file format !"
56        try :
57            import psyco 
58        except ImportError :   
59            pass # Psyco is not installed
60        else :   
61            # Psyco is installed, tell it to compile
62            # the CPU intensive methods : PCL and PCLXL
63            # parsing will greatly benefit from this.
64            psyco.bind(self.getJobSize)
65        self.infile = open(self.filename, self.openmode)
66        # self.logdebug("Opened %s in '%s' mode." % (self.filename, self.openmode))
67           
68    def __del__(self) :
69        """Ensures the input file gets closed."""
70        if self.infile :
71            self.infile.close()
72           
73    def logdebug(self, message) :       
74        """Logs a debug message if needed."""
75        if self.parent.options.debug :
76            sys.stderr.write("%s\n" % message)
77           
78    def isValid(self) :   
79        """Returns True if data is in the expected format, else False."""
80        raise RuntimeError, "Not implemented !"
81       
82    def getJobSize(self) :   
83        """Counts pages in a document."""
84        raise RuntimeError, "Not implemented !"
85       
86    def convertToTiffMultiPage24NC(self, outfname, dpi) :
87        """Converts the input file to TIFF format, X dpi, 24 bits per pixel, uncompressed.
88           Writes TIFF datas to the file named by outfname.
89        """   
90        if self.totiffcommands :
91            infname = self.filename
92            for totiffcommand in self.totiffcommands :
93                error = False
94                commandline = totiffcommand % locals()
95                # self.logdebug("Executing '%s'" % commandline)
96                status = os.system(commandline)
97                if os.WIFEXITED(status) :
98                    if os.WEXITSTATUS(status) :
99                        error = True
100                else :       
101                    error = True
102                if not os.path.exists(outfname) :
103                    error = True
104                elif not os.stat(outfname).st_size :
105                    error = True
106                else :       
107                    break       # Conversion worked fine it seems.
108                sys.stderr.write("Command failed : %s\n" % repr(commandline))
109            if error :
110                raise PDLParserError, "Problem during conversion to TIFF."
111        else :       
112            raise PDLParserError, "Impossible to compute ink coverage for this file format."
Note: See TracBrowser for help on using the browser.