Changeset 395

Show
Ignore:
Timestamp:
09/08/06 11:16:49 (18 years ago)
Author:
jerome
Message:

Improved the new pcl345 parser. It correctly parses the testsuite, but doesn't
work with some other documents someone sent to me...

Files:
1 modified

Legend:

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

    r394 r395  
    107107        """Reads a byte from the input stream.""" 
    108108        tag = ord(self.minfile[self.pos]) 
     109        #self.logdebug("%08x  ===>  %02x" % (self.pos, tag)) 
    109110        self.pos += 1 
    110         self.logdebug("BYTE %02x" % tag) 
    111111        return tag 
    112112         
     
    128128    def escape(self) :     
    129129        """Handles the ESC character.""" 
    130         self.logdebug("ESCAPE") 
     130        #self.logdebug("ESCAPE") 
    131131        self.handleTag(self.esctags) 
    132132         
    133133    def escAmp(self) :     
    134134        """Handles the ESC& sequence.""" 
    135         self.logdebug("AMP") 
     135        #self.logdebug("AMP") 
    136136        self.handleTag(self.escamptags) 
    137137         
    138138    def escStar(self) :     
    139139        """Handles the ESC* sequence.""" 
    140         self.logdebug("STAR") 
     140        #self.logdebug("STAR") 
    141141        self.handleTag(self.escstartags) 
    142142         
    143143    def escLeftPar(self) :     
    144144        """Handles the ESC( sequence.""" 
    145         self.logdebug("LEFTPAR") 
     145        #self.logdebug("LEFTPAR") 
    146146        self.handleTag(self.escleftpartags) 
    147147         
    148148    def escRightPar(self) :     
    149149        """Handles the ESC( sequence.""" 
    150         self.logdebug("RIGHTPAR") 
     150        #self.logdebug("RIGHTPAR") 
    151151        self.handleTag(self.escrightpartags) 
    152152         
    153153    def escE(self) :     
    154154        """Handles the ESCE sequence.""" 
    155         self.logdebug("RESET") 
     155        #self.logdebug("RESET") 
    156156        self.resets += 1 
    157157         
    158158    def escAmpl(self) :     
    159159        """Handles the ESC&l sequence.""" 
    160         self.logdebug("l") 
    161160        while 1 : 
    162161            (value, end) = self.getInteger() 
     
    182181                self.copies.append(value) 
    183182                self.logdebug("COPIES %i" % value) 
    184             elif end == 'L' :     
    185                 self.logdebug("ESC&l%iL" % value) 
     183                 
     184    def escAmpa(self) :     
     185        """Handles the ESC&a sequence.""" 
     186        while 1 : 
     187            (value, end) = self.getInteger() 
     188            if value is None : 
     189                return 
     190            if end == 'G' :     
     191                self.logdebug("BACKSIDES %i" % value) 
     192                self.backsides.append(value) 
     193                 
     194    def escAmpb(self) :     
     195        """Handles the ESC&b sequence.""" 
     196        while 1 : 
     197            (value, end) = self.getInteger() 
     198            if value is None : 
     199                return 
     200            if end == 'W' :     
     201                self.pos += value 
     202                #self.logdebug("SKIPTO %08x" % self.pos) 
     203                 
     204    def escAmpn(self) :     
     205        """Handles the ESC&n sequence.""" 
     206        while 1 : 
     207            (value, end) = self.getInteger() 
     208            if value is None : 
     209                return 
     210            if end == 'W' :     
     211                self.pos += value 
     212                #self.logdebug("SKIPTO %08x" % self.pos) 
     213                 
     214    def escAmpp(self) :     
     215        """Handles the ESC&p sequence.""" 
     216        while 1 : 
     217            (value, end) = self.getInteger() 
     218            if value is None : 
     219                return 
     220            if end == 'X' :     
     221                self.pos += value 
     222                #self.logdebug("SKIPTO %08x" % self.pos) 
     223                 
     224    def escAmpu(self) :     
     225        """Handles the ESC&u sequence.""" 
     226        while 1 : 
     227            (value, end) = self.getInteger() 
     228            if value is None : 
     229                return 
    186230                 
    187231    def escStarb(self) :     
    188232        """Handles the ESC*b sequence.""" 
    189         self.logdebug("b") 
    190         while 1 : 
    191             (value, end) = self.getInteger() 
    192             self.logdebug("%s === %s" % (value, end)) 
     233        while 1 : 
     234            (value, end) = self.getInteger() 
    193235            if (end is None) and (value is None) : 
    194236                return 
    195237            if end in ('V', 'W', 'v', 'w') :     
    196238                self.pos += (value or 0) 
    197                 self.logdebug("SKIPTO %08x" % self.pos) 
     239                #self.logdebug("SKIPTO %08x" % self.pos) 
     240                 
     241    def escStarcgilmv(self) :     
     242        """Handles the ESC*c, ESC*g, ESC*i, ESC*l, ESC*m, ESC*v sequences.""" 
     243        while 1 : 
     244            (value, end) = self.getInteger() 
     245            if value is None : 
     246                return 
     247            if end == 'W' :     
     248                self.pos += value 
     249                #self.logdebug("SKIPTO %08x" % self.pos) 
    198250                 
    199251    def escStaro(self) :     
    200252        """Handles the ESC*o sequence.""" 
    201         self.logdebug("o") 
    202         while 1 : 
    203             (value, end) = self.getInteger() 
    204             if value is None : 
    205                 return 
    206             if end == 'M' : 
    207                 self.logdebug("ESC*o%iM" % value) 
     253        while 1 : 
     254            (value, end) = self.getInteger() 
     255            if value is None : 
     256                return 
    208257                 
    209258    def escStarp(self) :     
    210259        """Handles the ESC*p sequence.""" 
    211         self.logdebug("p") 
    212         while 1 : 
    213             (value, end) = self.getInteger() 
    214             if value is None : 
    215                 return 
    216             if end in ('X', 'Y') :     
    217                 self.logdebug("ESC*p%i%s" % (value, end)) 
     260        while 1 : 
     261            (value, end) = self.getInteger() 
     262            if value is None : 
     263                return 
    218264                 
    219265    def escStarr(self) :     
    220266        """Handles the ESC*r sequence.""" 
    221         self.logdebug("r") 
    222267        while 1 : 
    223268            (value, end) = self.getInteger() 
     
    225270                if end is None : 
    226271                    return 
    227                 elif end == 'b' : 
    228                     if self.minfile[self.pos] == 'C' : 
    229                         self.logdebug("Looks like it's PCL3.") 
    230                         self.ispcl3 = True 
    231                         self.pos += 1 
    232                 elif end == 'C' :         
    233                     self.logdebug("EndGFX") 
     272                elif end in ('B', 'C') :         
     273                    #self.logdebug("EndGFX") 
    234274                    if not self.startgfx : 
    235275                        self.logdebug("EndGFX found before StartGFX, ignored.") 
     
    237277                        self.endgfx.append(1) 
    238278            if end == 'A' and (0 <= value <= 3) : 
    239                 self.logdebug("StartGFX %i" % value) 
     279                #self.logdebug("StartGFX %i" % value) 
    240280                self.startgfx.append(value) 
    241             elif end == 'U' :     
    242                 self.logdebug("ESC*r%iU" % value) 
    243             elif end == 'S' :     
    244                 self.logdebug("ESC*r%iS" % value) 
    245281                 
    246282    def escStart(self) :     
    247283        """Handles the ESC*t sequence.""" 
    248         self.logdebug("t") 
    249         while 1 : 
    250             (value, end) = self.getInteger() 
    251             if value is None : 
    252                 return 
    253             if end == 'R' :     
    254                 self.logdebug("ESC*t%iR" % value) 
    255                  
    256     def escAmpu(self) :     
    257         """Handles the ESC&u sequence.""" 
    258         self.logdebug("u") 
    259         while 1 : 
    260             (value, end) = self.getInteger() 
    261             if value is None : 
    262                 return 
    263             if end == 'D' :     
    264                 self.logdebug("ESC&u%iD" % value) 
    265                  
    266          
     284        while 1 : 
     285            (value, end) = self.getInteger() 
     286            if value is None : 
     287                return 
     288         
     289    def escRightorLeftParsf(self) :     
     290        """Handles the ESC(s, ESC)s, ESC(f sequences.""" 
     291        while 1 : 
     292            (value, end) = self.getInteger() 
     293            if value is None : 
     294                return 
     295            if end == 'W' :     
     296                self.pos += value 
     297                #self.logdebug("SKIPTO %08x" % self.pos) 
     298                 
    267299    def getInteger(self) :     
    268300        """Returns an integer value and the end character.""" 
     
    271303        while 1 : 
    272304            char = chr(self.readByte()) 
    273             if char == ESCAPE : 
     305            if char in (ESCAPE, FORMFEED) : 
    274306                self.pos -= 1 # Adjust position 
    275307                return (None, None) 
     
    304336        infileno = self.infile.fileno() 
    305337        self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) 
    306         self.ispcl3 = False 
    307338        self.pagecount = 0 
    308339        self.resets = 0 
     340        self.backsides = [] 
    309341        self.copies = [] 
    310342        self.mediasourcesvalues = [] 
     
    328360         
    329361        self.escamptags = [lambda : None ] * 256 
     362        self.escamptags[ord('a')] = self.escAmpa 
     363        self.escamptags[ord('b')] = self.escAmpb 
    330364        self.escamptags[ord('l')] = self.escAmpl 
     365        self.escamptags[ord('n')] = self.escAmpn 
     366        self.escamptags[ord('p')] = self.escAmpp 
    331367        self.escamptags[ord('u')] = self.escAmpu 
    332368         
     
    337373        self.escstartags[ord('r')] = self.escStarr 
    338374        self.escstartags[ord('t')] = self.escStart 
     375        self.escstartags[ord('c')] = self.escStarcgilmv 
     376        self.escstartags[ord('g')] = self.escStarcgilmv 
     377        self.escstartags[ord('i')] = self.escStarcgilmv 
     378        self.escstartags[ord('l')] = self.escStarcgilmv 
     379        self.escstartags[ord('m')] = self.escStarcgilmv 
     380        self.escstartags[ord('v')] = self.escStarcgilmv 
     381         
     382        self.escleftpartags = [ lambda : None ] * 256 
     383        self.escleftpartags[ord('s')] = self.escRightorLeftParsf 
     384        self.escleftpartags[ord('f')] = self.escRightorLeftParsf 
     385         
     386        self.escrightpartags = [ lambda : None ] * 256 
     387        self.escrightpartags[ord('s')] = self.escRightorLeftParsf 
    339388         
    340389        self.pos = 0 
     
    342391            try : 
    343392                while 1 : 
    344                     tag = self.readByte() 
    345                     self.logdebug("%08x ===> %02x" % (self.pos-1, tag)) 
    346                     tags[tag]() 
     393                    tags[self.readByte()]() 
    347394            except IndexError : # EOF ?             
    348395                pass 
     
    350397            self.minfile.close() 
    351398         
    352             self.logdebug("Pagecount : \t\t%i" % self.pagecount) 
    353             self.logdebug("Resets : \t\t%i" % self.resets) 
    354             self.logdebug("Copies : \t\t%s" % self.copies) 
    355             self.logdebug("MediaTypes : \t\t%s" % self.mediatypesvalues) 
    356             self.logdebug("MediaSizes : \t\t%s" % self.mediasizesvalues) 
    357             self.logdebug("MediaSources : \t\t%s" % self.mediasourcesvalues) 
    358             self.logdebug("Orientations : \t\t%s" % self.orientationsvalues) 
    359             self.logdebug("StartGfx : \t\t%s" % len(self.startgfx)) 
    360             self.logdebug("EndGfx : \t\t%s" % len(self.endgfx)) 
    361          
    362         return self.pagecount 
    363          
    364          
    365          
    366         """ 
    367         tagsends = { "&n" : "W",  
    368                      "&b" : "W",  
    369                      "*i" : "W",  
    370                      "*l" : "W",  
    371                      "*m" : "W",  
    372                      "*v" : "W",  
    373                      "*c" : "W",  
    374                      "(f" : "W",  
    375                      "(s" : "W",  
    376                      ")s" : "W",  
    377                      "&p" : "X",  
    378                      # "&l" : "XHAOM",  # treated specially 
    379                      "&a" : "G", # TODO : 0 means next side, 1 front side, 2 back side 
    380                      "*g" : "W", 
    381                      "*r" : "sbABC", 
    382                      "*t" : "R", 
    383                      # "*b" : "VW", # treated specially because it occurs very often 
    384                    }   
    385         irmarker = chr(0xcd) + chr(0xca) # Marker for Canon ImageRunner printers 
    386         irmarker2 = chr(0x10) + chr(0x02) 
    387         wasirmarker = 0 
    388         hasirmarker = (minfile[:2] == (irmarker)) 
    389         pagecount = resets = ejects = backsides = startgfx = endgfx = 0 
    390         starb = ampl = ispcl3 = escstart = 0 
    391         mediasourcecount = mediasizecount = orientationcount = mediatypecount = 0 
    392         tag = None 
    393         endmark = chr(0x1b) + chr(0x0c) + chr(0x00)  
    394         asciilimit = chr(0x80) 
    395         pages = {} 
    396         pos = 0 
    397         try : 
    398             try : 
    399                 while 1 : 
    400                     if hasirmarker and (minfile[pos:pos+2] == irmarker) : 
    401                         codop = minfile[pos+2:pos+4] 
    402                         # self.logdebug("Marker at 0x%08x     (%s)" % (pos, wasirmarker)) 
    403                         length = unpack(">H", minfile[pos+8:pos+10])[0] 
    404                         pos += 20 
    405                         if codop != irmarker2 : 
    406                             pos += length 
    407                         wasirmarker = 1     
    408                     else :         
    409                         wasirmarker = 0 
    410                         elif char == "\033" :     
    411                             starb = ampl = 0 
    412                             if minfile[pos : pos+8] == r"%-12345X" : 
    413                                 endpos = pos + 9 
    414                                 quotes = 0 
    415                                 while (minfile[endpos] not in endmark) and \ 
    416                                       ((minfile[endpos] < asciilimit) or (quotes % 2)) : 
    417                                     if minfile[endpos] == '"' : 
    418                                         quotes += 1 
    419                                     endpos += 1 
    420                                 self.setPageDict(pages, pagecount, "escaped", minfile[pos : endpos]) 
    421                                 pos += (endpos - pos) 
    422                             else : 
    423                                 # 
    424                                 #     <ESC>*b###y#m###v###w... -> PCL3 raster graphics 
    425                                 #     <ESC>*b###W -> Start of a raster data row/block 
    426                                 #     <ESC>*b###V -> Start of a raster data plane 
    427                                 #     <ESC>*c###W -> Start of a user defined pattern 
    428                                 #     <ESC>*i###W -> Start of a viewing illuminant block 
    429                                 #     <ESC>*l###W -> Start of a color lookup table 
    430                                 #     <ESC>*m###W -> Start of a download dither matrix block 
    431                                 #     <ESC>*v###W -> Start of a configure image data block 
    432                                 #     <ESC>*r1A -> Start Gfx  
    433                                 #     <ESC>(s###W -> Start of a characters description block 
    434                                 #     <ESC>)s###W -> Start of a fonts description block 
    435                                 #     <ESC>(f###W -> Start of a symbol set block 
    436                                 #     <ESC>&b###W -> Start of configuration data block 
    437                                 #     <ESC>&l###X -> Number of copies for current page 
    438                                 #     <ESC>&n###W -> Starts an alphanumeric string ID block 
    439                                 #     <ESC>&p###X -> Start of a non printable characters block 
    440                                 #     <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 
    441                                 #     <ESC>*g###W -> Needed for planes in PCL3 output 
    442                                 #     <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 
    443                                 #     <ESC>&l###A -> mediasize 
    444                                 #     <ESC>&l###O -> orientation 
    445                                 #     <ESC>&l###M -> mediatype 
    446                                 #     <ESC>*t###R -> gfx resolution 
    447                                 # 
    448                                 tagstart = minfile[pos] ; pos += 1 
    449                                 if tagstart in "E9=YZ" : # one byte PCL tag 
    450                                     if tagstart == "E" : 
    451                                         resets += 1 
    452                                     continue             # skip to next tag 
    453                                 tag = tagstart + minfile[pos] ; pos += 1 
    454                                 if tag == "*b" :  
    455                                     starb = 1 
    456                                     tagend = "VW" 
    457                                 elif tag == "&l" :     
    458                                     ampl = 1 
    459                                     tagend = "XHAOM" 
    460                                 else :     
    461                                     try : 
    462                                         tagend = tagsends[tag] 
    463                                     except KeyError :     
    464                                         continue # Unsupported PCL tag 
    465                                 # Now read the numeric argument 
    466                                 size = 0 
    467                                 while 1 : 
    468                                     char = minfile[pos] ; pos += 1 
    469                                     if not char.isdigit() : 
    470                                         break 
    471                                     size = (size * 10) + int(char)     
    472                                 if char in tagend :     
    473                                     if tag == "&l" : 
    474                                         if char == "X" :  
    475                                             self.setPageDict(pages, pagecount, "copies", size) 
    476                                         elif char == "H" : 
    477                                             self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
    478                                             mediasourcecount += 1 
    479                                             ejects += 1  
    480                                         elif char == "A" : 
    481                                             self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
    482                                             mediasizecount += 1 
    483                                         elif char == "O" : 
    484                                             self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
    485                                             orientationcount += 1 
    486                                         elif char == "M" : 
    487                                             self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
    488                                             mediatypecount += 1 
    489                                     elif tag == "*r" : 
    490                                         # Special tests for PCL3 
    491                                         if (char == "s") and size : 
    492                                             while 1 : 
    493                                                 char = minfile[pos] ; pos += 1 
    494                                                 if char == "A" : 
    495                                                     break 
    496                                         elif (char == "b") and (minfile[pos] == "C") and not size : 
    497                                             ispcl3 = 1 # Certainely a PCL3 file 
    498                                         startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 
    499                                         endgfx += (not size) and (char in ("C", "B")) # End Gfx 
    500                                     elif tag == "*t" :     
    501                                         escstart += 1 
    502                                     elif (tag == "&a") and (size == 2) : 
    503                                         # We are on the backside, so mark current page as duplex 
    504                                         self.setPageDict(pages, pagecount, "duplex", 1) 
    505                                         backsides += 1      # Back side in duplex mode 
    506                                     else :     
    507                                         # we just ignore the block. 
    508                                         if tag == "&n" :  
    509                                             # we have to take care of the operation id byte 
    510                                             # which is before the string itself 
    511                                             size += 1 
    512                                         pos += size     
    513                         else :                             
    514                             if starb : 
    515                                 # special handling of PCL3 in which  
    516                                 # *b introduces combined ESCape sequences 
    517                                 size = 0 
    518                                 while 1 : 
    519                                     char = minfile[pos] ; pos += 1 
    520                                     if not char.isdigit() : 
    521                                         break 
    522                                     size = (size * 10) + int(char)     
    523                                 if char in ("w", "v") :     
    524                                     ispcl3 = 1  # certainely a PCL3 document 
    525                                     pos += size - 1 
    526                                 elif char in ("y", "m") :     
    527                                     ispcl3 = 1  # certainely a PCL3 document 
    528                                     pos -= 1    # fix position : we were ahead 
    529                             elif ampl :         
    530                                 # special handling of PCL3 in which  
    531                                 # &l introduces combined ESCape sequences 
    532                                 size = 0 
    533                                 while 1 : 
    534                                     char = minfile[pos] ; pos += 1 
    535                                     if not char.isdigit() : 
    536                                         break 
    537                                     size = (size * 10) + int(char)     
    538                                 if char in ("a", "o", "h", "m") :     
    539                                     ispcl3 = 1  # certainely a PCL3 document 
    540                                     pos -= 1    # fix position : we were ahead 
    541                                     if char == "h" : 
    542                                         self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 
    543                                         mediasourcecount += 1 
    544                                     elif char == "a" : 
    545                                         self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 
    546                                         mediasizecount += 1 
    547                                     elif char == "o" : 
    548                                         self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 
    549                                         orientationcount += 1 
    550                                     elif char == "m" : 
    551                                         self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 
    552                                         mediatypecount += 1 
    553             except IndexError : # EOF ? 
    554                 pass 
    555         finally : 
    556             minfile.close() 
    557                              
    558         # if pagecount is still 0, we will use the number 
    559         # of resets instead of the number of form feed characters. 
    560         # but the number of resets is always at least 2 with a valid 
    561         # pcl file : one at the very start and one at the very end 
    562         # of the job's data. So we substract 2 from the number of 
    563         # resets. And since on our test data we needed to substract 
    564         # 1 more, we finally substract 3, and will test several 
    565         # PCL files with this. If resets < 2, then the file is 
    566         # probably not a valid PCL file, so we use 0 
    567          
    568         if self.debug : 
    569             sys.stderr.write("pagecount : %s\n" % pagecount) 
    570             sys.stderr.write("resets : %s\n" % resets) 
    571             sys.stderr.write("ejects : %s\n" % ejects) 
    572             sys.stderr.write("backsides : %s\n" % backsides) 
    573             sys.stderr.write("startgfx : %s\n" % startgfx) 
    574             sys.stderr.write("endgfx : %s\n" % endgfx) 
    575             sys.stderr.write("mediasourcecount : %s\n" % mediasourcecount) 
    576             sys.stderr.write("mediasizecount : %s\n" % mediasizecount) 
    577             sys.stderr.write("orientationcount : %s\n" % orientationcount) 
    578             sys.stderr.write("mediatypecount : %s\n" % mediatypecount) 
    579             sys.stderr.write("escstart : %s\n" % escstart) 
    580             sys.stderr.write("hasirmarker : %s\n" % hasirmarker) 
    581          
    582         if hasirmarker : 
    583             self.logdebug("Rule #20 (probably a Canon ImageRunner)") 
    584             pagecount += 1 
    585         elif (orientationcount == (pagecount - 1)) and (resets == 1) : 
    586             if resets == ejects == startgfx == mediasourcecount == escstart == 1 : 
    587                 self.logdebug("Rule #19") 
    588             else :     
    589                 self.logdebug("Rule #1") 
    590                 pagecount -= 1 
    591         elif pagecount and (pagecount == orientationcount) : 
    592             self.logdebug("Rule #2") 
    593         elif resets == ejects == mediasourcecount == mediasizecount == escstart == 1 : 
    594             #if ((startgfx and endgfx) and (startgfx != endgfx)) or (startgfx == endgfx == 0) : 
    595             if (startgfx and endgfx) or (startgfx == endgfx == 0) : 
    596                 self.logdebug("Rule #3") 
    597                 pagecount = orientationcount 
    598             elif (endgfx and not startgfx) and (pagecount > orientationcount) :     
    599                 self.logdebug("Rule #4") 
    600                 pagecount = orientationcount 
    601             else :      
    602                 self.logdebug("Rule #5") 
    603                 pagecount += 1 
    604         elif (ejects == mediasourcecount == orientationcount) and (startgfx == endgfx) :      
    605             if (resets == 2) and (orientationcount == (pagecount - 1)) and (orientationcount > 1) : 
    606                 self.logdebug("Rule #6") 
    607                 pagecount = orientationcount 
    608         elif pagecount == mediasourcecount == escstart :  
    609             self.logdebug("Rule #7") 
    610         elif resets == startgfx == endgfx == mediasizecount == orientationcount == escstart == 1 :      
    611             self.logdebug("Rule #8") 
    612         elif resets == startgfx == endgfx == (pagecount - 1) :     
    613             self.logdebug("Rule #9") 
    614         elif (not startgfx) and (not endgfx) : 
    615             self.logdebug("Rule #10") 
    616         elif (resets == 2) and (startgfx == endgfx) and (mediasourcecount == 1) : 
    617             if orientationcount == (pagecount - 1) : 
    618                 self.logdebug("Rule #11") 
    619                 pagecount = orientationcount 
    620             elif not pagecount :     
    621                 self.logdebug("Rule #17") 
    622                 pagecount = ejects 
    623         elif (resets == 1) and (startgfx == endgfx) and (mediasourcecount == 0) : 
    624             if (startgfx > 1) and (startgfx != (pagecount - 1)) : 
    625                 self.logdebug("Rule #12") 
    626                 pagecount -= 1 
    627             else :     
    628                 self.logdebug("Rule #18") 
    629         elif startgfx == endgfx :     
    630             self.logdebug("Rule #13") 
    631             pagecount = startgfx 
    632         elif startgfx == (endgfx - 1) :     
    633             self.logdebug("Rule #14") 
    634             pagecount = startgfx 
    635         elif (startgfx == 1) and not endgfx :     
    636             self.logdebug("Rule #15") 
    637             pass 
    638         else :     
    639             self.logdebug("Rule #16") 
    640             pagecount = abs(startgfx - endgfx) 
    641              
    642         defaultpjlcopies = 1     
    643         defaultduplexmode = "Simplex" 
    644         defaultpapersize = "" 
    645         oldpjlcopies = -1 
    646         oldduplexmode = "" 
    647         oldpapersize = "" 
    648         for pnum in range(pagecount) : 
    649             # if no number of copies defined, take the preceding one else the one set before any page else 1. 
    650             page = pages.get(pnum, pages.get(pnum - 1, pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : "", "duplex": 0}))) 
    651             pjlstuff = page["escaped"] 
    652             if pjlstuff : 
    653                 pjlparser = pjl.PJLParser(pjlstuff) 
    654                 nbdefaultcopies = int(pjlparser.default_variables.get("COPIES", -1)) 
    655                 nbcopies = int(pjlparser.environment_variables.get("COPIES", -1)) 
    656                 nbdefaultqty = int(pjlparser.default_variables.get("QTY", -1)) 
    657                 nbqty = int(pjlparser.environment_variables.get("QTY", -1)) 
    658                 if nbdefaultcopies > -1 : 
    659                     defaultpjlcopies = nbdefaultcopies 
    660                 if nbdefaultqty > -1 : 
    661                     defaultpjlcopies = nbdefaultqty 
    662                 if nbcopies > -1 : 
    663                     pjlcopies = nbcopies 
    664                 elif nbqty > -1 : 
    665                     pjlcopies = nbqty 
    666                 else : 
    667                     if oldpjlcopies == -1 :     
    668                         pjlcopies = defaultpjlcopies 
    669                     else :     
    670                         pjlcopies = oldpjlcopies     
    671                 if page["duplex"] :         
    672                     duplexmode = "Duplex" 
    673                 else :     
    674                     defaultdm = pjlparser.default_variables.get("DUPLEX", "") 
    675                     if defaultdm : 
    676                         if defaultdm.upper() == "ON" : 
    677                             defaultduplexmode = "Duplex" 
    678                         else :     
    679                             defaultduplexmode = "Simplex" 
    680                     envdm = pjlparser.environment_variables.get("DUPLEX", "") 
    681                     if envdm : 
    682                         if envdm.upper() == "ON" : 
    683                             duplexmode = "Duplex" 
    684                         else :     
    685                             duplexmode = "Simplex" 
    686                     else :         
    687                         duplexmode = oldduplexmode or defaultduplexmode 
    688                 defaultps = pjlparser.default_variables.get("PAPER", "") 
    689                 if defaultps : 
    690                     defaultpapersize = defaultps 
    691                 envps = pjlparser.environment_variables.get("PAPER", "") 
    692                 if envps : 
    693                     papersize = envps 
    694                 else :     
    695                     if not oldpapersize : 
    696                         papersize = defaultpapersize 
    697                     else :     
    698                         papersize = oldpapersize 
    699             else :         
    700                 if oldpjlcopies == -1 : 
    701                     pjlcopies = defaultpjlcopies 
    702                 else :     
    703                     pjlcopies = oldpjlcopies 
    704                  
    705                 duplexmode = (page["duplex"] and "Duplex") or oldduplexmode or defaultduplexmode 
    706                 if not oldpapersize :     
    707                     papersize = defaultpapersize 
    708                 else :     
    709                     papersize = oldpapersize 
    710                 papersize = oldpapersize or page["mediasize"] 
    711             if page["mediasize"] != "Default" : 
    712                 papersize = page["mediasize"] 
    713             if not duplexmode :     
    714                 duplexmode = oldduplexmode or defaultduplexmode 
    715             oldpjlcopies = pjlcopies     
    716             oldduplexmode = duplexmode 
    717             oldpapersize = papersize 
    718             copies = pjlcopies * page["copies"]         
    719             pagecount += (copies - 1) 
    720             self.logdebug("%s*%s*%s*%s*%s*%s*BW" % (copies, \ 
    721                                               page["mediatype"], \ 
    722                                               papersize, \ 
    723                                               page["orientation"], \ 
    724                                               page["mediasource"], \ 
    725                                               duplexmode)) 
    726                  
    727         return pagecount 
    728         """ 
     399        self.logdebug("Pagecount : \t\t\t%i" % self.pagecount) 
     400        self.logdebug("Resets : \t\t\t%i" % self.resets) 
     401        self.logdebug("Copies : \t\t\t%s" % self.copies) 
     402        self.logdebug("NbCopiesMarks : \t\t%i" % len(self.copies)) 
     403        self.logdebug("MediaTypes : \t\t\t%s" % self.mediatypesvalues) 
     404        self.logdebug("NbMediaTypes : \t\t\t%i" % len(self.mediatypesvalues)) 
     405        self.logdebug("MediaSizes : \t\t\t%s" % self.mediasizesvalues) 
     406        self.logdebug("NbMediaSizes : \t\t\t%i" % len(self.mediasizesvalues)) 
     407        self.logdebug("MediaSources : \t\t\t%s" % self.mediasourcesvalues) 
     408        nbmediasourcesdefault = len([m for m in self.mediasourcesvalues if m == 'Default']) 
     409        self.logdebug("MediaSourcesDefault : \t\t%i" % nbmediasourcesdefault) 
     410        self.logdebug("MediaSourcesNOTDefault : \t%i" % (len(self.mediasourcesvalues) - nbmediasourcesdefault)) 
     411        self.logdebug("Orientations : \t\t\t%s" % self.orientationsvalues) 
     412        self.logdebug("NbOrientations : \t\t\t%i" % len(self.orientationsvalues)) 
     413        self.logdebug("StartGfx : \t\t\t%s" % len(self.startgfx)) 
     414        self.logdebug("EndGfx : \t\t\t%s" % len(self.endgfx)) 
     415        self.logdebug("BackSides : \t\t\t%s" % self.backsides) 
     416        self.logdebug("NbBackSides : \t\t\t%i" % len(self.backsides)) 
     417         
     418        return self.pagecount or nbmediasourcesdefault 
    729419         
    730420def test() :