root / pkpgcounter / trunk / pkpgpdls / qpdl.py @ 443

Revision 443, 12.0 kB (checked in by jerome, 17 years ago)

Changed copyright years.

  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
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
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23
24"""This modules implements a page counter for QPDL (aka SPL2) documents."""
25
26import sys
27import os
28import mmap
29from struct import unpack
30
31import pdlparser
32import pjl
33
34class Parser(pdlparser.PDLParser) :
35    """A parser for QPDL (aka SPL2) documents."""
36    mediasizes = { 
37                    # The first values are identical to that of PCLXL
38                    0 : "Letter",
39                    1 : "Legal",
40                    2 : "A4",
41                    3 : "Executive",
42                    4 : "Ledger",
43                    5 : "A3",
44                    6 : "COM10Envelope",
45                    7 : "MonarchEnvelope",
46                    8 : "C5Envelope",
47                    9 : "DLEnvelope",
48                    10 : "JB4",
49                    11 : "JB5",
50                    12 : "B5Envelope",
51                    12 : "B5",
52                    14 : "JPostcard",
53                    15 : "JDoublePostcard",
54                    16 : "A5",
55                    17 : "A6",
56                    18 : "JB6",
57                    21 : "Custom",
58                    23 : "C6",
59                    24 : "Folio",
60                 }   
61                 
62    mediasources = {             
63                     # Again, values are identical to that of PCLXL
64                     0 : "Default",
65                     1 : "Auto",
66                     2 : "Manual",
67                     3 : "MultiPurpose",
68                     4 : "UpperCassette",
69                     5 : "LowerCassette",
70                     6 : "EnvelopeTray",
71                     7 : "ThirdCassette",
72                   }
73           
74    def isValid(self) :   
75        """Returns True if data is QPDL aka SPL2, else False."""
76        if ((self.firstblock[:128].find("\033%-12345X") != -1) and \
77             ((self.firstblock.find("LANGUAGE=QPDL") != -1) or \
78              (self.firstblock.find("LANGUAGE = QPDL") != -1))) :
79            self.logdebug("DEBUG: Input file is in the QPDL (aka SPL2) format.")
80            return True
81        else :   
82            return False
83           
84    def beginPage(self, nextpos) :
85        """Indicates the beginning of a new page, and extracts media information."""
86        self.pagecount += 1
87       
88        copies = unpack(self.unpackShort, self.minfile[nextpos+1:nextpos+3])[0]
89        mediasize = ord(self.minfile[nextpos+3])
90        mediasource = ord(self.minfile[nextpos+8])
91        duplexmode = unpack(self.unpackShort, self.minfile[nextpos+10:nextpos+12])[0]
92       
93        self.pages[self.pagecount] = { "copies" : copies, 
94                                       "mediasize" : self.mediasizes.get(mediasize, str(mediasize)),
95                                       "mediasource" : self.mediasources.get(mediasource, str(mediasource)),
96                                       "duplex" : duplexmode,
97                                     } 
98        return 16       # Length of a page header
99       
100    def endPage(self, nextpos) :   
101        """Indicates the end of a page."""
102        epcopies = unpack(self.unpackShort, self.minfile[nextpos:nextpos+2])[0]
103        bpcopies = self.pages[self.pagecount]["copies"]
104        if epcopies != bpcopies :
105            self.logdebug("ERROR: discrepancy between beginPage (%i) and endPage (%i) copies" % (bpcopies, epcopies))
106        return 2        # Length of a page footer
107       
108    def beginBand(self, nextpos) :
109        """Indicates the beginning of a new band."""
110        bandlength = unpack(self.unpackLong, self.minfile[nextpos+6:nextpos+10])[0]
111        return bandlength + 10 # Length of a band header - length of checksum
112       
113    def littleEndian(self) :
114        """Toggles to little endianness."""
115        self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" }
116        self.unpackShort = self.unpackType[2]
117        self.unpackLong = self.unpackType[4]
118        return 0
119       
120    def bigEndian(self) :
121        """Toggles to big endianness."""
122        self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" }
123        self.unpackShort = self.unpackType[2]
124        self.unpackLong = self.unpackType[4]
125        return 0
126   
127    def escape(self, nextpos) :   
128        """Handles the ESC code."""
129        pos = endpos = nextpos
130        minfile = self.minfile
131        if minfile[pos : pos+8] == r"%-12345X" :
132            endpos = pos + 9
133            endmark = chr(0x0c) + chr(0x00) + chr(0x1b)
134            asciilimit = chr(0x80)
135            quotes = 0
136            while (minfile[endpos] not in endmark) and \
137                   ((minfile[endpos] < asciilimit) or (quotes % 2)) :
138                if minfile[endpos] == '"' :
139                    quotes += 1
140                endpos += 1
141               
142            # Store this in a per page mapping.   
143            # NB : First time will be at page 0 (i.e. **before** page 1) !
144            stuff = self.escapedStuff.setdefault(self.pagecount, [])
145            stuff.append(minfile[pos : endpos])
146            self.logdebug("Escaped datas : [%s]" % repr(minfile[pos : endpos]))
147        return endpos - pos
148       
149    def maybeEOF(self, nextpos) :   
150        """Tries to detect the EOF marker."""
151        if self.minfile[nextpos:nextpos+9] == self.eofmarker :
152            return 9
153        else :   
154            return 0
155       
156    def getJobSize(self) :
157        """Counts pages in a QPDL (SPL2) document.
158       
159           Algorithm by Jerome Alet.
160           
161           The documentation used for this was :
162         
163           Sp�fication Technique (documentation non officielle)
164           Le Language SPL2
165           par Aur�en Croc
166           http://splix.ap2c.org
167        """
168        # Initialize table of tags
169        self.tags = [ lambda pos : 0 ] * 256   
170        self.tags[0x00] = self.beginPage
171        self.tags[0x01] = self.endPage
172        self.tags[0x09] = self.maybeEOF
173        self.tags[0x0c] = self.beginBand
174        self.tags[0x1b] = self.escape # The escape code
175       
176        self.eofmarker = "\033%-12345X"
177       
178        infileno = self.infile.fileno()
179        self.pages = { 0 : { "copies" : 1, 
180                             "orientation" : "Default", 
181                             "mediatype" : "Plain", 
182                             "mediasize" : "Default", 
183                             "mediasource" : "Default", 
184                             "duplex" : None,
185                           } 
186                     }     
187        self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
188        self.pagecount = 0
189        self.escapedStuff = {}   # For escaped datas, mostly PJL commands
190        self.bigEndian()
191        pos = 0
192        tags = self.tags
193        try :
194            try :
195                while 1 :
196                    tag = ord(minfile[pos])
197                    pos += 1
198                    pos += tags[tag](pos)
199            except IndexError : # EOF ?           
200                pass
201        finally :
202            self.minfile.close()
203           
204        defaultduplexmode = "Simplex"
205        defaultpapersize = ""
206        defaultpjlcopies = 1   
207        oldpjlcopies = -1
208        oldduplexmode = ""
209        oldpapersize = ""
210        for pnum in range(1, self.pagecount + 1) :
211            # NB : is number of copies is 0, the page won't be output
212            # but the formula below is still correct : we want
213            # to decrease the total number of pages in this case.
214            page = self.pages.get(pnum, self.pages.get(1, { "copies" : 1, "mediasize" : "Default", "duplex" : None }))
215            pjlstuff = self.escapedStuff.get(pnum, self.escapedStuff.get(0, []))
216            if pjlstuff :
217                pjlparser = pjl.PJLParser("".join(pjlstuff))
218                nbdefaultcopies = int(pjlparser.default_variables.get("COPIES", -1))
219                nbcopies = int(pjlparser.environment_variables.get("COPIES", -1))
220                nbdefaultqty = int(pjlparser.default_variables.get("QTY", -1))
221                nbqty = int(pjlparser.environment_variables.get("QTY", -1))
222                if nbdefaultcopies > -1 :
223                    defaultpjlcopies = nbdefaultcopies
224                if nbdefaultqty > -1 :
225                    defaultpjlcopies = nbdefaultqty
226                if nbcopies > -1 :
227                    pjlcopies = nbcopies
228                elif nbqty > -1 :
229                    pjlcopies = nbqty
230                else :
231                    if oldpjlcopies == -1 :   
232                        pjlcopies = defaultpjlcopies
233                    else :   
234                        pjlcopies = oldpjlcopies   
235                if page["duplex"] :       
236                    duplexmode = page["duplex"]
237                else :   
238                    defaultdm = pjlparser.default_variables.get("DUPLEX", "")
239                    if defaultdm :
240                        if defaultdm.upper() == "ON" :
241                            defaultduplexmode = "Duplex"
242                        else :   
243                            defaultduplexmode = "Simplex"
244                    envdm = pjlparser.environment_variables.get("DUPLEX", "")
245                    if envdm :
246                        if envdm.upper() == "ON" :
247                            duplexmode = "Duplex"
248                        else :   
249                            duplexmode = "Simplex"
250                    else :       
251                        if not oldduplexmode :
252                            duplexmode = defaultduplexmode
253                        else :   
254                            duplexmode = oldduplexmode
255                defaultps = pjlparser.default_variables.get("PAPER", "")
256                if defaultps :
257                    defaultpapersize = defaultps
258                envps = pjlparser.environment_variables.get("PAPER", "")
259                if envps :
260                    papersize = envps
261                else :   
262                    if not oldpapersize :
263                        papersize = defaultpapersize
264                    else :   
265                        papersize = oldpapersize
266            else :       
267                if oldpjlcopies == -1 :
268                    pjlcopies = defaultpjlcopies
269                else :   
270                    pjlcopies = oldpjlcopies
271                if not oldduplexmode :
272                    duplexmode = defaultduplexmode
273                else :   
274                    duplexmode = oldduplexmode
275                if not oldpapersize :   
276                    papersize = defaultpapersize
277                else :   
278                    papersize = oldpapersize
279                duplexmode = oldduplexmode
280                papersize = oldpapersize or page["mediasize"]
281            if page["mediasize"] != "Default" :
282                papersize = page["mediasize"]
283            if not duplexmode :   
284                duplexmode = oldduplexmode or defaultduplexmode
285            oldpjlcopies = pjlcopies   
286            oldduplexmode = duplexmode
287            oldpapersize = papersize
288            copies = pjlcopies * page["copies"]
289            self.pagecount += (copies - 1)
290            self.logdebug("%s*%s*%s*%s" % (copies, 
291                                           papersize, 
292                                           page["mediasource"], 
293                                           duplexmode))
294        return self.pagecount
295       
296if __name__ == "__main__" :   
297    pdlparser.test(Parser)
Note: See TracBrowser for help on using the browser.