Index: /pkipplib/trunk/tests/test.py
===================================================================
--- /pkipplib/trunk/tests/test.py (revision 28)
+++ /pkipplib/trunk/tests/test.py (revision 13)
@@ -26,11 +26,8 @@
 import pkipplib        
     
-cups = pkipplib.CUPS()
-answer = cups.createSubscription("ipp://localhost/", ["printer-added", "printer-deleted"],
-                                                     userdata="samplenotifier:blah",
-                                                     recipient="samplenotifier",
-                                                     charset="utf-8")
+cups = pkipplib.CUPS()    
+answer = cups.getPPD("HL6050DN")
+#answer = cups.getJobAttributes(566)
 print answer
-
 #print answer.operation["attributes-charset"]
 
Index: /pkipplib/trunk/pkipplib/pkipplib.py
===================================================================
--- /pkipplib/trunk/pkipplib/pkipplib.py (revision 30)
+++ /pkipplib/trunk/pkipplib/pkipplib.py (revision 18)
@@ -24,7 +24,5 @@
 
 import sys
-import os
 import urllib2
-import socket
 from struct import pack, unpack
 
@@ -238,32 +236,4 @@
 IPP_PRINTER_IS_DEACTIVATED = 0x50a
   
-CUPS_PRINTER_LOCAL = 0x0000
-CUPS_PRINTER_CLASS = 0x0001
-CUPS_PRINTER_REMOTE = 0x0002
-CUPS_PRINTER_BW = 0x0004
-CUPS_PRINTER_COLOR = 0x0008
-CUPS_PRINTER_DUPLEX = 0x0010
-CUPS_PRINTER_STAPLE = 0x0020
-CUPS_PRINTER_COPIES = 0x0040
-CUPS_PRINTER_COLLATE = 0x0080
-CUPS_PRINTER_PUNCH = 0x0100
-CUPS_PRINTER_COVER = 0x0200
-CUPS_PRINTER_BIND = 0x0400
-CUPS_PRINTER_SORT = 0x0800
-CUPS_PRINTER_SMALL = 0x1000
-CUPS_PRINTER_MEDIUM = 0x2000
-CUPS_PRINTER_LARGE = 0x4000
-CUPS_PRINTER_VARIABLE = 0x8000
-CUPS_PRINTER_IMPLICIT = 0x1000
-CUPS_PRINTER_DEFAULT = 0x2000
-CUPS_PRINTER_FAX = 0x4000
-CUPS_PRINTER_REJECTING = 0x8000
-CUPS_PRINTER_DELETE = 0x1000
-CUPS_PRINTER_NOT_SHARED = 0x2000
-CUPS_PRINTER_AUTHENTICATED = 0x4000
-CUPS_PRINTER_COMMANDS = 0x8000
-CUPS_PRINTER_OPTIONS = 0xe6ff
-  
-  
 class IPPError(Exception) :
     """An exception for IPP related stuff."""
@@ -296,5 +266,4 @@
     def __getitem__(self, key) :
         """Returns an attribute's value."""
-        answer = []
         attributeslist = getattr(self.request, "_%s_attributes" % self.name)
         for i in range(len(attributeslist)) :
@@ -303,7 +272,5 @@
                 (attrname, attrvalue) = attribute[j]
                 if attrname == key :
-                    answer.extend(attrvalue)
-        if answer :
-            return answer
+                    return attrvalue
         raise KeyError, key            
     
@@ -315,4 +282,7 @@
                                 operation_id=None, \
                                 request_id=None, \
+                                url = "http://localhost:631", \
+                                username = None, \
+                                password = None, \
                                 debug=False) :
         """Initializes an IPP Message object.
@@ -323,4 +293,7 @@
              debug : a boolean value to output debug info on stderr.
         """
+        self.url = url
+        self.username = username
+        self.password = password
         self.debug = debug
         self._data = data
@@ -346,5 +319,5 @@
         self.tags[0x05] = "unsupported-attributes-tag"
         self.tags[0x06] = "subscription-attributes-tag"
-        self.tags[0x07] = "event_notification-attributes-tag"
+        self.tags[0x07] = "event-notification-attributes-tag"
         
         # out of band values
