Changeset 539 for pkpgcounter/trunk/pkpgpdls/cfax.py
- Timestamp:
- 12/09/07 09:58:08 (17 years ago)
- Files:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/cfax.py
r527 r539 21 21 # 22 22 23 """This modules implements a page counter for DVIdocuments."""23 """This modules implements a page counter for Structured Fax documents.""" 24 24 25 import sys 26 import os 27 import mmap 28 from struct import unpack 25 import struct 29 26 30 27 import pdlparser 31 28 32 29 class Parser(pdlparser.PDLParser) : 33 """A parser for DVI documents.""" 34 totiffcommands = [ 'dvips -q -o - "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" -' ] 35 required = [ "dvips", "gs" ] 30 """A parser for Structured Fax documents.""" 36 31 def isValid(self) : 37 """Returns True if data is DVI, else False.""" 38 try : 39 if (ord(self.firstblock[0]) == 0xf7) \ 40 and (ord(self.lastblock[-1]) == 0xdf) : 41 self.logdebug("DEBUG: Input file is in the DVI format.") 42 return True 43 else : 44 return False 45 except IndexError : 32 """Returns True if data is Structured Fax, else False.""" 33 if self.firstblock.startswith("Sfff") : 34 self.logdebug("DEBUG: Input file is in the Structured Fax format.") 35 return True 36 else : 46 37 return False 47 38 48 39 def getJobSize(self) : 49 """Counts pages in a DVIdocument.40 """Counts pages in a Structured Fax document. 50 41 51 42 Algorithm by Jerome Alet. … … 53 44 The documentation used for this was : 54 45 55 http:// www.math.umd.edu/~asnowden/comp-cont/dvi.html46 http://delphi.pjh2.de/articles/graphic/sff_format.php 56 47 """ 57 infileno = self.infile.fileno() 58 minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) 48 unpack = struct.unpack 59 49 pagecount = 0 60 pos = -1 61 eofchar = chr(0xdf) 62 postchar = chr(0xf8) 50 docheader = self.infile.read(20) 63 51 try : 64 try : 65 while minfile[pos] == eofchar : 66 pos -= 1 67 idbyte = minfile[pos] 68 if idbyte != minfile[1] : 69 raise IndexError, "Invalid DVI file." 70 pos = unpack(">I", minfile[pos - 4:pos])[0] 71 if minfile[pos] != postchar : 72 raise IndexError, "Invalid DVI file." 73 pagecount = unpack(">H", minfile[pos + 27: pos + 29])[0] 74 except IndexError : # EOF ? 75 pass 76 finally : 77 minfile.close() # reached EOF 78 return pagecount 52 (sffid, 53 version, 54 reserved, 55 userinfo, 56 docpagecount, 57 offsetfirstpage, 58 offsetlastpage, 59 offsetdocumentend) = unpack("<4sBBHHHII", docheader) 60 self.infile.seek(offsetfirstpage - len(docheader), 1) 61 while True : 62 headerid = self.infile.read(1) 63 if not headerid : 64 break 65 headerid = ord(headerid) 66 if 1 <= headerid <= 216 : # Normal record header 67 self.infile.seek(headerid, 1) 68 elif headerid == 255 : # Illegal line / Additional user info 69 additionalbyte = self.infile.read(1) 70 if not additionalbyte : 71 break 72 additionalbyte = ord(additionalbyte) 73 if 1 <= additionalbyte <= 255 : 74 # Skip additional user information (reserved) 75 self.infile.seek(additionalbyte, 1) 76 elif not headerid : 77 # Record with more than 216 MH-coded bytes 78 recordlen = self.infile.read(2) 79 if not recordlen : 80 break 81 recordlen = unpack("<H", recordlen)[0] 82 self.infile.seek(recordlen, 1) 83 elif headerid == 254 : # Page header 84 pageheader = self.infile.read(17) 85 if not pageheader : 86 break 87 headerlen = ord(pageheader[0]) 88 if not headerlen : 89 break # End Of Document 90 (vres, 91 hres, 92 coding, 93 reserved, 94 linelen, 95 pagelen, 96 offsetpreviouspage, 97 offsetnextpage) = unpack("<4BHHII", pageheader[1:]) 98 pagecount += 1 99 if (offsetnextpage == 1) or (vres == 255) : 100 break # End Of Document 101 self.infile.seek(offsetnextpage, 1) 102 except struct.error : 103 raise pdlparser.PDLParserError, "Invalid Structured Fax datas" 104 return max(docpagecount, pagecount)