root / pkipplib / trunk / pkipplib / pkipplib.py @ 40

Revision 40, 29.3 kB (checked in by jerome, 17 years ago)

Now licensed under the terms of the GNU General Public License,
version 3 or later.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
RevLine 
[3]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
[14]4# pkipplib : IPP and CUPS support for Python
[3]5#
[38]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[40]7# This program is free software: you can redistribute it and/or modify
[3]8# it under the terms of the GNU General Public License as published by
[40]9# the Free Software Foundation, either version 3 of the License, or
[3]10# (at your option) any later version.
[40]11#
[3]12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
[40]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[3]19#
20# $Id$
21#
22#
23
24import sys
[25]25import os
[3]26import urllib2
[21]27import socket
[3]28from struct import pack, unpack
29
30IPP_VERSION = "1.1"     # default version number
31
32IPP_PORT = 631
33
34IPP_MAX_NAME = 256
35IPP_MAX_VALUES = 8
36
37IPP_TAG_ZERO = 0x00
38IPP_TAG_OPERATION = 0x01
39IPP_TAG_JOB = 0x02
40IPP_TAG_END = 0x03
41IPP_TAG_PRINTER = 0x04
42IPP_TAG_UNSUPPORTED_GROUP = 0x05
43IPP_TAG_SUBSCRIPTION = 0x06
44IPP_TAG_EVENT_NOTIFICATION = 0x07
45IPP_TAG_UNSUPPORTED_VALUE = 0x10
46IPP_TAG_DEFAULT = 0x11
47IPP_TAG_UNKNOWN = 0x12
48IPP_TAG_NOVALUE = 0x13
49IPP_TAG_NOTSETTABLE = 0x15
50IPP_TAG_DELETEATTR = 0x16
51IPP_TAG_ADMINDEFINE = 0x17
52IPP_TAG_INTEGER = 0x21
53IPP_TAG_BOOLEAN = 0x22
54IPP_TAG_ENUM = 0x23
55IPP_TAG_STRING = 0x30
56IPP_TAG_DATE = 0x31
57IPP_TAG_RESOLUTION = 0x32
58IPP_TAG_RANGE = 0x33
59IPP_TAG_BEGIN_COLLECTION = 0x34
60IPP_TAG_TEXTLANG = 0x35
61IPP_TAG_NAMELANG = 0x36
62IPP_TAG_END_COLLECTION = 0x37
63IPP_TAG_TEXT = 0x41
64IPP_TAG_NAME = 0x42
65IPP_TAG_KEYWORD = 0x44
66IPP_TAG_URI = 0x45
67IPP_TAG_URISCHEME = 0x46
68IPP_TAG_CHARSET = 0x47
69IPP_TAG_LANGUAGE = 0x48
70IPP_TAG_MIMETYPE = 0x49
71IPP_TAG_MEMBERNAME = 0x4a
72IPP_TAG_MASK = 0x7fffffff
73IPP_TAG_COPY = -0x7fffffff-1
74
75IPP_RES_PER_INCH = 3
76IPP_RES_PER_CM = 4
77
78IPP_FINISHINGS_NONE = 3
79IPP_FINISHINGS_STAPLE = 4
80IPP_FINISHINGS_PUNCH = 5
81IPP_FINISHINGS_COVER = 6
82IPP_FINISHINGS_BIND = 7
83IPP_FINISHINGS_SADDLE_STITCH = 8
84IPP_FINISHINGS_EDGE_STITCH = 9
85IPP_FINISHINGS_FOLD = 10
86IPP_FINISHINGS_TRIM = 11
87IPP_FINISHINGS_BALE = 12
88IPP_FINISHINGS_BOOKLET_MAKER = 13
89IPP_FINISHINGS_JOB_OFFSET = 14
90IPP_FINISHINGS_STAPLE_TOP_LEFT = 20
91IPP_FINISHINGS_STAPLE_BOTTOM_LEFT = 21
92IPP_FINISHINGS_STAPLE_TOP_RIGHT = 22
93IPP_FINISHINGS_STAPLE_BOTTOM_RIGHT = 23
94IPP_FINISHINGS_EDGE_STITCH_LEFT = 24
95IPP_FINISHINGS_EDGE_STITCH_TOP = 25
96IPP_FINISHINGS_EDGE_STITCH_RIGHT = 26
97IPP_FINISHINGS_EDGE_STITCH_BOTTOM = 27
98IPP_FINISHINGS_STAPLE_DUAL_LEFT = 28
99IPP_FINISHINGS_STAPLE_DUAL_TOP = 29
100IPP_FINISHINGS_STAPLE_DUAL_RIGHT = 30
101IPP_FINISHINGS_STAPLE_DUAL_BOTTOM = 31
102IPP_FINISHINGS_BIND_LEFT = 50
103IPP_FINISHINGS_BIND_TOP = 51
104IPP_FINISHINGS_BIND_RIGHT = 52
105IPP_FINISHINGS_BIND_BOTTO = 53
106
107IPP_PORTRAIT = 3
108IPP_LANDSCAPE = 4
109IPP_REVERSE_LANDSCAPE = 5
110IPP_REVERSE_PORTRAIT = 6
111
112IPP_QUALITY_DRAFT = 3
113IPP_QUALITY_NORMAL = 4
114IPP_QUALITY_HIGH = 5
115
116IPP_JOB_PENDING = 3
117IPP_JOB_HELD = 4
118IPP_JOB_PROCESSING = 5
119IPP_JOB_STOPPED = 6
120IPP_JOB_CANCELLED = 7
121IPP_JOB_ABORTED = 8
122IPP_JOB_COMPLETE = 9
123
124IPP_PRINTER_IDLE = 3
125IPP_PRINTER_PROCESSING = 4
126IPP_PRINTER_STOPPED = 5
127
128IPP_ERROR = -1
129IPP_IDLE = 0
130IPP_HEADER = 1
131IPP_ATTRIBUTE = 2
132IPP_DATA = 3
133
134IPP_PRINT_JOB = 0x0002
135IPP_PRINT_URI = 0x0003
136IPP_VALIDATE_JOB = 0x0004
137IPP_CREATE_JOB = 0x0005
138IPP_SEND_DOCUMENT = 0x0006
139IPP_SEND_URI = 0x0007
140IPP_CANCEL_JOB = 0x0008
141IPP_GET_JOB_ATTRIBUTES = 0x0009
142IPP_GET_JOBS = 0x000a
143IPP_GET_PRINTER_ATTRIBUTES = 0x000b
144IPP_HOLD_JOB = 0x000c
145IPP_RELEASE_JOB = 0x000d
146IPP_RESTART_JOB = 0x000e
147IPP_PAUSE_PRINTER = 0x0010
148IPP_RESUME_PRINTER = 0x0011
149IPP_PURGE_JOBS = 0x0012
150IPP_SET_PRINTER_ATTRIBUTES = 0x0013
151IPP_SET_JOB_ATTRIBUTES = 0x0014
152IPP_GET_PRINTER_SUPPORTED_VALUES = 0x0015
153IPP_CREATE_PRINTER_SUBSCRIPTION = 0x0016
154IPP_CREATE_JOB_SUBSCRIPTION = 0x0017
155IPP_GET_SUBSCRIPTION_ATTRIBUTES = 0x0018
156IPP_GET_SUBSCRIPTIONS = 0x0019
157IPP_RENEW_SUBSCRIPTION = 0x001a
158IPP_CANCEL_SUBSCRIPTION = 0x001b
159IPP_GET_NOTIFICATIONS = 0x001c
160IPP_SEND_NOTIFICATIONS = 0x001d
161IPP_GET_PRINT_SUPPORT_FILES = 0x0021
162IPP_ENABLE_PRINTER = 0x0022
163IPP_DISABLE_PRINTER = 0x0023
164IPP_PAUSE_PRINTER_AFTER_CURRENT_JOB = 0x0024
165IPP_HOLD_NEW_JOBS = 0x0025
166IPP_RELEASE_HELD_NEW_JOBS = 0x0026
167IPP_DEACTIVATE_PRINTER = 0x0027
168IPP_ACTIVATE_PRINTER = 0x0028
169IPP_RESTART_PRINTER = 0x0029
170IPP_SHUTDOWN_PRINTER = 0x002a
171IPP_STARTUP_PRINTER = 0x002b
172IPP_REPROCESS_JOB = 0x002c
173IPP_CANCEL_CURRENT_JOB = 0x002d
174IPP_SUSPEND_CURRENT_JOB = 0x002e
175IPP_RESUME_JOB = 0x002f
176IPP_PROMOTE_JOB = 0x0030
177IPP_SCHEDULE_JOB_AFTER = 0x0031
178IPP_PRIVATE = 0x4000
179CUPS_GET_DEFAULT = 0x4001
180CUPS_GET_PRINTERS = 0x4002
181CUPS_ADD_PRINTER = 0x4003
182CUPS_DELETE_PRINTER = 0x4004
183CUPS_GET_CLASSES = 0x4005
184CUPS_ADD_CLASS = 0x4006
185CUPS_DELETE_CLASS = 0x4007
186CUPS_ACCEPT_JOBS = 0x4008
187CUPS_REJECT_JOBS = 0x4009
188CUPS_SET_DEFAULT = 0x400a
189CUPS_GET_DEVICES = 0x400b
190CUPS_GET_PPDS = 0x400c
191CUPS_MOVE_JOB = 0x400d
192CUPS_AUTHENTICATE_JOB = 0x400e
[39]193CUPS_GET_PPD = 0x400f
194CUPS_GET_DOCUMENT = 0x4027
[3]195
196IPP_OK = 0x0000
197IPP_OK_SUBST = 0x0001
198IPP_OK_CONFLICT = 0x0002
199IPP_OK_IGNORED_SUBSCRIPTIONS = 0x0003
200IPP_OK_IGNORED_NOTIFICATIONS = 0x0004
201IPP_OK_TOO_MANY_EVENTS = 0x0005
202IPP_OK_BUT_CANCEL_SUBSCRIPTION = 0x0006
203IPP_REDIRECTION_OTHER_SITE = 0x0300
204IPP_BAD_REQUEST = 0x0400
205IPP_FORBIDDEN = 0x0401
206IPP_NOT_AUTHENTICATED = 0x0402
207IPP_NOT_AUTHORIZED = 0x0403
208IPP_NOT_POSSIBLE = 0x0404
209IPP_TIMEOUT = 0x0405
210IPP_NOT_FOUND = 0x0406
211IPP_GONE = 0x0407
212IPP_REQUEST_ENTITY = 0x0408
213IPP_REQUEST_VALUE = 0x0409
214IPP_DOCUMENT_FORMAT = 0x040a
215IPP_ATTRIBUTES = 0x040b
216IPP_URI_SCHEME = 0x040c
217IPP_CHARSET = 0x040d
218IPP_CONFLICT = 0x040e
219IPP_COMPRESSION_NOT_SUPPORTED = 0x040f
220IPP_COMPRESSION_ERROR = 0x0410
221IPP_DOCUMENT_FORMAT_ERROR = 0x0411
222IPP_DOCUMENT_ACCESS_ERROR = 0x0412
223IPP_ATTRIBUTES_NOT_SETTABLE = 0x0413
224IPP_IGNORED_ALL_SUBSCRIPTIONS = 0x0414
225IPP_TOO_MANY_SUBSCRIPTIONS = 0x0415
226IPP_IGNORED_ALL_NOTIFICATIONS = 0x0416
227IPP_PRINT_SUPPORT_FILE_NOT_FOUND = 0x0417
228
229IPP_INTERNAL_ERROR = 0x0500
230IPP_OPERATION_NOT_SUPPORTED = 0x0501
231IPP_SERVICE_UNAVAILABLE = 0x0502
232IPP_VERSION_NOT_SUPPORTED = 0x0503
233IPP_DEVICE_ERROR = 0x0504
234IPP_TEMPORARY_ERROR = 0x0505
235IPP_NOT_ACCEPTING = 0x0506
236IPP_PRINTER_BUSY = 0x0507
237IPP_ERROR_JOB_CANCELLED = 0x0508
238IPP_MULTIPLE_JOBS_NOT_SUPPORTED = 0x0509
239IPP_PRINTER_IS_DEACTIVATED = 0x50a
240 
[26]241CUPS_PRINTER_LOCAL = 0x0000
242CUPS_PRINTER_CLASS = 0x0001
243CUPS_PRINTER_REMOTE = 0x0002
244CUPS_PRINTER_BW = 0x0004
245CUPS_PRINTER_COLOR = 0x0008
246CUPS_PRINTER_DUPLEX = 0x0010
247CUPS_PRINTER_STAPLE = 0x0020
248CUPS_PRINTER_COPIES = 0x0040
249CUPS_PRINTER_COLLATE = 0x0080
250CUPS_PRINTER_PUNCH = 0x0100
251CUPS_PRINTER_COVER = 0x0200
252CUPS_PRINTER_BIND = 0x0400
253CUPS_PRINTER_SORT = 0x0800
254CUPS_PRINTER_SMALL = 0x1000
255CUPS_PRINTER_MEDIUM = 0x2000
256CUPS_PRINTER_LARGE = 0x4000
257CUPS_PRINTER_VARIABLE = 0x8000
258CUPS_PRINTER_IMPLICIT = 0x1000
259CUPS_PRINTER_DEFAULT = 0x2000
260CUPS_PRINTER_FAX = 0x4000
261CUPS_PRINTER_REJECTING = 0x8000
262CUPS_PRINTER_DELETE = 0x1000
263CUPS_PRINTER_NOT_SHARED = 0x2000
264CUPS_PRINTER_AUTHENTICATED = 0x4000
265CUPS_PRINTER_COMMANDS = 0x8000
266CUPS_PRINTER_OPTIONS = 0xe6ff
267 
268 
[3]269class IPPError(Exception) :
270    """An exception for IPP related stuff."""
271    def __init__(self, message = ""):
272        self.message = message
273        Exception.__init__(self, message)
274    def __repr__(self):
275        return self.message
276    __str__ = __repr__
277
[7]278class FakeAttribute :
279    """Fakes an IPPRequest attribute to simplify usage syntax."""
280    def __init__(self, request, name) :
281        """Initializes the fake attribute."""
282        self.request = request
283        self.name = name
284       
285    def __setitem__(self, key, value) :
286        """Appends the value to the real attribute."""
287        attributeslist = getattr(self.request, "_%s_attributes" % self.name)
288        for i in range(len(attributeslist)) :
289            attribute = attributeslist[i]
290            for j in range(len(attribute)) :
291                (attrname, attrvalue) = attribute[j]
292                if attrname == key :
293                    attribute[j][1].append(value)
294                    return
295            attribute.append((key, [value]))       
296           
297    def __getitem__(self, key) :
298        """Returns an attribute's value."""
[26]299        answer = []
[7]300        attributeslist = getattr(self.request, "_%s_attributes" % self.name)
301        for i in range(len(attributeslist)) :
302            attribute = attributeslist[i]
303            for j in range(len(attribute)) :
304                (attrname, attrvalue) = attribute[j]
305                if attrname == key :
[26]306                    answer.extend(attrvalue)
307        if answer :
308            return answer
[7]309        raise KeyError, key           
310   
[3]311class IPPRequest :
312    """A class for IPP requests."""
313    attributes_types = ("operation", "job", "printer", "unsupported", \
314                                     "subscription", "event_notification")
315    def __init__(self, data="", version=IPP_VERSION, 
316                                operation_id=None, \
[7]317                                request_id=None, \
318                                debug=False) :
[3]319        """Initializes an IPP Message object.
320       
321           Parameters :
322           
323             data : the complete IPP Message's content.
324             debug : a boolean value to output debug info on stderr.
325        """
326        self.debug = debug
327        self._data = data
[7]328        self.parsed = False
[3]329       
330        # Initializes message
331        self.setVersion(version)               
332        self.setOperationId(operation_id)
333        self.setRequestId(request_id)
334        self.data = ""
335       
336        for attrtype in self.attributes_types :
[7]337            setattr(self, "_%s_attributes" % attrtype, [[]])
[3]338       
339        # Initialize tags   
340        self.tags = [ None ] * 256 # by default all tags reserved
341       
342        # Delimiter tags
343        self.tags[0x01] = "operation-attributes-tag"
344        self.tags[0x02] = "job-attributes-tag"
345        self.tags[0x03] = "end-of-attributes-tag"
346        self.tags[0x04] = "printer-attributes-tag"
347        self.tags[0x05] = "unsupported-attributes-tag"
348        self.tags[0x06] = "subscription-attributes-tag"
[27]349        self.tags[0x07] = "event_notification-attributes-tag"
[3]350       
351        # out of band values
352        self.tags[0x10] = "unsupported"
353        self.tags[0x11] = "reserved-for-future-default"
354        self.tags[0x12] = "unknown"
355        self.tags[0x13] = "no-value"
356        self.tags[0x15] = "not-settable"
357        self.tags[0x16] = "delete-attribute"
358        self.tags[0x17] = "admin-define"
359 
360        # integer values
361        self.tags[0x20] = "generic-integer"
362        self.tags[0x21] = "integer"
363        self.tags[0x22] = "boolean"
364        self.tags[0x23] = "enum"
365       
366        # octetString
367        self.tags[0x30] = "octetString-with-an-unspecified-format"
368        self.tags[0x31] = "dateTime"
369        self.tags[0x32] = "resolution"
370        self.tags[0x33] = "rangeOfInteger"
371        self.tags[0x34] = "begCollection" # TODO : find sample files for testing
372        self.tags[0x35] = "textWithLanguage"
373        self.tags[0x36] = "nameWithLanguage"
374        self.tags[0x37] = "endCollection"
375       
376        # character strings
377        self.tags[0x40] = "generic-character-string"
378        self.tags[0x41] = "textWithoutLanguage"
379        self.tags[0x42] = "nameWithoutLanguage"
380        self.tags[0x44] = "keyword"
381        self.tags[0x45] = "uri"
382        self.tags[0x46] = "uriScheme"
383        self.tags[0x47] = "charset"
384        self.tags[0x48] = "naturalLanguage"
385        self.tags[0x49] = "mimeMediaType"
386        self.tags[0x4a] = "memberAttrName"
387       
388        # Reverse mapping to generate IPP messages
389        self.tagvalues = {}
390        for i in range(len(self.tags)) :
391            value = self.tags[i]
392            if value is not None :
393                self.tagvalues[value] = i
394                                     
[7]395    def __getattr__(self, name) :                                 
396        """Fakes attribute access."""
397        if name in self.attributes_types :
398            return FakeAttribute(self, name)
399        else :
400            raise AttributeError, name
401           
[3]402    def __str__(self) :       
403        """Returns the parsed IPP message in a readable form."""
404        if not self.parsed :
405            return ""
406        mybuffer = []
407        mybuffer.append("IPP version : %s.%s" % self.version)
408        mybuffer.append("IPP operation Id : 0x%04x" % self.operation_id)
409        mybuffer.append("IPP request Id : 0x%08x" % self.request_id)
410        for attrtype in self.attributes_types :
[7]411            for attribute in getattr(self, "_%s_attributes" % attrtype) :
[3]412                if attribute :
413                    mybuffer.append("%s attributes :" % attrtype.title())
414                for (name, value) in attribute :
415                    mybuffer.append("  %s : %s" % (name, value))
416        if self.data :           
417            mybuffer.append("IPP datas : %s" % repr(self.data))
418        return "\n".join(mybuffer)
419       
420    def logDebug(self, msg) :   
421        """Prints a debug message."""
422        if self.debug :
423            sys.stderr.write("%s\n" % msg)
424            sys.stderr.flush()
425           
426    def setVersion(self, version) :
427        """Sets the request's operation id."""
428        if version is not None :
429            try :
430                self.version = [int(p) for p in version.split(".")]
431            except AttributeError :
432                if len(version) == 2 : # 2-tuple
433                    self.version = version
434                else :   
435                    try :
436                        self.version = [int(p) for p in str(float(version)).split(".")]
437                    except :
438                        self.version = [int(p) for p in IPP_VERSION.split(".")]
439       
440    def setOperationId(self, opid) :       
441        """Sets the request's operation id."""
442        self.operation_id = opid
443       
444    def setRequestId(self, reqid) :       
445        """Sets the request's request id."""
446        self.request_id = reqid
447       
448    def dump(self) :   
449        """Generates an IPP Message.
450       
451           Returns the message as a string of text.
452        """   
453        mybuffer = []
[7]454        if None not in (self.version, self.operation_id) :
[3]455            mybuffer.append(chr(self.version[0]) + chr(self.version[1]))
456            mybuffer.append(pack(">H", self.operation_id))
[25]457            mybuffer.append(pack(">I", self.request_id or 1))
[3]458            for attrtype in self.attributes_types :
[7]459                for attribute in getattr(self, "_%s_attributes" % attrtype) :
[3]460                    if attribute :
461                        mybuffer.append(chr(self.tagvalues["%s-attributes-tag" % attrtype]))
462                    for (attrname, value) in attribute :
463                        nameprinted = 0
464                        for (vtype, val) in value :
465                            mybuffer.append(chr(self.tagvalues[vtype]))
466                            if not nameprinted :
467                                mybuffer.append(pack(">H", len(attrname)))
468                                mybuffer.append(attrname)
469                                nameprinted = 1
470                            else :     
471                                mybuffer.append(pack(">H", 0))
472                            if vtype in ("integer", "enum") :
473                                mybuffer.append(pack(">H", 4))
474                                mybuffer.append(pack(">I", val))
475                            elif vtype == "boolean" :
476                                mybuffer.append(pack(">H", 1))
477                                mybuffer.append(chr(val))
478                            else :   
479                                mybuffer.append(pack(">H", len(val)))
480                                mybuffer.append(val)
481            mybuffer.append(chr(self.tagvalues["end-of-attributes-tag"]))
482        mybuffer.append(self.data)   
483        return "".join(mybuffer)
484           
485    def parse(self) :
486        """Parses an IPP Request.
487       
488           NB : Only a subset of RFC2910 is implemented.
489        """
490        self._curname = None
491        self._curattributes = None
492       
493        self.setVersion((ord(self._data[0]), ord(self._data[1])))
494        self.setOperationId(unpack(">H", self._data[2:4])[0])
495        self.setRequestId(unpack(">I", self._data[4:8])[0])
496        self.position = 8
497        endofattributes = self.tagvalues["end-of-attributes-tag"]
[27]498        maxdelimiter = self.tagvalues["event_notification-attributes-tag"]
[3]499        nulloffset = lambda : 0
500        try :
501            tag = ord(self._data[self.position])
502            while tag != endofattributes :
503                self.position += 1
504                name = self.tags[tag]
505                if name is not None :
506                    func = getattr(self, name.replace("-", "_"), nulloffset)
507                    self.position += func()
508                    if ord(self._data[self.position]) > maxdelimiter :
509                        self.position -= 1
510                        continue
511                oldtag = tag       
512                tag = ord(self._data[self.position])
513                if tag == oldtag :
514                    self._curattributes.append([])
515        except IndexError :
516            raise IPPError, "Unexpected end of IPP message."
517           
518        self.data = self._data[self.position+1:]           
[7]519        self.parsed = True
[3]520       
521    def parseTag(self) :   
522        """Extracts information from an IPP tag."""
523        pos = self.position
524        tagtype = self.tags[ord(self._data[pos])]
525        pos += 1
526        posend = pos2 = pos + 2
527        namelength = unpack(">H", self._data[pos:pos2])[0]
528        if not namelength :
529            name = self._curname
530        else :   
531            posend += namelength
532            self._curname = name = self._data[pos2:posend]
533        pos2 = posend + 2
534        valuelength = unpack(">H", self._data[posend:pos2])[0]
535        posend = pos2 + valuelength
536        value = self._data[pos2:posend]
537        if tagtype in ("integer", "enum") :
538            value = unpack(">I", value)[0]
539        elif tagtype == "boolean" :   
540            value = ord(value)
541        try :   
542            (oldname, oldval) = self._curattributes[-1][-1]
543            if oldname == name :
544                oldval.append((tagtype, value))
545            else :   
546                raise IndexError
547        except IndexError :   
548            self._curattributes[-1].append((name, [(tagtype, value)]))
549        self.logDebug("%s(%s) : %s" % (name, tagtype, value))
550        return posend - self.position
551       
552    def operation_attributes_tag(self) : 
553        """Indicates that the parser enters into an operation-attributes-tag group."""
[7]554        self._curattributes = self._operation_attributes
[3]555        return self.parseTag()
556       
557    def job_attributes_tag(self) : 
558        """Indicates that the parser enters into a job-attributes-tag group."""
[7]559        self._curattributes = self._job_attributes
[3]560        return self.parseTag()
561       
562    def printer_attributes_tag(self) : 
563        """Indicates that the parser enters into a printer-attributes-tag group."""
[7]564        self._curattributes = self._printer_attributes
[3]565        return self.parseTag()
566       
567    def unsupported_attributes_tag(self) : 
568        """Indicates that the parser enters into an unsupported-attributes-tag group."""
[7]569        self._curattributes = self._unsupported_attributes
[3]570        return self.parseTag()
571       
572    def subscription_attributes_tag(self) : 
573        """Indicates that the parser enters into a subscription-attributes-tag group."""
[7]574        self._curattributes = self._subscription_attributes
[3]575        return self.parseTag()
576       
577    def event_notification_attributes_tag(self) : 
578        """Indicates that the parser enters into an event-notification-attributes-tag group."""
[7]579        self._curattributes = self._event_notification_attributes
[3]580        return self.parseTag()
581       
[21]582           
[7]583class CUPS :
584    """A class for a CUPS instance."""
[25]585    def __init__(self, url=None, username=None, password=None, charset="utf-8", language="en-us", debug=False) :
[7]586        """Initializes the CUPS instance."""
[25]587        if url is not None :
588            self.url = url.replace("ipp://", "http://")
589            if self.url.endswith("/") :
590                self.url = self.url[:-1]
591        else :       
592            self.url = self.getDefaultURL()
[7]593        self.username = username
594        self.password = password
595        self.charset = charset
596        self.language = language
[25]597        self.debug = debug
[23]598        self.lastError = None
599        self.lastErrorMessage = None
[25]600        self.requestId = None
[7]601       
[25]602    def getDefaultURL(self) :   
603        """Builds a default URL."""
604        # TODO : encryption methods.
605        server = os.environ.get("CUPS_SERVER") or "localhost"
606        port = os.environ.get("IPP_PORT") or 631
607        if server.startswith("/") :
608            # it seems it's a unix domain socket.
609            # we can't handle this right now, so we use the default instead.
610            return "http://localhost:%s" % port
611        else :   
612            return "http://%s:%s" % (server, port)
613           
[9]614    def identifierToURI(self, service, ident) :
615        """Transforms an identifier into a particular URI depending on requested service."""
616        return "%s/%s/%s" % (self.url.replace("http://", "ipp://"),
617                             service,
618                             ident)
619       
[25]620    def nextRequestId(self) :       
621        """Increments the current request id and returns the new value."""
622        try :
623            self.requestId += 1
624        except TypeError :   
625            self.requestId = 1
626        return self.requestId
627           
[7]628    def newRequest(self, operationid=None) :
629        """Generates a new empty request."""
630        if operationid is not None :
631            req = IPPRequest(operation_id=operationid, \
[25]632                             request_id=self.nextRequestId(), \
633                             debug=self.debug)
[7]634            req.operation["attributes-charset"] = ("charset", self.charset)
635            req.operation["attributes-natural-language"] = ("naturalLanguage", self.language)
636            return req
637   
[26]638    def doRequest(self, req, url=None) :
[25]639        """Sends a request to the CUPS server.
640           returns a new IPPRequest object, containing the parsed answer.
641        """   
[26]642        connexion = urllib2.Request(url=url or self.url, \
[25]643                             data=req.dump())
644        connexion.add_header("Content-Type", "application/ipp")
645        if self.username :
646            pwmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
647            pwmanager.add_password(None, \
648                                   "%s%s" % (connexion.get_host(), connexion.get_selector()), \
649                                   self.username, \
650                                   self.password or "")
651            authhandler = urllib2.HTTPBasicAuthHandler(pwmanager)                       
652            opener = urllib2.build_opener(authhandler)
653            urllib2.install_opener(opener)
654        self.lastError = None   
655        self.lastErrorMessage = None
656        try :   
657            response = urllib2.urlopen(connexion)
658        except (urllib2.URLError, urllib2.HTTPError, socket.error), error :   
659            self.lastError = error
660            self.lastErrorMessage = str(error)
661            return None
[24]662        else :   
[25]663            datas = response.read()
664            ippresponse = IPPRequest(datas)
665            ippresponse.parse()
666            return ippresponse
667   
[26]668    def getPPD(self, queuename) :   
669        """Retrieves the PPD for a particular queuename."""
670        req = self.newRequest(IPP_GET_PRINTER_ATTRIBUTES)
671        req.operation["printer-uri"] = ("uri", self.identifierToURI("printers", queuename))
672        for attrib in ("printer-uri-supported", "printer-type", "member-uris") :
673            req.operation["requested-attributes"] = ("nameWithoutLanguage", attrib)
674        return self.doRequest(req)  # TODO : get the PPD from the actual print server
675       
[7]676    def getDefault(self) :
677        """Retrieves CUPS' default printer."""
[23]678        return self.doRequest(self.newRequest(CUPS_GET_DEFAULT))
[7]679   
680    def getJobAttributes(self, jobid) :   
681        """Retrieves a print job's attributes."""
682        req = self.newRequest(IPP_GET_JOB_ATTRIBUTES)
[9]683        req.operation["job-uri"] = ("uri", self.identifierToURI("jobs", jobid))
[23]684        return self.doRequest(req)
[7]685       
[26]686    def getPrinters(self) :   
687        """Returns the list of print queues names."""
688        req = self.newRequest(CUPS_GET_PRINTERS)
689        req.operation["requested-attributes"] = ("keyword", "printer-name")
690        req.operation["printer-type"] = ("enum", 0)
691        req.operation["printer-type-mask"] = ("enum", CUPS_PRINTER_CLASS)
692        return [printer[1] for printer in self.doRequest(req).printer["printer-name"]]
[9]693       
[26]694    def getDevices(self) :   
695        """Returns a list of devices as (deviceclass, deviceinfo, devicemakeandmodel, deviceuri) tuples."""
696        answer = self.doRequest(self.newRequest(CUPS_GET_DEVICES))
697        return zip([d[1] for d in answer.printer["device-class"]], \
698                   [d[1] for d in answer.printer["device-info"]], \
699                   [d[1] for d in answer.printer["device-make-and-model"]], \
700                   [d[1] for d in answer.printer["device-uri"]])
701                   
702    def getPPDs(self) :   
703        """Returns a list of PPDs as (ppdnaturallanguage, ppdmake, ppdmakeandmodel, ppdname) tuples."""
704        answer = self.doRequest(self.newRequest(CUPS_GET_PPDS))
705        return zip([d[1] for d in answer.printer["ppd-natural-language"]], \
706                   [d[1] for d in answer.printer["ppd-make"]], \
707                   [d[1] for d in answer.printer["ppd-make-and-model"]], \
708                   [d[1] for d in answer.printer["ppd-name"]])
[28]709                   
710    def createSubscription(self, uri, events=["all"],
711                                      userdata=None,
712                                      recipient=None,
713                                      pullmethod=None,
714                                      charset=None,
715                                      naturallanguage=None,
716                                      leaseduration=None,
717                                      timeinterval=None,
718                                      jobid=None) :
[35]719        """Creates a job, printer or server subscription.
720         
721           uri : the subscription's uri, e.g. ipp://server
722           events : a list of events to subscribe to, e.g. ["printer-added", "printer-deleted"]
723           recipient : the notifier's uri
724           pullmethod : the pull method to use
725           charset : the charset to use when sending notifications
726           naturallanguage : the language to use when sending notifications
727           leaseduration : the duration of the lease in seconds
728           timeinterval : the interval of time during notifications
729           jobid : the optional job id in case of a job subscription
730        """   
[30]731        if jobid is not None :
732            opid = IPP_CREATE_JOB_SUBSCRIPTION
733            uritype = "job-uri"
734        else :
735            opid = IPP_CREATE_PRINTER_SUBSCRIPTION
736            uritype = "printer-uri"
737        req = self.newRequest(opid)
738        req.operation[uritype] = ("uri", uri)
[28]739        for event in events :
740            req.subscription["notify-events"] = ("keyword", event)
741        if userdata is not None :   
742            req.subscription["notify-user-data"] = ("octetString-with-an-unspecified-format", userdata)
743        if recipient is not None :   
744            req.subscription["notify-recipient"] = ("uri", recipient)
745        if pullmethod is not None :
746            req.subscription["notify-pull-method"] = ("keyword", pullmethod)
747        if charset is not None :
748            req.subscription["notify-charset"] = ("charset", charset)
749        if naturallanguage is not None :
750            req.subscription["notify-natural-language"] = ("naturalLanguage", naturallanguage)
751        if leaseduration is not None :
752            req.subscription["notify-lease-duration"] = ("integer", leaseduration)
753        if timeinterval is not None :
754            req.subscription["notify-time-interval"] = ("integer", timeinterval)
755        if jobid is not None :
756            req.subscription["notify-job-id"] = ("integer", jobid)
757        return self.doRequest(req)
[7]758           
[30]759    def cancelSubscription(self, uri, subscriptionid, jobid=None) :   
[35]760        """Cancels a subscription.
761       
762           uri : the subscription's uri.
763           subscriptionid : the subscription's id.
764           jobid : the optional job's id.
765        """
[30]766        req = self.newRequest(IPP_CANCEL_SUBSCRIPTION)
767        if jobid is not None :
768            uritype = "job-uri"
769        else :
770            uritype = "printer-uri"
771        req.operation[uritype] = ("uri", uri)
772        req.event_notification["notify-subscription-id"] = ("integer", subscriptionid)
773        return self.doRequest(req)
[28]774       
[3]775if __name__ == "__main__" :           
776    if (len(sys.argv) < 2) or (sys.argv[1] == "--debug") :
[23]777        print "usage : python pkipplib.py /var/spool/cups/c00005 [--debug] (for example)\n"
[3]778    else :   
779        infile = open(sys.argv[1], "rb")
[25]780        filedata = infile.read()
[3]781        infile.close()
782       
[25]783        msg = IPPRequest(filedata, debug=(sys.argv[-1]=="--debug"))
784        msg.parse()
785        msg2 = IPPRequest(msg.dump())
786        msg2.parse()
787        filedata2 = msg2.dump()
[3]788       
[25]789        if filedata == filedata2 :
[3]790            print "Test OK : parsing original and parsing the output of the dump produce the same dump !"
[25]791            print str(msg)
[3]792        else :   
793            print "Test Failed !"
[25]794            print str(msg)
[3]795            print
[25]796            print str(msg2)
[3]797       
Note: See TracBrowser for help on using the browser.