@@ -445,4 +418,12 @@
         self.request_id = reqid
         
+    def nextRequestId(self) :        
+        """Increments the current request id and returns the new value."""
+        try :
+            self.request_id += 1
+        except TypeError :    
+            self.request_id = 1
+        return self.request_id
+            
     def dump(self) :    
         """Generates an IPP Message.
@@ -452,7 +433,9 @@
         mybuffer = []
         if None not in (self.version, self.operation_id) :
+            if self.request_id is None :
+                self.nextRequestId()
             mybuffer.append(chr(self.version[0]) + chr(self.version[1]))
             mybuffer.append(pack(">H", self.operation_id))
-            mybuffer.append(pack(">I", self.request_id or 1))
+            mybuffer.append(pack(">I", self.request_id))
             for attrtype in self.attributes_types :
                 for attribute in getattr(self, "_%s_attributes" % attrtype) :
@@ -495,5 +478,5 @@
         self.position = 8
         endofattributes = self.tagvalues["end-of-attributes-tag"]
-        maxdelimiter = self.tagvalues["event_notification-attributes-tag"]
+        maxdelimiter = self.tagvalues["event-notification-attributes-tag"]
         nulloffset = lambda : 0
         try :
@@ -579,36 +562,33 @@
         return self.parseTag()
         
+    def doRequest(self, url=None, username=None, password=None, samerequestid=False) :
+        """Sends the current request to the URL.
+           NB : only ipp:// URLs are currently unsupported so use
+           either http://host:631/ or https://host:443 instead...
+           
+           returns a new IPPRequest object, containing the parsed answer.
+        """   
+        if not samerequestid :
+            self.nextRequestId()
+        cx = urllib2.Request(url=url or self.url or "http://localhost:631/", 
+                             data=self.dump())
+        cx.add_header("Content-Type", "application/ipp")
+        response = urllib2.urlopen(cx)
+        datas = response.read()
+        ippresponse = IPPRequest(datas)
+        ippresponse.parse()
+        return ippresponse
+        
             
 class CUPS :
     """A class for a CUPS instance."""
-    def __init__(self, url=None, username=None, password=None, charset="utf-8", language="en-us", debug=False) :
+    def __init__(self, url="http://localhost:631", username=None, password=None, charset="utf-8", language="en-us") :
         """Initializes the CUPS instance."""
-        if url is not None :
-            self.url = url.replace("ipp://", "http://")
-            if self.url.endswith("/") :
-                self.url = self.url[:-1]
-        else :        
-            self.url = self.getDefaultURL()
+        self.url = url
         self.username = username
         self.password = password
         self.charset = charset
         self.language = language
-        self.debug = debug
-        self.lastError = None
-        self.lastErrorMessage = None
-        self.requestId = None
-        
-    def getDefaultURL(self) :    
-        """Builds a default URL."""
-        # TODO : encryption methods.
-        server = os.environ.get("CUPS_SERVER") or "localhost"
-        port = os.environ.get("IPP_PORT") or 631
-        if server.startswith("/") :
-            # it seems it's a unix domain socket.
-            # we can't handle this right now, so we use the default instead.
-            return "http://localhost:%s" % port
-        else :    
-            return "http://%s:%s" % (server, port)
-            
+        
     def identifierToURI(self, service, ident) :
         """Transforms an identifier into a particular URI depending on requested service."""
@@ -617,52 +597,25 @@
                              ident)
         
-    def nextRequestId(self) :        
-        """Increments the current request id and returns the new value."""
-        try :
-            self.requestId += 1
-        except TypeError :    
-            self.requestId = 1
-        return self.requestId
-            
     def newRequest(self, operationid=None) :
         """Generates a new empty request."""
         if operationid is not None :
             req = IPPRequest(operation_id=operationid, \
-                             request_id=self.nextRequestId(), \
-                             debug=self.debug)
+                             url=self.url, \
+                             username=self.username, \
+                             password=self.password)
             req.operation["attributes-charset"] = ("charset", self.charset)
             req.operation["attributes-natural-language"] = ("naturalLanguage", self.language)
             return req
     
-    def doRequest(self, req, url=None) :
-        """Sends a request to the CUPS server.
-           returns a new IPPRequest object, containing the parsed answer.
-        """   
-        connexion = urllib2.Request(url=url or self.url, \
-                             data=req.dump())
-        connexion.add_header("Content-Type", "application/ipp")
-        if self.username :
-            pwmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
-            pwmanager.add_password(None, \
-                                   "%s%s" % (connexion.get_host(), connexion.get_selector()), \
-                                   self.username, \
-                                   self.password or "")
-            authhandler = urllib2.HTTPBasicAuthHandler(pwmanager)                       
-            opener = urllib2.build_opener(authhandler)
-            urllib2.install_opener(opener)
-        self.lastError = None    
-        self.lastErrorMessage = None
-        try :    
-            response = urllib2.urlopen(connexion)
-        except (urllib2.URLError, urllib2.HTTPError, socket.error), error :    
-            self.lastError = error
-            self.lastErrorMessage = str(error)
-            return None
-        else :    
-            datas = response.read()
-            ippresponse = IPPRequest(datas)
-            ippresponse.parse()
-            return ippresponse
+    def getDefault(self) :
+        """Retrieves CUPS' default printer."""
+        return self.newRequest(CUPS_GET_DEFAULT).doRequest()
     
