Changeset 491 for pkpgcounter/trunk/pkpgpdls/pdlparser.py
- Timestamp:
- 11/20/07 00:04:09 (17 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/pdlparser.py
r463 r491 42 42 """Generic PDL parser.""" 43 43 totiffcommands = None # Default command to convert to TIFF 44 def __init__(self, infile, debug=0, firstblock=None, lastblock=None) : 44 openmode = "rb" # Default file opening mode 45 def __init__(self, filename, debug=0) : 45 46 """Initialize the generic parser.""" 46 self. infile = infile47 self.filename = filename 47 48 self.debug = debug 48 if firstblock is None : 49 self.infile.seek(0) 50 firstblock = self.infile.read(FIRSTBLOCKSIZE) 51 try : 52 self.infile.seek(-LASTBLOCKSIZE, 2) 53 lastblock = self.infile.read(LASTBLOCKSIZE) 54 except IOError : 55 lastblock = "" 56 self.infile.seek(0) 57 self.firstblock = firstblock 58 self.lastblock = lastblock 49 self.infile = None 50 (self.firstblock, self.lastblock) = self.readBlocks() 51 self.infile = open(self.filename, self.openmode) 59 52 if not self.isValid() : 60 53 raise PDLParserError, "Invalid file format !" … … 68 61 # parsing will greatly benefit from this. 69 62 psyco.bind(self.getJobSize) 63 64 def __del__(self) : 65 """Ensures the input file gets closed.""" 66 if self.infile : 67 self.infile.close() 68 69 def readBlocks(self) : 70 """Reads first and last block of the input file.""" 71 infile = open(self.filename, "rb") 72 try : 73 firstblock = infile.read(FIRSTBLOCKSIZE) 74 try : 75 infile.seek(-LASTBLOCKSIZE, 2) 76 lastblock = infile.read(LASTBLOCKSIZE) 77 except IOError : 78 lastblock = "" 79 finally : 80 infile.close() 81 return (firstblock, lastblock) 70 82 71 83 def logdebug(self, message) : … … 87 99 """ 88 100 if self.totiffcommands : 89 for totiffcommand in self.totiffcommands : 90 self.infile.seek(0) 91 error = False 92 commandline = totiffcommand % locals() 93 child = popen2.Popen4(commandline) 94 try : 101 infile = open(self.filename, "rb") 102 try : 103 for totiffcommand in self.totiffcommands : 104 infile.seek(0) 105 error = False 106 commandline = totiffcommand % locals() 107 child = popen2.Popen4(commandline) 95 108 try : 96 data = self.infile.read(MEGABYTE) 97 while data : 98 child.tochild.write(data) 99 child.tochild.flush() 100 data = self.infile.read(MEGABYTE) 101 except (IOError, OSError) : 109 try : 110 data = infile.read(MEGABYTE) 111 while data : 112 child.tochild.write(data) 113 child.tochild.flush() 114 data = infile.read(MEGABYTE) 115 except (IOError, OSError) : 116 error = True 117 finally : 118 child.tochild.close() 119 dummy = child.fromchild.read() 120 child.fromchild.close() 121 122 try : 123 status = child.wait() 124 except OSError : 102 125 error = True 103 finally : 104 child.tochild.close() 105 dummy = child.fromchild.read() 106 child.fromchild.close() 107 108 try : 109 status = child.wait() 110 except OSError : 111 error = True 112 else : 113 if os.WIFEXITED(status) : 114 if os.WEXITSTATUS(status) : 126 else : 127 if os.WIFEXITED(status) : 128 if os.WEXITSTATUS(status) : 129 error = True 130 else : 115 131 error = True 132 133 if not os.path.exists(fname) : 134 error = True 135 elif not os.stat(fname).st_size : 136 error = True 116 137 else : 117 error = True 118 119 if not os.path.exists(fname) : 120 error = True 121 elif not os.stat(fname).st_size : 122 error = True 123 else : 124 break # Conversion worked fine it seems. 125 self.logdebug("Command failed : %s" % repr(commandline)) 138 break # Conversion worked fine it seems. 139 self.logdebug("Command failed : %s" % repr(commandline)) 140 finally : 141 infile.close() 126 142 if error : 127 143 raise PDLParserError, "Problem during conversion to TIFF." 128 144 else : 129 145 raise PDLParserError, "Impossible to compute ink coverage for this file format." 130 131 def test(parserclass) :132 """Test function."""133 if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :134 sys.argv.append("-")135 totalsize = 0136 for arg in sys.argv[1:] :137 if arg == "-" :138 infile = sys.stdin139 mustclose = 0140 else :141 infile = open(arg, "rbU")142 mustclose = 1143 try :144 parser = parserclass(infile, debug=1)145 totalsize += parser.getJobSize()146 except PDLParserError, msg :147 sys.stderr.write("ERROR: %s\n" % msg)148 sys.stderr.flush()149 if mustclose :150 infile.close()151 print "%s" % totalsize152