Changeset 307 for pkpgcounter

Show
Ignore:
Timestamp:
02/03/06 18:37:53 (18 years ago)
Author:
jerome
Message:

Ensure that the input file is closed in all cases.
Skip faulty tags (due to unavailable PCLXL Class 3.0 specification) in PCLXL parser

Location:
pkpgcounter/trunk/pkpgpdls
Files:
3 modified

Legend:

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

    r305 r307  
    148148        pos = 0 
    149149        try : 
    150             while 1 : 
    151                 if hasirmarker and (minfile[pos:pos+2] == irmarker) : 
    152                     codop = minfile[pos+2:pos+4] 
    153                     # self.logdebug("Marker at 0x%08x     (%s)" % (pos, wasirmarker)) 
    154                     length = unpack(">H", minfile[pos+8:pos+10])[0] 
    155                     pos += 20 
    156                     if codop != irmarker2 : 
    157                         pos += length 
    158                     wasirmarker = 1     
    159                 else :         
    160                     wasirmarker = 0 
    161                     char = minfile[pos] ; pos += 1 
    162                     if char == "\014" :     
    163                         pagecount += 1 
    164                     elif char == "\033" :     
    165                         starb = ampl = 0 
    166                         if minfile[pos : pos+8] == r"%-12345X" : 
    167                             endpos = pos + 9 
    168                             quotes = 0 
    169                             while (minfile[endpos] not in endmark) and \ 
    170                                   ((minfile[endpos] < asciilimit) or (quotes % 2)) : 
    171                                 if minfile[endpos] == '"' : 
    172                                     quotes += 1 
    173                                 endpos += 1 
    174                             self.setPageDict(pages, pagecount, "escaped", minfile[pos : endpos]) 
    175                             pos += (endpos - pos) 
    176                         else : 
    177                             # 
    178                             #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics 
    179                             #     <ESC>*b###W -> Start of a raster data row/block 
    180                             #     <ESC>*b###V -> Start of a raster data plane 
    181                             #     <ESC>*c###W -> Start of a user defined pattern 
    182                             #     <ESC>*i###W -> Start of a viewing illuminant block 
    183                             #     <ESC>*l###W -> Start of a color lookup table 
    184                             #     <ESC>*m###W -> Start of a download dither matrix block 
    185                             #     <ESC>*v###W -> Start of a configure image data block 
    186                             #     <ESC>*r1A -> Start Gfx  
    187                             #     <ESC>(s###W -> Start of a characters description block 
    188                             #     <ESC>)s###W -> Start of a fonts description block 
    189                             #     <ESC>(f###W -> Start of a symbol set block 
    190                             #     <ESC>&b###W -> Start of configuration data block 
    191                             #     <ESC>&l###X -> Number of copies for current page 
    192                             #     <ESC>&n###W -> Starts an alphanumeric string ID block 
    193                             #     <ESC>&p###X -> Start of a non printable characters block 
    194                             #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 
    195                             #     <ESC>*g###W -> Needed for planes in PCL3 output 
    196                             #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 
    197                             #     <ESC>&l###A -> mediasize 
    198                             #     <ESC>&l###O -> orientation 
    199                             #     <ESC>&l###M -> mediatype 
    200                             #     <ESC>*t###R -> gfx resolution 
    201                             # 
    202                             tagstart = minfile[pos] ; pos += 1 
    203                             if tagstart in "E9=YZ" : # one byte PCL tag 
    204                                 if tagstart == "E" : 
    205                                     resets += 1 
    206                                 continue             # skip to next tag 
    207                             tag = tagstart + minfile[pos] ; pos += 1 
    208                             if tag == "*b" :  
    209                                 starb = 1 
    210                                 tagend = "VW" 
    211                             elif tag == "&l" :     
    212                                 ampl = 1 
    213                                 tagend = "XHAOM" 
    214                             else :     
    215                                 try : 
    216                                     tagend = tagsends[tag] 
    217                                 except KeyError :     
    218                                     continue # Unsupported PCL tag 
    219                             # Now read the numeric argument 
    220                             size = 0 
    221                             while 1 : 
    222                                 char = minfile[pos] ; pos += 1 
    223                                 if not char.isdigit() : 
    224                                     break 
    225                                 size = (size * 10) + int(char)     
    226                             if char in tagend :     
    227                                 if tag == "&l" : 
    228                                     if char == "X" :  
    229                                         self.setPageDict(pages, pagecount, "copies", size) 
    230                                     elif char == "H" : 
     150            try : 
     151                while 1 : 
     152                    if hasirmarker and (minfile[pos:pos+2] == irmarker) : 
     153                        codop = minfile[pos+2:pos+4] 
     154                        # self.logdebug("Marker at 0x%08x     (%s)" % (pos, wasirmarker)) 
     155                        length = unpack(">H", minfile[pos+8:pos+10])[0] 
     156                        pos += 20 
     157                        if codop != irmarker2 : 
     158                            pos += length 
     159                        wasirmarker = 1     
     160                    else :         
     161                        wasirmarker = 0 
     162                        char = minfile[pos] ; pos += 1 
     163                        if char == "\014" :     
     164                            pagecount += 1 
     165                        elif char == "\033" :     
     166                            starb = ampl = 0 
     167                            if minfile[pos : pos+8] == r"%-12345X" : 
     168                                endpos = pos + 9 
     169                                quotes = 0 
     170                                while (minfile[endpos] not in endmark) and \ 
     171                                      ((minfile[endpos] < asciilimit) or (quotes % 2)) : 
     172                                    if minfile[endpos] == '"' : 
     173                                        quotes += 1 
     174                                    endpos += 1 
     175                                self.setPageDict(pages, pagecount, "escaped", minfile[pos : endpos]) 
     176                                pos += (endpos - pos) 
     177                            else : 
     178                                # 
     179                                #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics 
     180                                #     <ESC>*b###W -> Start of a raster data row/block 
     181                                #     <ESC>*b###V -> Start of a raster data plane 
     182                                #     <ESC>*c###W -> Start of a user defined pattern 
     183                                #     <ESC>*i###W -> Start of a viewing illuminant block 
     184                                #     <ESC>*l###W -> Start of a color lookup table 
     185                                #     <ESC>*m###W -> Start of a download dither matrix block 
     186                                #     <ESC>*v###W -> Start of a configure image data block 
     187                                #     <ESC>*r1A -> Start Gfx  
     188                                #     <ESC>(s###W -> Start of a characters description block 
     189                                #     <ESC>)s###W -> Start of a fonts description block 
     190                                #     <ESC>(f###W -> Start of a symbol set block 
     191                                #     <ESC>&b###W -> Start of configuration data block 
     192                                #     <ESC>&l###X -> Number of copies for current page 
     193                                #     <ESC>&n###W -> Starts an alphanumeric string ID block 
     194                                #     <ESC>&p###X -> Start of a non printable characters block 
     195                                #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 
     196                                #     <ESC>*g###W -> Needed for planes in PCL3 output 
     197                                #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 
     198                                #     <ESC>&l###A -> mediasize 
     199                                #     <ESC>&l###O -> orientation 
     200                                #     <ESC>&l###M -> mediatype 
     201                                #     <ESC>*t###R -> gfx resolution 
     202                                # 
     203                                tagstart = minfile[pos] ; pos += 1 
     204                                if tagstart in "E9=YZ" : # one byte PCL tag 
     205                                    if tagstart == "E" : 
     206                                        resets += 1 
     207                                    continue             # skip to next tag 
     208                                tag = tagstart + minfile[pos] ; pos += 1 
     209                                if tag == "*b" :  
     210                                    starb = 1 
     211                                    tagend = "VW" 
     212                                elif tag == "&l" :     
     213                                    ampl = 1 
     214                                    tagend = "XHAOM" 
     215                                else :     
     216                                    try : 
     217                                        tagend = tagsends[tag] 
     218                                    except KeyError :     
     219                                        continue # Unsupported PCL tag 
     220                                # Now read the numeric argument 
     221                                size = 0 
     222                                while 1 : 
     223                                    char = minfile[pos] ; pos += 1 
     224                                    if not char.isdigit() : 
     225                                        break 
     226                                    size = (size * 10) + int(char)     
     227                                if char in tagend :     
     228                                    if tag == "&l" : 
     229                                        if char == "X" :  
     230                                            self.setPageDict(pages, pagecount, "copies", size) 
     231                                        elif char == "H" : 
     232                                            self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
     233                                            mediasourcecount += 1 
     234                                            ejects += 1  
     235                                        elif char == "A" : 
     236                                            self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
     237                                            mediasizecount += 1 
     238                                        elif char == "O" : 
     239                                            self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
     240                                            orientationcount += 1 
     241                                        elif char == "M" : 
     242                                            self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
     243                                            mediatypecount += 1 
     244                                    elif tag == "*r" : 
     245                                        # Special tests for PCL3 
     246                                        if (char == "s") and size : 
     247                                            while 1 : 
     248                                                char = minfile[pos] ; pos += 1 
     249                                                if char == "A" : 
     250                                                    break 
     251                                        elif (char == "b") and (minfile[pos] == "C") and not size : 
     252                                            ispcl3 = 1 # Certainely a PCL3 file 
     253                                        startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 
     254                                        endgfx += (not size) and (char in ("C", "B")) # End Gfx 
     255                                    elif tag == "*t" :     
     256                                        escstart += 1 
     257                                    elif (tag == "&a") and (size == 2) : 
     258                                        # We are on the backside, so mark current page as duplex 
     259                                        self.setPageDict(pages, pagecount, "duplex", 1) 
     260                                        backsides += 1      # Back side in duplex mode 
     261                                    else :     
     262                                        # we just ignore the block. 
     263                                        if tag == "&n" :  
     264                                            # we have to take care of the operation id byte 
     265                                            # which is before the string itself 
     266                                            size += 1 
     267                                        pos += size     
     268                        else :                             
     269                            if starb : 
     270                                # special handling of PCL3 in which  
     271                                # *b introduces combined ESCape sequences 
     272                                size = 0 
     273                                while 1 : 
     274                                    char = minfile[pos] ; pos += 1 
     275                                    if not char.isdigit() : 
     276                                        break 
     277                                    size = (size * 10) + int(char)     
     278                                if char in ("w", "v") :     
     279                                    ispcl3 = 1  # certainely a PCL3 document 
     280                                    pos += size - 1 
     281                                elif char in ("y", "m") :     
     282                                    ispcl3 = 1  # certainely a PCL3 document 
     283                                    pos -= 1    # fix position : we were ahead 
     284                            elif ampl :         
     285                                # special handling of PCL3 in which  
     286                                # &l introduces combined ESCape sequences 
     287                                size = 0 
     288                                while 1 : 
     289                                    char = minfile[pos] ; pos += 1 
     290                                    if not char.isdigit() : 
     291                                        break 
     292                                    size = (size * 10) + int(char)     
     293                                if char in ("a", "o", "h", "m") :     
     294                                    ispcl3 = 1  # certainely a PCL3 document 
     295                                    pos -= 1    # fix position : we were ahead 
     296                                    if char == "h" : 
    231297                                        self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
    232298                                        mediasourcecount += 1 
    233                                         ejects += 1  
    234                                     elif char == "A" : 
     299                                    elif char == "a" : 
    235300                                        self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
    236301                                        mediasizecount += 1 
    237                                     elif char == "O" : 
     302                                    elif char == "o" : 
    238303                                        self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
    239304                                        orientationcount += 1 
    240                                     elif char == "M" : 
     305                                    elif char == "m" : 
    241306                                        self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
    242307                                        mediatypecount += 1 
    243                                 elif tag == "*r" : 
    244                                     # Special tests for PCL3 
    245                                     if (char == "s") and size : 
    246                                         while 1 : 
    247                                             char = minfile[pos] ; pos += 1 
    248                                             if char == "A" : 
    249                                                 break 
    250                                     elif (char == "b") and (minfile[pos] == "C") and not size : 
    251                                         ispcl3 = 1 # Certainely a PCL3 file 
    252                                     startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 
    253                                     endgfx += (not size) and (char in ("C", "B")) # End Gfx 
    254                                 elif tag == "*t" :     
    255                                     escstart += 1 
    256                                 elif (tag == "&a") and (size == 2) : 
    257                                     # We are on the backside, so mark current page as duplex 
    258                                     self.setPageDict(pages, pagecount, "duplex", 1) 
    259                                     backsides += 1      # Back side in duplex mode 
    260                                 else :     
    261                                     # we just ignore the block. 
    262                                     if tag == "&n" :  
    263                                         # we have to take care of the operation id byte 
    264                                         # which is before the string itself 
    265                                         size += 1 
    266                                     pos += size     
    267                     else :                             
    268                         if starb : 
    269                             # special handling of PCL3 in which  
    270                             # *b introduces combined ESCape sequences 
    271                             size = 0 
    272                             while 1 : 
    273                                 char = minfile[pos] ; pos += 1 
    274                                 if not char.isdigit() : 
    275                                     break 
    276                                 size = (size * 10) + int(char)     
    277                             if char in ("w", "v") :     
    278                                 ispcl3 = 1  # certainely a PCL3 document 
    279                                 pos += size - 1 
    280                             elif char in ("y", "m") :     
    281                                 ispcl3 = 1  # certainely a PCL3 document 
    282                                 pos -= 1    # fix position : we were ahead 
    283                         elif ampl :         
    284                             # special handling of PCL3 in which  
    285                             # &l introduces combined ESCape sequences 
    286                             size = 0 
    287                             while 1 : 
    288                                 char = minfile[pos] ; pos += 1 
    289                                 if not char.isdigit() : 
    290                                     break 
    291                                 size = (size * 10) + int(char)     
    292                             if char in ("a", "o", "h", "m") :     
    293                                 ispcl3 = 1  # certainely a PCL3 document 
    294                                 pos -= 1    # fix position : we were ahead 
    295                                 if char == "h" : 
    296                                     self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
    297                                     mediasourcecount += 1 
    298                                 elif char == "a" : 
    299                                     self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
    300                                     mediasizecount += 1 
    301                                 elif char == "o" : 
    302                                     self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
    303                                     orientationcount += 1 
    304                                 elif char == "m" : 
    305                                     self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
    306                                     mediatypecount += 1 
    307         except IndexError : # EOF ? 
    308             minfile.close() # reached EOF 
     308            except IndexError : # EOF ? 
     309                pass 
     310        finally : 
     311            minfile.close() 
    309312                             
    310313        # if pagecount is still 0, we will use the number 
  • pkpgcounter/trunk/pkpgpdls/pclxl.py

    r302 r307  
    289289    def reservedForFutureUse(self) : 
    290290        """Outputs something when a reserved byte is encountered.""" 
    291         self.logdebug("Byte at %s is out of the PCLXL Protocol Class 2.0 Specification" % self.pos) 
     291        self.logdebug("Byte at %x is out of the PCLXL Protocol Class 2.0 Specification" % self.pos) 
    292292        return 0     
    293293         
     
    295295        """Passthrough mode, as detailed in PCLXL Feature Reference Protocol Class 3.0 Supplement.""" 
    296296        # TODO : do something here to skip the block. 
    297         self.logdebug("PassThrough marker detected at %s" % self.pos) 
     297        self.logdebug("PassThrough marker detected at %x" % self.pos) 
    298298        return 0 
    299299         
     
    529529        self.pagecount = 0 
    530530        self.escapedStuff = {} 
    531         self.pos = pos = 0 
     531        self.pos = pos = oldpos = 0 
    532532        try : 
    533             while 1 : 
    534                 char = minfile[pos] 
    535                 pos += 1 
    536                 length = tags[ord(char)] 
    537                 if not length : 
    538                     continue 
    539                 if callable(length) :     
    540                     self.pos = pos 
    541                     length = length() 
    542                     pos = self.pos 
    543                 pos += length     
    544         except IndexError : # EOF ? 
    545             self.minfile.close() # reached EOF 
     533            try : 
     534                while 1 : 
     535                    try : 
     536                        char = minfile[pos] 
     537                    except OverflowError :     
     538                        pos = oldpos + 1 
     539                    pos += 1 
     540                    length = tags[ord(char)] 
     541                    if length : 
     542                        if callable(length) :     
     543                            self.pos = pos 
     544                            length = length() 
     545                            pos = self.pos 
     546                        oldpos = pos     
     547                        pos += length     
     548            except IndexError : # EOF ?             
     549                pass 
     550        finally : 
     551            self.minfile.close() 
    546552             
    547553        # now handle number of copies for each page (may differ). 
  • pkpgcounter/trunk/pkpgpdls/version.py

    r305 r307  
    2020# 
    2121 
    22 __version__ = "1.75" 
     22__version__ = "1.76beta" 
    2323 
    2424__doc__ = """pkpgcounter : a generic Page Description Languages parser."""