+    def getJobAttributes(self, jobid) :    
+        """Retrieves a print job's attributes."""
+        req = self.newRequest(IPP_GET_JOB_ATTRIBUTES)
+        req.operation["job-uri"] = ("uri", self.identifierToURI("jobs", jobid))
+        return req.doRequest()
+        
     def getPPD(self, queuename) :    
         """Retrieves the PPD for a particular queuename."""
@@ -671,110 +624,28 @@
         for attrib in ("printer-uri-supported", "printer-type", "member-uris") :
             req.operation["requested-attributes"] = ("nameWithoutLanguage", attrib)
-        return self.doRequest(req)  # TODO : get the PPD from the actual print server
-        
-    def getDefault(self) :
-        """Retrieves CUPS' default printer."""
-        return self.doRequest(self.newRequest(CUPS_GET_DEFAULT))
-    
-    def getJobAttributes(self, jobid) :    
-        """Retrieves a print job's attributes."""
-        req = self.newRequest(IPP_GET_JOB_ATTRIBUTES)
-        req.operation["job-uri"] = ("uri", self.identifierToURI("jobs", jobid))
-        return self.doRequest(req)
-        
-    def getPrinters(self) :    
-        """Returns the list of print queues names."""
-        req = self.newRequest(CUPS_GET_PRINTERS)
-        req.operation["requested-attributes"] = ("keyword", "printer-name")
-        req.operation["printer-type"] = ("enum", 0)
-        req.operation["printer-type-mask"] = ("enum", CUPS_PRINTER_CLASS)
-        return [printer[1] for printer in self.doRequest(req).printer["printer-name"]]
-        
-    def getDevices(self) :    
-        """Returns a list of devices as (deviceclass, deviceinfo, devicemakeandmodel, deviceuri) tuples."""
-        answer = self.doRequest(self.newRequest(CUPS_GET_DEVICES))
-        return zip([d[1] for d in answer.printer["device-class"]], \
-                   [d[1] for d in answer.printer["device-info"]], \
-                   [d[1] for d in answer.printer["device-make-and-model"]], \
-                   [d[1] for d in answer.printer["device-uri"]])
-                   
-    def getPPDs(self) :    
-        """Returns a list of PPDs as (ppdnaturallanguage, ppdmake, ppdmakeandmodel, ppdname) tuples."""
-        answer = self.doRequest(self.newRequest(CUPS_GET_PPDS))
-        return zip([d[1] for d in answer.printer["ppd-natural-language"]], \
-                   [d[1] for d in answer.printer["ppd-make"]], \
-                   [d[1] for d in answer.printer["ppd-make-and-model"]], \
-                   [d[1] for d in answer.printer["ppd-name"]])
-                   
-    def createSubscription(self, uri, events=["all"],
-                                      userdata=None,
-                                      recipient=None,
-                                      pullmethod=None,
-                                      charset=None,
-                                      naturallanguage=None,
-                                      leaseduration=None,
-                                      timeinterval=None,
-                                      jobid=None) :
-        """Creates a job, printer or server subscription."""
-        if jobid is not None :
-            opid = IPP_CREATE_JOB_SUBSCRIPTION
-            uritype = "job-uri"
-        else :
-            opid = IPP_CREATE_PRINTER_SUBSCRIPTION
-            uritype = "printer-uri"
-        req = self.newRequest(opid)
-        req.operation[uritype] = ("uri", uri)
-        for event in events :
-            req.subscription["notify-events"] = ("keyword", event)
-        if userdata is not None :    
-            req.subscription["notify-user-data"] = ("octetString-with-an-unspecified-format", userdata)
-        if recipient is not None :    
-            req.subscription["notify-recipient"] = ("uri", recipient)
-        if pullmethod is not None :
-            req.subscription["notify-pull-method"] = ("keyword", pullmethod)
-        if charset is not None :
-            req.subscription["notify-charset"] = ("charset", charset)
-        if naturallanguage is not None :
-            req.subscription["notify-natural-language"] = ("naturalLanguage", naturallanguage)
-        if leaseduration is not None :
-            req.subscription["notify-lease-duration"] = ("integer", leaseduration)
-        if timeinterval is not None :
-            req.subscription["notify-time-interval"] = ("integer", timeinterval)
-        if jobid is not None :
-            req.subscription["notify-job-id"] = ("integer", jobid)
-        return self.doRequest(req)
-            
-    def cancelSubscription(self, uri, subscriptionid, jobid=None) :    
-        """Cancels a subscription."""
-        req = self.newRequest(IPP_CANCEL_SUBSCRIPTION)
-        if jobid is not None :
-            uritype = "job-uri"
-        else :
-            uritype = "printer-uri"
-        req.operation[uritype] = ("uri", uri)
-        req.event_notification["notify-subscription-id"] = ("integer", subscriptionid)
-        return self.doRequest(req)
-        
+        return req.doRequest()  # TODO : get the PPD from the actual print server
+        
+            
 if __name__ == "__main__" :            
     if (len(sys.argv) < 2) or (sys.argv[1] == "--debug") :
