Changeset 26

Show
Ignore:
Timestamp:
06/06/06 23:06:33 (17 years ago)
Author:
jerome
Message:

Added support for getPPDs, getDevices, and getPrinters.
Allows a request to be sent to a different url than the
CUPS instance itself.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pkipplib/trunk/pkipplib/pkipplib.py

    r25 r26  
    238238IPP_PRINTER_IS_DEACTIVATED = 0x50a 
    239239   
     240CUPS_PRINTER_LOCAL = 0x0000 
     241CUPS_PRINTER_CLASS = 0x0001 
     242CUPS_PRINTER_REMOTE = 0x0002 
     243CUPS_PRINTER_BW = 0x0004 
     244CUPS_PRINTER_COLOR = 0x0008 
     245CUPS_PRINTER_DUPLEX = 0x0010 
     246CUPS_PRINTER_STAPLE = 0x0020 
     247CUPS_PRINTER_COPIES = 0x0040 
     248CUPS_PRINTER_COLLATE = 0x0080 
     249CUPS_PRINTER_PUNCH = 0x0100 
     250CUPS_PRINTER_COVER = 0x0200 
     251CUPS_PRINTER_BIND = 0x0400 
     252CUPS_PRINTER_SORT = 0x0800 
     253CUPS_PRINTER_SMALL = 0x1000 
     254CUPS_PRINTER_MEDIUM = 0x2000 
     255CUPS_PRINTER_LARGE = 0x4000 
     256CUPS_PRINTER_VARIABLE = 0x8000 
     257CUPS_PRINTER_IMPLICIT = 0x1000 
     258CUPS_PRINTER_DEFAULT = 0x2000 
     259CUPS_PRINTER_FAX = 0x4000 
     260CUPS_PRINTER_REJECTING = 0x8000 
     261CUPS_PRINTER_DELETE = 0x1000 
     262CUPS_PRINTER_NOT_SHARED = 0x2000 
     263CUPS_PRINTER_AUTHENTICATED = 0x4000 
     264CUPS_PRINTER_COMMANDS = 0x8000 
     265CUPS_PRINTER_OPTIONS = 0xe6ff 
     266   
     267   
    240268class IPPError(Exception) : 
    241269    """An exception for IPP related stuff.""" 
     
    268296    def __getitem__(self, key) : 
    269297        """Returns an attribute's value.""" 
     298        answer = [] 
    270299        attributeslist = getattr(self.request, "_%s_attributes" % self.name) 
    271300        for i in range(len(attributeslist)) : 
     
    274303                (attrname, attrvalue) = attribute[j] 
    275304                if attrname == key : 
    276                     return attrvalue 
     305                    answer.extend(attrvalue) 
     306        if answer : 
     307            return answer 
    277308        raise KeyError, key             
    278309     
     
    604635            return req 
    605636     
    606     def doRequest(self, req) : 
     637    def doRequest(self, req, url=None) : 
    607638        """Sends a request to the CUPS server. 
    608639           returns a new IPPRequest object, containing the parsed answer. 
    609640        """    
    610         connexion = urllib2.Request(url=self.url, \ 
     641        connexion = urllib2.Request(url=url or self.url, \ 
    611642                             data=req.dump()) 
    612643        connexion.add_header("Content-Type", "application/ipp") 
     
    634665            return ippresponse 
    635666     
     667    def getPPD(self, queuename) :     
     668        """Retrieves the PPD for a particular queuename.""" 
     669        req = self.newRequest(IPP_GET_PRINTER_ATTRIBUTES) 
     670        req.operation["printer-uri"] = ("uri", self.identifierToURI("printers", queuename)) 
     671        for attrib in ("printer-uri-supported", "printer-type", "member-uris") : 
     672            req.operation["requested-attributes"] = ("nameWithoutLanguage", attrib) 
     673        return self.doRequest(req)  # TODO : get the PPD from the actual print server 
     674         
    636675    def getDefault(self) : 
    637676        """Retrieves CUPS' default printer.""" 
     
    644683        return self.doRequest(req) 
    645684         
    646     def getPPD(self, queuename) :     
    647         """Retrieves the PPD for a particular queuename.""" 
    648         req = self.newRequest(IPP_GET_PRINTER_ATTRIBUTES) 
    649         req.operation["printer-uri"] = ("uri", self.identifierToURI("printers", queuename)) 
    650         for attrib in ("printer-uri-supported", "printer-type", "member-uris") : 
    651             req.operation["requested-attributes"] = ("nameWithoutLanguage", attrib) 
    652         return self.doRequest(req)  # TODO : get the PPD from the actual print server 
    653          
     685    def getPrinters(self) :     
     686        """Returns the list of print queues names.""" 
     687        req = self.newRequest(CUPS_GET_PRINTERS) 
     688        req.operation["requested-attributes"] = ("keyword", "printer-name") 
     689        req.operation["printer-type"] = ("enum", 0) 
     690        req.operation["printer-type-mask"] = ("enum", CUPS_PRINTER_CLASS) 
     691        return [printer[1] for printer in self.doRequest(req).printer["printer-name"]] 
     692         
     693    def getDevices(self) :     
     694        """Returns a list of devices as (deviceclass, deviceinfo, devicemakeandmodel, deviceuri) tuples.""" 
     695        answer = self.doRequest(self.newRequest(CUPS_GET_DEVICES)) 
     696        return zip([d[1] for d in answer.printer["device-class"]], \ 
     697                   [d[1] for d in answer.printer["device-info"]], \ 
     698                   [d[1] for d in answer.printer["device-make-and-model"]], \ 
     699                   [d[1] for d in answer.printer["device-uri"]]) 
     700                    
     701    def getPPDs(self) :     
     702        """Returns a list of PPDs as (ppdnaturallanguage, ppdmake, ppdmakeandmodel, ppdname) tuples.""" 
     703        answer = self.doRequest(self.newRequest(CUPS_GET_PPDS)) 
     704        return zip([d[1] for d in answer.printer["ppd-natural-language"]], \ 
     705                   [d[1] for d in answer.printer["ppd-make"]], \ 
     706                   [d[1] for d in answer.printer["ppd-make-and-model"]], \ 
     707                   [d[1] for d in answer.printer["ppd-name"]]) 
    654708             
    655709if __name__ == "__main__" :