root / pykota / trunk / pykota / cups.py @ 3496

Revision 3496, 4.8 kB (checked in by jerome, 15 years ago)

Implemented a workaround for Kerberized usernames. This workaround
should also take care of removing instances from the principal. Now only
the primary is used as the username. IMPORTANT : in the code we
consider the component separator character is always '/' and the realm
separator character is always '@', this may not always be the case.
Fixes #41.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3489]1# -*- coding: utf-8 -*-
[695]2#
[3249]3# PyKota : Print Quotas for CUPS
[695]4#
[3481]5# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
[3260]6# This program is free software: you can redistribute it and/or modify
[873]7# it under the terms of the GNU General Public License as published by
[3260]8# the Free Software Foundation, either version 3 of the License, or
[873]9# (at your option) any later version.
[3413]10#
[873]11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
[3413]15#
[873]16# You should have received a copy of the GNU General Public License
[3260]17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[695]18#
19# $Id$
20#
[3249]21
22"""This module implements some CUPS specific classes."""
23
[3288]24from pykota.errors import PyKotaToolError
[3249]25try :
26    from pkipplib import pkipplib
[3413]27except ImportError :
[3249]28    raise RuntimeError, "The python-pkipplib module is now mandatory. You can download pkipplib from http://www.pykota.com/"
29
[3413]30class JobTicket :
[3249]31    """A class to hold CUPS print job informations."""
[3252]32    def __init__(self, jobid=None, printername=None, copies=1, filename=None, \
33                       options=None) :
[3249]34        """Initializes a print job's information."""
35        self.JobId = jobid
[3252]36        self.PrinterName = printername
[3249]37        self.Copies = copies
38        self.FileName = filename
[3252]39        self.Options = options
[3249]40        self.Charset = None
[3252]41        self.OriginatingUserName = None
42        self.OriginalUserName = None
[3249]43        self.Title = None
44        self.BillingCode = None
45        self.OriginatingHostName = None
46        self.TimeAtCreation = None
47        self.TimeAtProcessing = None
48        self.MimeType = None
49        self.UUID = None
50        if self.JobId is not None :
51            self.retrieveAttributesFromCUPS()
[3413]52
53    def getAttributeTypeAndValue(self, ippanswer, attribute, category="job") :
[3249]54        """Retrieves a particular attribute's type and value from an IPP answer.
[3413]55
[3249]56           Returns a tuple of the form (type, value).
57        """
58        try :
59            return getattr(ippanswer, category)[attribute][0]
[3413]60        except KeyError :
[3249]61            return (None, None)
[3413]62
[3249]63    def retrieveAttributesFromCUPS(self) :
64        """Retrieve attribute's values from CUPS."""
65        server = pkipplib.CUPS() # TODO : username and password and/or encryption
66        answer = server.getJobAttributes(self.JobId)
67        if answer is None :
68            raise PyKotaToolError, "Network error while querying the CUPS server : %s" \
69                                      % server.lastErrorMessage
[3252]70        (dummy, self.Charset) = self.getAttributeTypeAndValue(answer, "attributes-charset", "operation")
71        (dummy, self.OriginatingUserName) = self.getAttributeTypeAndValue(answer, "job-originating-user-name")
[3249]72        (dummy, self.Title) = self.getAttributeTypeAndValue(answer, "job-name")
73        (dummy, self.BillingCode) = self.getAttributeTypeAndValue(answer, "job-billing")
74        (dummy, self.OriginatingHostName) = self.getAttributeTypeAndValue(answer, "job-originating-host-name")
75        (dummy, self.UUID) = self.getAttributeTypeAndValue(answer, "job-uuid")
76        (dummy, self.TimeAtCreation) = self.getAttributeTypeAndValue(answer, "time-at-creation")
77        (dummy, self.TimeAtProcessing) = self.getAttributeTypeAndValue(answer, "time-at-processing")
78        (dummy, self.MimeType) = self.getAttributeTypeAndValue(answer, "document-format")
[3413]79
[3496]80        # handle Kerberized usernames as described here :
81        # http://www.mit.edu/~kerberos/krb5-1.5/krb5-1.5.4/doc/krb5-user/What-is-a-Kerberos-Principal_003f.html
82        self.OriginatingUserName = self.OriginatingUserName.split("@", 1)[0].split("/")[0]
83
[3413]84        for attrib in ("OriginatingUserName",
[3255]85                       "OriginatingHostName",
[3413]86                       "Title",
[3255]87                       "BillingCode",
88                       "PrinterName",
89                       "Options",
90                       "Charset",
91                       "UUID",
92                       "MimeType") :
[3413]93            try :
94                setattr(self, attrib,
[3255]95                              getattr(self, attrib).decode("UTF-8", "replace"))
[3413]96            except AttributeError :
[3255]97                pass
[3413]98
[3252]99        self.OriginalUserName = self.OriginatingUserName[:]
[3413]100
101if __name__ == "__main__" :
[3249]102    import sys
103    if len(sys.argv) != 2 :
104        sys.stderr.write("usage : python cups.py jobid\n")
[3413]105    else :
[3255]106        job = JobTicket(int(sys.argv[1]), "FakePrinter")
[3413]107        for attribute in ("PrinterName", "Charset", "JobId", "Copies",
108                          "FileName", "OriginatingUserName",
109                          "Title", "BillingCode", "OriginatingHostName",
110                          "TimeAtCreation", "TimeAtProcessing", "UUID",
[3249]111                          "MimeType") :
112            sys.stdout.write("%s : %s\n" % (attribute, repr(getattr(job, attribute))))
Note: See TracBrowser for help on using the browser.