-        print "usage : python pkipplib.py /var/spool/cups/c00005 [--debug] (for example)\n"
+        print "usage : python ipp.py /var/spool/cups/c00005 [--debug] (for example)\n"
     else :    
         infile = open(sys.argv[1], "rb")
-        filedata = infile.read()
+        data = infile.read()
         infile.close()
         
-        msg = IPPRequest(filedata, debug=(sys.argv[-1]=="--debug"))
-        msg.parse()
-        msg2 = IPPRequest(msg.dump())
-        msg2.parse()
-        filedata2 = msg2.dump()
-        
-        if filedata == filedata2 :
+        message = IPPRequest(data, debug=(sys.argv[-1]=="--debug"))
+        message.parse()
+        message2 = IPPRequest(message.dump())
+        message2.parse()
+        data2 = message2.dump()
+        
+        if data == data2 :
             print "Test OK : parsing original and parsing the output of the dump produce the same dump !"
-            print str(msg)
+            print str(message)
         else :    
             print "Test Failed !"
-            print str(msg)
+            print str(message)
             print
-            print str(msg2)
-        
+            print str(message2)
+        
Index: /pkipplib/trunk/pkipplib/version.py
===================================================================
--- /pkipplib/trunk/pkipplib/version.py (revision 27)
+++ /pkipplib/trunk/pkipplib/version.py (revision 14)
@@ -21,5 +21,5 @@
 #
 
-__version__ = "0.07"
+__version__ = "0.03"
 
 __doc__ = "pkipplib : IPP and CUPS support for Python."
Index: /pkipplib/trunk/README
===================================================================
--- /pkipplib/trunk/README (revision 25)
+++ /pkipplib/trunk/README (revision 19)
@@ -109,6 +109,5 @@
 from pkipplib import pkipplib
 
-# Create a CUPS client instance
-# cups = pkipplib.CUPS(url="http://server:631", username="john", password="blah!")
+# By default, connects to http://localhost:631
 cups = pkipplib.CUPS()
 
@@ -130,5 +129,5 @@
     
 # Sends this request to the CUPS server    
-answer = cups.doRequest(request)
+answer = request.doRequest()    
 
 # Print the answer as a string of text
