root / pkpgcounter / trunk / pkpgpdls / lidil.py @ 522

Revision 522, 4.1 kB (checked in by jerome, 16 years ago)

Finally we need to duplicate some datas, since for some file formats (e.g. mstrash)
a preliminary conversion will have to be done (through wvware for example) and we
would need to overwrite original values, which is not desirable.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[328]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
[443]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[463]7# This program is free software: you can redistribute it and/or modify
[328]8# it under the terms of the GNU General Public License as published by
[463]9# the Free Software Foundation, either version 3 of the License, or
[328]10# (at your option) any later version.
[463]11#
[328]12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
[463]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[328]19#
20# $Id$
21#
22
[484]23"""This modules implements a page counter for HP LIDIL format.
[357]24
[484]25   Documentation used :
26   
27        hplip-2.7.10/prnt/ldl.py
28        hplip-2.7.10/prnt/hpijs/ldlencap.h
29"""
30
[488]31import struct
[328]32
33import pdlparser
34
[491]35HEADERSIZE = 10 # LIDIL header is 10 bytes long
36
[485]37# Packet types taken from hplip-2.7.10/prnt/ldl.py
38PACKET_TYPE_COMMAND = 0
39PACKET_TYPE_DISABLE_PACING = 1
40PACKET_TYPE_ENABLE_PACING = 2
41PACKET_TYPE_RESUME_NORMAL_OPERATION = 3
42PACKET_TYPE_DISABLE_RESPONSES = 4
43PACKET_TYPE_ENABLE_RESPONSES = 5
44PACKET_TYPE_RESET_LIDIL = 6
45PACKET_TYPE_SYNC = 7
46PACKET_TYPE_SYNC_COMPLETE = 8
47
48# Command codes we are interested in.
49LDL_LOAD_PAGE = 1
50LDL_EJECT_PAGE = 2
51
[328]52class Parser(pdlparser.PDLParser) :
[482]53    """A parser for HP LIDIL documents."""
[328]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.
[482]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) :
[482]66            self.logdebug("DEBUG: Input file is in the Hewlett-Packard LIDIL format.")
[387]67            return True
[328]68        else :   
[387]69            return False
[328]70       
71    def getJobSize(self) :
[482]72        """Computes the number of pages in a HP LIDIL document."""
[488]73        unpack = struct.unpack
74        ejectpage = loadpage = 0
[491]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."
82            try :   
83                (framesync,     
84                 cmdlength,
85                 dummy,
86                 packettype,
87                 commandnumber,
88                 referencenumber,
89                 datalength) = unpack(">BHBBBHH", header)
90            except struct.error :   
91                raise pdlparser.PDLParserError, "This file doesn't seem to be valid Hewlett-Packard LIDIL datas."
92            if packettype == PACKET_TYPE_COMMAND :
93                if commandnumber == LDL_LOAD_PAGE :
94                    loadpage += 1
95                elif commandnumber == LDL_EJECT_PAGE :
96                    ejectpage += 1
97            self.infile.seek(cmdlength + datalength - len(header), 1) # relative seek
[488]98           
99        # Number of page eject commands should be sufficient,
100        # but we never know : someone could try to cheat the printer
101        # by loading a page but not ejecting it, and ejecting it manually
102        # later on. Not sure if the printers would support this, but
103        # taking the max value works around the problem in any case.
104        self.logdebug("Load : %i    Eject : %i" % (loadpage, ejectpage))
105        return max(loadpage, ejectpage)
Note: See TracBrowser for help on using the browser.