Show
Ignore:
Timestamp:
09/03/05 00:40:57 (19 years ago)
Author:
jerome
Message:

Added a PJL parsing module to extract SET and DEFAULT statements.
Improved general readability.
Fixed some minor problems thanks to pychecker.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pkpgcounter/trunk/pkpgpdls/pcl345.py

    r249 r252  
    8484           self.firstblock.startswith("\033%8\033") or \ 
    8585           (self.firstblock.find("\033%-12345X") != -1) : 
    86             if self.debug :   
    87                 sys.stderr.write("DEBUG: Input file is in the PCL3/4/5 format.\n") 
     86            self.logdebug("DEBUG: Input file is in the PCL3/4/5 format.") 
    8887            return 1 
    8988        else :     
     
    9291    def setPageDict(self, pages, number, attribute, value) : 
    9392        """Initializes a page dictionnary.""" 
    94         dict = pages.setdefault(number, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait"}) 
    95         dict[attribute] = value 
     93        dic = pages.setdefault(number, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : []}) 
     94        dic[attribute] = value 
    9695         
    9796    def getJobSize(self) :      
     
    137136        mediasourcecount = mediasizecount = orientationcount = mediatypecount = 0 
    138137        tag = None 
     138        endmark = chr(0x1b) + chr(0x0c) + chr(0x00)  
     139        asciilimit = chr(0x80) 
    139140        pages = {} 
    140141        pos = 0 
     
    145146                    pagecount += 1 
    146147                elif char == "\033" :     
    147                     starb = ampl = 0 
    148                     # 
    149                     #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics 
    150                     #     <ESC>*b###W -> Start of a raster data row/block 
    151                     #     <ESC>*b###V -> Start of a raster data plane 
    152                     #     <ESC>*c###W -> Start of a user defined pattern 
    153                     #     <ESC>*i###W -> Start of a viewing illuminant block 
    154                     #     <ESC>*l###W -> Start of a color lookup table 
    155                     #     <ESC>*m###W -> Start of a download dither matrix block 
    156                     #     <ESC>*v###W -> Start of a configure image data block 
    157                     #     <ESC>*r1A -> Start Gfx  
    158                     #     <ESC>(s###W -> Start of a characters description block 
    159                     #     <ESC>)s###W -> Start of a fonts description block 
    160                     #     <ESC>(f###W -> Start of a symbol set block 
    161                     #     <ESC>&b###W -> Start of configuration data block 
    162                     #     <ESC>&l###X -> Number of copies for current page 
    163                     #     <ESC>&n###W -> Starts an alphanumeric string ID block 
    164                     #     <ESC>&p###X -> Start of a non printable characters block 
    165                     #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 
    166                     #     <ESC>*g###W -> Needed for planes in PCL3 output 
    167                     #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 
    168                     #     <ESC>&l###A -> mediasize 
    169                     #     <ESC>&l###O -> orientation 
    170                     #     <ESC>&l###M -> mediatype 
    171                     #     <ESC>*t###R -> gfx resolution 
    172                     # 
    173                     tagstart = minfile[pos] ; pos += 1 
    174                     if tagstart in "E9=YZ" : # one byte PCL tag 
    175                         if tagstart == "E" : 
    176                             resets += 1 
    177                         continue             # skip to next tag 
    178                     tag = tagstart + minfile[pos] ; pos += 1 
    179                     if tag == "*b" :  
    180                         starb = 1 
    181                         tagend = "VW" 
    182                     elif tag == "&l" :     
    183                         ampl = 1 
    184                         tagend = "XHAOM" 
    185                     else :     
    186                         try : 
    187                             tagend = tagsends[tag] 
    188                         except KeyError :     
    189                             continue # Unsupported PCL tag 
    190                     # Now read the numeric argument 
    191                     size = 0 
    192                     while 1 : 
    193                         char = minfile[pos] ; pos += 1 
    194                         if not char.isdigit() : 
    195                             break 
    196                         size = (size * 10) + int(char)     
    197                     if char in tagend :     
    198                         if tag == "&l" : 
    199                             if char == "X" :  
    200                                 self.setPageDict(pages, pagecount, "copies", size) 
    201                             elif char == "H" : 
    202                                 self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
    203                                 mediasourcecount += 1 
    204                                 ejects += 1  
    205                             elif char == "A" : 
    206                                 self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
    207                                 mediasizecount += 1 
    208                             elif char == "O" : 
    209                                 self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
    210                                 orientationcount += 1 
    211                             elif char == "M" : 
    212                                 self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
    213                                 mediatypecount += 1 
    214                         elif tag == "*r" : 
    215                             # Special tests for PCL3 
    216                             if (char == "s") and size : 
    217                                 while 1 : 
    218                                     char = minfile[pos] ; pos += 1 
    219                                     if char == "A" : 
    220                                         break 
    221                             elif (char == "b") and (minfile[pos] == "C") and not size : 
    222                                 ispcl3 = 1 # Certainely a PCL3 file 
    223                             startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 
    224                             endgfx += (not size) and (char in ("C", "B")) # End Gfx 
    225                         elif tag == "*t" :     
    226                             escstart += 1 
    227                         elif (tag == "&a") and (size == 2) : 
    228                             backsides += 1      # Back side in duplex mode 
     148                    if minfile[pos : pos+8] == r"%-12345X" : 
     149                        endpos = pos + 9 
     150                        while (minfile[endpos] not in endmark) and (minfile[endpos] < asciilimit) : 
     151                            endpos += 1 
     152                        self.setPageDict(pages, pagecount, "escaped", minfile[pos : endpos].replace('\r\n', '\n').split('\n')) 
     153                        self.logdebug("Escaped datas : [%s]" % repr(minfile[pos : endpos])) 
     154                        pos += (endpos - pos) 
     155                    else : 
     156                        starb = ampl = 0 
     157                        # 
     158                        #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics 
     159                        #     <ESC>*b###W -> Start of a raster data row/block 
     160                        #     <ESC>*b###V -> Start of a raster data plane 
     161                        #     <ESC>*c###W -> Start of a user defined pattern 
     162                        #     <ESC>*i###W -> Start of a viewing illuminant block 
     163                        #     <ESC>*l###W -> Start of a color lookup table 
     164                        #     <ESC>*m###W -> Start of a download dither matrix block 
     165                        #     <ESC>*v###W -> Start of a configure image data block 
     166                        #     <ESC>*r1A -> Start Gfx  
     167                        #     <ESC>(s###W -> Start of a characters description block 
     168                        #     <ESC>)s###W -> Start of a fonts description block 
     169                        #     <ESC>(f###W -> Start of a symbol set block 
     170                        #     <ESC>&b###W -> Start of configuration data block 
     171                        #     <ESC>&l###X -> Number of copies for current page 
     172                        #     <ESC>&n###W -> Starts an alphanumeric string ID block 
     173                        #     <ESC>&p###X -> Start of a non printable characters block 
     174                        #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 
     175                        #     <ESC>*g###W -> Needed for planes in PCL3 output 
     176                        #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 
     177                        #     <ESC>&l###A -> mediasize 
     178                        #     <ESC>&l###O -> orientation 
     179                        #     <ESC>&l###M -> mediatype 
     180                        #     <ESC>*t###R -> gfx resolution 
     181                        # 
     182                        tagstart = minfile[pos] ; pos += 1 
     183                        if tagstart in "E9=YZ" : # one byte PCL tag 
     184                            if tagstart == "E" : 
     185                                resets += 1 
     186                            continue             # skip to next tag 
     187                        tag = tagstart + minfile[pos] ; pos += 1 
     188                        if tag == "*b" :  
     189                            starb = 1 
     190                            tagend = "VW" 
     191                        elif tag == "&l" :     
     192                            ampl = 1 
     193                            tagend = "XHAOM" 
    229194                        else :     
    230                             # we just ignore the block. 
    231                             if tag == "&n" :  
    232                                 # we have to take care of the operation id byte 
    233                                 # which is before the string itself 
    234                                 size += 1 
    235                             pos += size     
     195                            try : 
     196                                tagend = tagsends[tag] 
     197                            except KeyError :     
     198                                continue # Unsupported PCL tag 
     199                        # Now read the numeric argument 
     200                        size = 0 
     201                        while 1 : 
     202                            char = minfile[pos] ; pos += 1 
     203                            if not char.isdigit() : 
     204                                break 
     205                            size = (size * 10) + int(char)     
     206                        if char in tagend :     
     207                            if tag == "&l" : 
     208                                if char == "X" :  
     209                                    self.setPageDict(pages, pagecount, "copies", size) 
     210                                elif char == "H" : 
     211                                    self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
     212                                    mediasourcecount += 1 
     213                                    ejects += 1  
     214                                elif char == "A" : 
     215                                    self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
     216                                    mediasizecount += 1 
     217                                elif char == "O" : 
     218                                    self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
     219                                    orientationcount += 1 
     220                                elif char == "M" : 
     221                                    self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
     222                                    mediatypecount += 1 
     223                            elif tag == "*r" : 
     224                                # Special tests for PCL3 
     225                                if (char == "s") and size : 
     226                                    while 1 : 
     227                                        char = minfile[pos] ; pos += 1 
     228                                        if char == "A" : 
     229                                            break 
     230                                elif (char == "b") and (minfile[pos] == "C") and not size : 
     231                                    ispcl3 = 1 # Certainely a PCL3 file 
     232                                startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 
     233                                endgfx += (not size) and (char in ("C", "B")) # End Gfx 
     234                            elif tag == "*t" :     
     235                                escstart += 1 
     236                            elif (tag == "&a") and (size == 2) : 
     237                                backsides += 1      # Back side in duplex mode 
     238                            else :     
     239                                # we just ignore the block. 
     240                                if tag == "&n" :  
     241                                    # we have to take care of the operation id byte 
     242                                    # which is before the string itself 
     243                                    size += 1 
     244                                pos += size     
    236245                else :                             
    237246                    if starb : 
     
    329338            pass        # should be OK. 
    330339        elif (not startgfx) and (not endgfx) : 
    331             pagecount = ejects or pagecount 
     340            if ejects : 
     341                pagecount = ejects 
    332342        elif startgfx == endgfx :     
    333343            pagecount = startgfx 
     
    339349            pagecount = abs(startgfx - endgfx) 
    340350             
    341         if self.debug :         
    342             for pnum in range(pagecount) : 
    343                 # if no number of copies defined, take the preceding one else the one set before any page else 1. 
    344                 page = pages.get(pnum, pages.get(pnum - 1, pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait"}))) 
    345                 sys.stderr.write("%s*%s*%s*%s*%s\n" % (page["copies"], page["mediatype"], page["mediasize"], page["orientation"], page["mediasource"])) 
     351        pjlstatements = [] 
     352        for pnum in range(pagecount) : 
     353            # if no number of copies defined, take the preceding one else the one set before any page else 1. 
     354            page = pages.get(pnum, pages.get(pnum - 1, pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : []}))) 
     355            pjlstuff = page["escaped"] 
     356            if not pjlstuff : 
     357                pjlcopies = 1 
     358            else :     
     359                pjlstatements.extend(pjlstuff) 
     360                newpjl = "\n".join(pjlstatements) 
     361                copiesstatement = newpjl.rfind("@PJL SET COPIES=") 
     362                qtystatement = newpjl.rfind("@PJL SET QTY=") 
     363                if copiesstatement > qtystatement : 
     364                    # we use the COPIES= statement 
     365                    try : 
     366                        pjlcopies = int(newpjl[copiesstatement+16:].split()[0].strip()) 
     367                    except :     
     368                        pjlcopies = 1 
     369                elif qtystatement > copiesstatement :     
     370                    # we use the QTY= statement 
     371                    try : 
     372                        pjlcopies = int(newpjl[qtystatement+13:].split()[0].strip()) 
     373                    except :     
     374                        pjlcopies = 1 
     375                else : 
     376                    # both can't be equal unless they both equal -1 (not found) 
     377                    pjlcopies = 1 
     378            copies = pjlcopies * page["copies"]         
     379            pagecount += (copies - 1) 
     380            self.logdebug("%s*%s*%s*%s*%s" % (copies, \ 
     381                                              page["mediatype"], \ 
     382                                              page["mediasize"], \ 
     383                                              page["orientation"], \ 
     384                                              page["mediasource"])) 
    346385                 
    347386        return pagecount