| 38 | # Packet types taken from hplip-2.7.10/prnt/ldl.py |
| 39 | PACKET_TYPE_COMMAND = 0 |
| 40 | PACKET_TYPE_DISABLE_PACING = 1 |
| 41 | PACKET_TYPE_ENABLE_PACING = 2 |
| 42 | PACKET_TYPE_RESUME_NORMAL_OPERATION = 3 |
| 43 | PACKET_TYPE_DISABLE_RESPONSES = 4 |
| 44 | PACKET_TYPE_ENABLE_RESPONSES = 5 |
| 45 | PACKET_TYPE_RESET_LIDIL = 6 |
| 46 | PACKET_TYPE_SYNC = 7 |
| 47 | PACKET_TYPE_SYNC_COMPLETE = 8 |
| 48 | |
| 49 | # Command codes we are interested in. |
| 50 | LDL_LOAD_PAGE = 1 |
| 51 | LDL_EJECT_PAGE = 2 |
| 52 | |
59 | | ejectpagemarker = "$\x00\x01\x00\x00\x02" # ensure it's a complete packet (ends with '$') |
60 | | return 0 |
| 74 | pagecount = 0 |
| 75 | infileno = self.infile.fileno() |
| 76 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
| 77 | pos = 0 |
| 78 | try : |
| 79 | try : |
| 80 | while 1 : |
| 81 | if minfile[pos] != "$" : # Frame Sync |
| 82 | raise pdlparser.PDLParserError, "This file doesn't seem to be valid Hewlett-Packard LIDIL datas" |
| 83 | try : |
| 84 | (framesync, |
| 85 | cmdlength, |
| 86 | dummy, |
| 87 | packettype, |
| 88 | commandnumber, |
| 89 | referencenumber, |
| 90 | datalength) = unpack(">BHBBBHH", minfile[pos:pos+10]) |
| 91 | except struct.error : |
| 92 | raise pdlparser.PDLParserError, "This file doesn't seem to be valid Hewlett-Packard LIDIL datas" |
| 93 | if (packettype == PACKET_TYPE_COMMAND) \ |
| 94 | and (commandnumber == LDL_EJECT_PAGE) : |
| 95 | pagecount += 1 |
| 96 | pos += (cmdlength + datalength) |
| 97 | except IndexError : # EOF ? |
| 98 | pass |
| 99 | finally : |
| 100 | minfile.close() |
| 101 | return pagecount |