[3410] | 1 | # -*- coding: utf-8 -*- |
---|
[328] | 2 | # |
---|
| 3 | # pkpgcounter : a generic Page Description Language parser |
---|
| 4 | # |
---|
[564] | 5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[463] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[328] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[463] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[328] | 9 | # (at your option) any later version. |
---|
[3436] | 10 | # |
---|
[328] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
[3436] | 15 | # |
---|
[328] | 16 | # You should have received a copy of the GNU General Public License |
---|
[463] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[328] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
| 21 | |
---|
[484] | 22 | """This modules implements a page counter for HP LIDIL format. |
---|
[357] | 23 | |
---|
[3436] | 24 | Documentation used : |
---|
| 25 | |
---|
[484] | 26 | hplip-2.7.10/prnt/ldl.py |
---|
| 27 | hplip-2.7.10/prnt/hpijs/ldlencap.h |
---|
| 28 | """ |
---|
| 29 | |
---|
[488] | 30 | import struct |
---|
[328] | 31 | |
---|
| 32 | import pdlparser |
---|
| 33 | |
---|
[491] | 34 | HEADERSIZE = 10 # LIDIL header is 10 bytes long |
---|
| 35 | |
---|
[485] | 36 | # Packet types taken from hplip-2.7.10/prnt/ldl.py |
---|
| 37 | PACKET_TYPE_COMMAND = 0 |
---|
| 38 | PACKET_TYPE_DISABLE_PACING = 1 |
---|
| 39 | PACKET_TYPE_ENABLE_PACING = 2 |
---|
| 40 | PACKET_TYPE_RESUME_NORMAL_OPERATION = 3 |
---|
| 41 | PACKET_TYPE_DISABLE_RESPONSES = 4 |
---|
| 42 | PACKET_TYPE_ENABLE_RESPONSES = 5 |
---|
| 43 | PACKET_TYPE_RESET_LIDIL = 6 |
---|
| 44 | PACKET_TYPE_SYNC = 7 |
---|
| 45 | PACKET_TYPE_SYNC_COMPLETE = 8 |
---|
| 46 | |
---|
| 47 | # Command codes we are interested in. |
---|
| 48 | LDL_LOAD_PAGE = 1 |
---|
| 49 | LDL_EJECT_PAGE = 2 |
---|
| 50 | |
---|
[328] | 51 | class Parser(pdlparser.PDLParser) : |
---|
[482] | 52 | """A parser for HP LIDIL documents.""" |
---|
[555] | 53 | format = "Hewlett-Packard LIDIL" |
---|
[3436] | 54 | def isValid(self) : |
---|
[482] | 55 | """Returns True if data is LIDIL, else False.""" |
---|
[484] | 56 | # Beginning Of File marker is a Sync packet, followed with |
---|
| 57 | # a Sync Complete packet followed with a Reset packet. |
---|
| 58 | # We just look at the start of the Sync packet for simplicity's sake. |
---|
| 59 | BOFMarker = "$\x01\x00\x00\x07" |
---|
| 60 | # End Of File marker is a Sync Complete packet followed |
---|
| 61 | # with a Reset packet. We ignore the preceding Sync packet |
---|
| 62 | # for simplicity's sake. |
---|
[3436] | 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$" |
---|
[522] | 64 | if self.firstblock.startswith(BOFMarker) \ |
---|
| 65 | and self.lastblock.endswith(EOFMarker) : |
---|
[387] | 66 | return True |
---|
[3436] | 67 | else : |
---|
[387] | 68 | return False |
---|
[3436] | 69 | |
---|
[328] | 70 | def getJobSize(self) : |
---|
[482] | 71 | """Computes the number of pages in a HP LIDIL document.""" |
---|
[488] | 72 | unpack = struct.unpack |
---|
| 73 | ejectpage = loadpage = 0 |
---|
[532] | 74 | try : |
---|
| 75 | while True : |
---|
| 76 | header = self.infile.read(HEADERSIZE) |
---|
| 77 | if not header : |
---|
| 78 | break |
---|
| 79 | if (len(header) != HEADERSIZE) or (header[0] != "$") : |
---|
| 80 | # Invalid header or no Frame Sync byte. |
---|
| 81 | raise pdlparser.PDLParserError, "This file doesn't seem to be valid Hewlett-Packard LIDIL datas." |
---|
[3436] | 82 | (framesync, |
---|
[491] | 83 | cmdlength, |
---|
| 84 | dummy, |
---|
| 85 | packettype, |
---|
| 86 | commandnumber, |
---|
| 87 | referencenumber, |
---|
| 88 | datalength) = unpack(">BHBBBHH", header) |
---|
[532] | 89 | if packettype == PACKET_TYPE_COMMAND : |
---|
| 90 | if commandnumber == LDL_LOAD_PAGE : |
---|
| 91 | loadpage += 1 |
---|
| 92 | elif commandnumber == LDL_EJECT_PAGE : |
---|
| 93 | ejectpage += 1 |
---|
| 94 | self.infile.seek(cmdlength + datalength - len(header), 1) # relative seek |
---|
[3436] | 95 | except struct.error : |
---|
[532] | 96 | raise pdlparser.PDLParserError, "This file doesn't seem to be valid Hewlett-Packard LIDIL datas." |
---|
[3436] | 97 | |
---|
[488] | 98 | # Number of page eject commands should be sufficient, |
---|
| 99 | # but we never know : someone could try to cheat the printer |
---|
| 100 | # by loading a page but not ejecting it, and ejecting it manually |
---|
| 101 | # later on. Not sure if the printers would support this, but |
---|
| 102 | # taking the max value works around the problem in any case. |
---|
| 103 | self.logdebug("Load : %i Eject : %i" % (loadpage, ejectpage)) |
---|
| 104 | return max(loadpage, ejectpage) |
---|