Changeset 520 for pkpgcounter
- Timestamp:
- 11/28/07 00:52:52 (17 years ago)
- Location:
- pkpgcounter/trunk/pkpgpdls
- Files:
-
- 20 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/analyzer.py
r519 r520 56 56 self.workfile = None 57 57 self.mustclose = None 58 self.firstblock = None 59 self.lastblock = None 58 60 59 61 def getJobSize(self) : … … 126 128 self.workfile.close() 127 129 130 def readFirstAndLastBlocks(self) : 131 """Reads the first and last blocks of data.""" 132 # Now read first and last block of the input file 133 # to be able to detect the real file format and the parser to use. 134 self.firstblock = self.workfile.read(pdlparser.FIRSTBLOCKSIZE) 135 try : 136 self.workfile.seek(-pdlparser.LASTBLOCKSIZE, 2) 137 self.lastblock = self.workfile.read(pdlparser.LASTBLOCKSIZE) 138 except IOError : 139 self.lastblock = "" 140 128 141 def detectPDLHandler(self) : 129 142 """Tries to autodetect the document format. … … 133 146 if not os.stat(self.filename).st_size : 134 147 raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) 135 136 # Now read first and last block of the input file 137 # to be able to detect the real file format and the parser to use. 138 firstblock = self.workfile.read(pdlparser.FIRSTBLOCKSIZE) 139 try : 140 self.workfile.seek(-pdlparser.LASTBLOCKSIZE, 2) 141 lastblock = self.workfile.read(pdlparser.LASTBLOCKSIZE) 142 except IOError : 143 lastblock = "" 148 self.readFirstAndLastBlocks() 144 149 145 150 # IMPORTANT : the order is important below. FIXME. … … 162 167 plain) : # IMPORTANT : don't move this one up ! 163 168 try : 164 return module.Parser(self.filename, firstblock, 165 lastblock, 166 self.options.debug) 169 return module.Parser(self) 167 170 except pdlparser.PDLParserError : 168 171 pass # try next parser -
pkpgcounter/trunk/pkpgpdls/dvi.py
r492 r520 36 36 """Returns True if data is DVI, else False.""" 37 37 try : 38 if (ord(self.firstblock[0]) == 0xf7) and (ord(self.lastblock[-1]) == 0xdf) : 38 if (ord(self.parent.firstblock[0]) == 0xf7) \ 39 and (ord(self.parent.lastblock[-1]) == 0xdf) : 39 40 self.logdebug("DEBUG: Input file is in the DVI format.") 40 41 return True -
pkpgcounter/trunk/pkpgpdls/escp2.py
r491 r520 31 31 def isValid(self) : 32 32 """Returns True if data is ESC/P2, else False.""" 33 if self. firstblock.startswith("\033@") or \34 self. firstblock.startswith("\033*") or \35 self. firstblock.startswith("\n\033@") or \36 self. firstblock.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 128433 if self.parent.firstblock.startswith("\033@") or \ 34 self.parent.firstblock.startswith("\033*") or \ 35 self.parent.firstblock.startswith("\n\033@") or \ 36 self.parent.firstblock.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284 37 37 self.logdebug("DEBUG: Input file is in the ESC/P2 format.") 38 38 return True -
pkpgcounter/trunk/pkpgpdls/escpages03.py
r491 r520 35 35 def isValid(self) : 36 36 """Returns True if data is TIFF, else False.""" 37 if self. firstblock.startswith("\033\1@EJL") and \38 (self. firstblock.find("=ESC/PAGES03\n") != -1) :37 if self.parent.firstblock.startswith("\033\1@EJL") and \ 38 (self.parent.firstblock.find("=ESC/PAGES03\n") != -1) : 39 39 self.logdebug("DEBUG: Input file is in the ESC/PageS03 format.") 40 40 return True -
pkpgcounter/trunk/pkpgpdls/hbp.py
r491 r520 34 34 def isValid(self) : 35 35 """Returns True if data is HBP, else False.""" 36 if self. firstblock.find("@PJL ENTER LANGUAGE = HBP\n") != -1 :36 if self.parent.firstblock.find("@PJL ENTER LANGUAGE = HBP\n") != -1 : 37 37 self.logdebug("DEBUG: Input file is in the HBP format.") 38 38 return True -
pkpgcounter/trunk/pkpgpdls/inkcoverage.py
r492 r520 112 112 try : 113 113 image = Image.open(fname) 114 except IOError, msg :114 except (IOError, OverflowError), msg : 115 115 raise pdlparser.PDLParserError, "%s (%s)" % (msg, fname) 116 116 else : -
pkpgcounter/trunk/pkpgpdls/lidil.py
r491 r520 62 62 # for simplicity's sake. 63 63 EOFMarker = "$\x00\x10\x00\x08\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff$$\x00\x10\x00\x06\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff$" 64 if self. firstblock.startswith(BOFMarker) \65 and self. lastblock.endswith(EOFMarker) :64 if self.parent.firstblock.startswith(BOFMarker) \ 65 and self.parent.lastblock.endswith(EOFMarker) : 66 66 self.logdebug("DEBUG: Input file is in the Hewlett-Packard LIDIL format.") 67 67 return True -
pkpgcounter/trunk/pkpgpdls/mstrash.py
r519 r520 36 36 37 37 Identifying datas taken from the file command's magic database. 38 39 38 IMPORTANT : some magic values are not reused here because they 39 IMPORTANT : seem to be specific to some particular i18n release. 40 40 """ 41 if self. firstblock.startswith("PO^Q`") \42 or self. firstblock.startswith("\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1") \43 or self.firstblock.startswith("\xfe7\x00#") \44 or self.firstblock.startswith("\xdb\xa5-\x00\x00\x00") \45 or self.firstblock.startswith("\x31\xbe\x00\x00") \46 or self.firstblock[2112:].startswith("MSWordDoc") :41 if self.parent.firstblock.startswith("PO^Q`") \ 42 or self.parent.firstblock.startswith("\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1") \ 43 or self.parent.firstblock.startswith("\xfe7\x00#") \ 44 or self.parent.firstblock.startswith("\xdb\xa5-\x00\x00\x00") \ 45 or self.parent.firstblock.startswith("\x31\xbe\x00\x00") \ 46 or self.parent.firstblock[2112:].startswith("MSWordDoc") : 47 47 self.logdebug("DEBUG: Input file seems to be in a Microsoft shitty file format.") 48 48 return True -
pkpgcounter/trunk/pkpgpdls/ooo.py
r491 r520 32 32 def isValid(self) : 33 33 """Returns True if data is OpenDocument, else False.""" 34 if self. firstblock[:2] == "PK" :34 if self.parent.firstblock[:2] == "PK" : 35 35 try : 36 36 self.archive = zipfile.ZipFile(self.infile) -
pkpgcounter/trunk/pkpgpdls/pcl345.py
r508 r520 92 92 def isValid(self) : 93 93 """Returns True if data is PCL3/4/5, else False.""" 94 if self. firstblock.startswith("\033E\033") or \95 (self. firstblock.startswith("\033*rbC") and (not self.lastblock[-3:] == "\f\033@")) or \96 self. firstblock.startswith("\033*rB\033") or \97 self. firstblock.startswith("\033%8\033") or \98 (self. firstblock.find("\033%-12345X") != -1) or \99 (self. firstblock.find("@PJL ENTER LANGUAGE=PCL\012\015\033") != -1) or \100 (self. firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.firstblock.find("\033E\033") != -1)) :94 if self.parent.firstblock.startswith("\033E\033") or \ 95 (self.parent.firstblock.startswith("\033*rbC") and (not self.parent.lastblock[-3:] == "\f\033@")) or \ 96 self.parent.firstblock.startswith("\033*rB\033") or \ 97 self.parent.firstblock.startswith("\033%8\033") or \ 98 (self.parent.firstblock.find("\033%-12345X") != -1) or \ 99 (self.parent.firstblock.find("@PJL ENTER LANGUAGE=PCL\012\015\033") != -1) or \ 100 (self.parent.firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.parent.firstblock.find("\033E\033") != -1)) : 101 101 self.logdebug("DEBUG: Input file is in the PCL3/4/5 format.") 102 102 return True -
pkpgcounter/trunk/pkpgpdls/pclxl.py
r492 r520 83 83 def isValid(self) : 84 84 """Returns True if data is PCLXL aka PCL6, else False.""" 85 if (((self. firstblock[:128].find("\033%-12345X") != -1) and \86 (self. firstblock.find(" HP-PCL XL;") != -1) and \87 ((self. firstblock.find("LANGUAGE=PCLXL") != -1) or \88 (self. firstblock.find("LANGUAGE = PCLXL") != -1)))) \89 or ((self. firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.firstblock.find(" HP-PCL XL;") != -1))) :85 if (((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \ 86 (self.parent.firstblock.find(" HP-PCL XL;") != -1) and \ 87 ((self.parent.firstblock.find("LANGUAGE=PCLXL") != -1) or \ 88 (self.parent.firstblock.find("LANGUAGE = PCLXL") != -1)))) \ 89 or ((self.parent.firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.parent.firstblock.find(" HP-PCL XL;") != -1))) : 90 90 self.logdebug("DEBUG: Input file is in the PCLXL (aka PCL6) format.") 91 91 return True -
pkpgcounter/trunk/pkpgpdls/pdf.py
r492 r520 47 47 def isValid(self) : 48 48 """Returns True if data is PDF, else False.""" 49 if self. firstblock.startswith("%PDF-") or \50 self. firstblock.startswith("\033%-12345X%PDF-") or \51 ((self. firstblock[:128].find("\033%-12345X") != -1) and (self.firstblock.upper().find("LANGUAGE=PDF") != -1)) or \52 (self. firstblock.find("%PDF-") != -1) :49 if self.parent.firstblock.startswith("%PDF-") or \ 50 self.parent.firstblock.startswith("\033%-12345X%PDF-") or \ 51 ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and (self.parent.firstblock.upper().find("LANGUAGE=PDF") != -1)) or \ 52 (self.parent.firstblock.find("%PDF-") != -1) : 53 53 self.logdebug("DEBUG: Input file is in the PDF format.") 54 54 return True -
pkpgcounter/trunk/pkpgpdls/pdlparser.py
r493 r520 43 43 totiffcommands = None # Default command to convert to TIFF 44 44 openmode = "rb" # Default file opening mode 45 def __init__(self, filename, firstblock, lastblock, debug=0) :45 def __init__(self, parent) : 46 46 """Initialize the generic parser.""" 47 self.filename = filename 48 self.debug = debug 47 self.parent = parent 49 48 self.infile = None 50 (self.firstblock, self.lastblock) = (firstblock, lastblock)51 49 if not self.isValid() : 52 50 raise PDLParserError, "Invalid file format !" … … 60 58 # parsing will greatly benefit from this. 61 59 psyco.bind(self.getJobSize) 62 self.infile = open(self. filename, self.openmode)63 # self.logdebug("Opened %s in '%s' mode." % (self. filename, self.openmode))60 self.infile = open(self.parent.filename, self.openmode) 61 # self.logdebug("Opened %s in '%s' mode." % (self.parent.filename, self.openmode)) 64 62 65 63 def __del__(self) : … … 70 68 def logdebug(self, message) : 71 69 """Logs a debug message if needed.""" 72 if self. debug :70 if self.parent.options.debug : 73 71 sys.stderr.write("%s\n" % message) 74 72 … … 86 84 """ 87 85 if self.totiffcommands : 88 infname = self. filename86 infname = self.parent.filename 89 87 for totiffcommand in self.totiffcommands : 90 88 error = False -
pkpgcounter/trunk/pkpgpdls/pil.py
r518 r520 35 35 """Returns True if data is an image format supported by PIL, else False.""" 36 36 try : 37 image = Image.open(self. filename)37 image = Image.open(self.parent.filename) 38 38 except (IOError, OverflowError) : 39 39 return False … … 45 45 """Counts pages in an image file.""" 46 46 index = 0 47 image = Image.open(self. filename)47 image = Image.open(self.parent.filename) 48 48 try : 49 49 while True : -
pkpgcounter/trunk/pkpgpdls/plain.py
r495 r520 39 39 If it's impossible to find one we consider it's not plain text. 40 40 """ 41 lines = self. firstblock.split("\r\n")41 lines = self.parent.firstblock.split("\r\n") 42 42 if len(lines) == 1 : 43 43 lines = lines[0].split("\r") -
pkpgcounter/trunk/pkpgpdls/postscript.py
r492 r520 37 37 def isValid(self) : 38 38 """Returns True if data is PostScript, else False.""" 39 if self. firstblock.startswith("%!") or \40 self. firstblock.startswith("\004%!") or \41 self. firstblock.startswith("\033%-12345X%!PS") or \42 ((self. firstblock[:128].find("\033%-12345X") != -1) and \43 ((self. firstblock.find("LANGUAGE=POSTSCRIPT") != -1) or \44 (self. firstblock.find("LANGUAGE = POSTSCRIPT") != -1) or \45 (self. firstblock.find("LANGUAGE = Postscript") != -1))) or \46 (self. firstblock.find("%!PS-Adobe") != -1) :39 if self.parent.firstblock.startswith("%!") or \ 40 self.parent.firstblock.startswith("\004%!") or \ 41 self.parent.firstblock.startswith("\033%-12345X%!PS") or \ 42 ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \ 43 ((self.parent.firstblock.find("LANGUAGE=POSTSCRIPT") != -1) or \ 44 (self.parent.firstblock.find("LANGUAGE = POSTSCRIPT") != -1) or \ 45 (self.parent.firstblock.find("LANGUAGE = Postscript") != -1))) or \ 46 (self.parent.firstblock.find("%!PS-Adobe") != -1) : 47 47 self.logdebug("DEBUG: Input file is in the PostScript format.") 48 48 return True … … 57 57 # we need to reopen the input file in binary mode again, just in case 58 58 # otherwise we might break the original file's contents. 59 infile = open(self. filename, "rb")59 infile = open(self.parent.filename, "rb") 60 60 try : 61 61 child = popen2.Popen4(command) -
pkpgcounter/trunk/pkpgpdls/qpdl.py
r491 r520 73 73 def isValid(self) : 74 74 """Returns True if data is QPDL aka SPL2, else False.""" 75 if ((self. firstblock[:128].find("\033%-12345X") != -1) and \76 ((self. firstblock.find("LANGUAGE=QPDL") != -1) or \77 (self. firstblock.find("LANGUAGE = QPDL") != -1))) :75 if ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \ 76 ((self.parent.firstblock.find("LANGUAGE=QPDL") != -1) or \ 77 (self.parent.firstblock.find("LANGUAGE = QPDL") != -1))) : 78 78 self.logdebug("DEBUG: Input file is in the QPDL (aka SPL2) format.") 79 79 return True -
pkpgcounter/trunk/pkpgpdls/spl1.py
r493 r520 37 37 def isValid(self) : 38 38 """Returns True if data is SPL1, else False.""" 39 if ((self. firstblock[:128].find("\033%-12345X") != -1) and \40 (self. firstblock.find("$PJL ") != -1) and \41 ((self. firstblock.find("LANGUAGE=SMART") != -1) or \42 (self. firstblock.find("LANGUAGE = SMART") != -1))) :39 if ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \ 40 (self.parent.firstblock.find("$PJL ") != -1) and \ 41 ((self.parent.firstblock.find("LANGUAGE=SMART") != -1) or \ 42 (self.parent.firstblock.find("LANGUAGE = SMART") != -1))) : 43 43 self.logdebug("DEBUG: Input file is in the SPL1 format.") 44 44 return True -
pkpgcounter/trunk/pkpgpdls/tiff.py
r492 r520 37 37 littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0) 38 38 bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a) 39 if self. firstblock[:4] in (littleendian, bigendian) :39 if self.parent.firstblock[:4] in (littleendian, bigendian) : 40 40 self.logdebug("DEBUG: Input file is in the TIFF format.") 41 41 return True -
pkpgcounter/trunk/pkpgpdls/zjstream.py
r491 r520 31 31 def isValid(self) : 32 32 """Returns True if data is ZjStream, else False.""" 33 if self. firstblock[:4] == "ZJZJ" :33 if self.parent.firstblock[:4] == "ZJZJ" : 34 34 self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.") 35 35 return self.littleEndian() 36 elif self. firstblock[:4] == "JZJZ" :36 elif self.parent.firstblock[:4] == "JZJZ" : 37 37 self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.") 38 38 return self.bigEndian()