Changeset 582

Show
Ignore:
Timestamp:
03/13/05 15:30:22 (18 years ago)
Author:
jerome
Message:

More code for IPP parsing

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tea4cups/trunk/tea4cups

    r581 r582  
    3333from struct import unpack 
    3434 
    35 OPERATION_ATTRIBUTES_TAG = 0x01 
    36 JOB_ATTRIBUTES_TAG = 0x02 
    37 END_OF_ATTRIBUTES_TAG = 0x03 
    38 PRINTER_ATTRIBUTES_TAG = 0x04 
    39 UNSUPPORTED_ATTRIBUTES_TAG = 0x05 
    40  
    4135class TeeError(Exception): 
    4236    """Base exception for Tea4CUPS related stuff.""" 
     
    5650    pass  
    5751     
     52# Some IPP constants     
     53OPERATION_ATTRIBUTES_TAG = 0x01 
     54JOB_ATTRIBUTES_TAG = 0x02 
     55END_OF_ATTRIBUTES_TAG = 0x03 
     56PRINTER_ATTRIBUTES_TAG = 0x04 
     57UNSUPPORTED_ATTRIBUTES_TAG = 0x05 
     58 
    5859class IPPMessage : 
    5960    """A class for IPP message files.""" 
     
    372373        self.Directory = self.getPrintQueueOption(self.PrinterName, "directory") 
    373374        self.DataFile = os.path.join(self.Directory, "%s-%s-%s-%s" % (self.myname, self.PrinterName, self.UserName, self.JobId)) 
    374              
     375        self.ClientHost = self.extractJobOriginatingHostName() 
     376             
     377    def extractJobOriginatingHostName(self) :         
     378        """Extracts the client's hostname or IP address from the CUPS message file for current job.""" 
     379        requestroot = cupsdconf.get("requestroot", "/var/spool/cups") 
     380        if (len(self.JobId) < 5) and self.JobId.isdigit() : 
     381            ippmessagefile = "c%05i" % int(self.JobId) 
     382        else :     
     383            ippmessagefile = "c%s" % self.JobId 
     384        ippmessagefile = os.path.join(requestroot, ippmessagefile) 
     385        ippmessage = {} 
     386        try : 
     387            ippdatafile = open(ippmessagefile) 
     388        except :     
     389            self.logInfo("Unable to open IPP message file %s" % ippmessagefile, "warn") 
     390        else :     
     391            self.logDebug("Parsing of IPP message file %s begins." % ippmessagefile) 
     392            try : 
     393                ippmessage = IPPMessage(ippdatafile.read()) 
     394            except IPPError, msg :     
     395                self.logInfo("Error while parsing %s : %s" % (ippmessagefile, msg), "warn") 
     396            else :     
     397                self.logDebug("Parsing of IPP message file %s ends." % ippmessagefile) 
     398            ippdatafile.close() 
     399        return ippmessage.get("job-originating-host-name")     
     400                 
    375401    def exportAttributes(self) :     
    376402        """Exports our backend's attributes to the environment.""" 
     
    381407        os.environ["TEAJOBSIZE"] = str(self.JobSize) 
    382408        os.environ["TEAMD5SUM"] = self.JobMD5Sum 
     409        os.environ["TEACLIENTHOST"] = self.ClientHost or "" 
    383410        os.environ["TEAJOBID"] = self.JobId 
    384411        os.environ["TEAUSERNAME"] = self.UserName