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

Revision 3249, 3.9 kB (checked in by jerome, 17 years ago)

Removed ipp module which was still present in PyKota. Now pkipplib
is mandatory.
Added a module to handle cups dialoging and datas.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2# -*- coding: ISO-8859-15 -*-
3#
4# PyKota : Print Quotas for CUPS
5#
6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
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
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23
24"""This module implements some CUPS specific classes."""
25
26from pykota.tool import PyKotaToolError
27try :
28    from pkipplib import pkipplib
29except ImportError :       
30    raise RuntimeError, "The python-pkipplib module is now mandatory. You can download pkipplib from http://www.pykota.com/"
31
32class Job :   
33    """A class to hold CUPS print job informations."""
34    def __init__(self, jobid=None, copies=1, filename=None) :
35        """Initializes a print job's information."""
36        self.JobId = jobid
37        self.Copies = copies
38        self.FileName = filename
39        self.Charset = None
40        self.UserName = None
41        self.Title = None
42        self.BillingCode = None
43        self.OriginatingHostName = None
44        self.TimeAtCreation = None
45        self.TimeAtProcessing = None
46        self.MimeType = None
47        self.UUID = None
48        if self.JobId is not None :
49            self.retrieveAttributesFromCUPS()
50       
51    def getAttributeTypeAndValue(self, ippanswer, attribute, category="job") :   
52        """Retrieves a particular attribute's type and value from an IPP answer.
53       
54           Returns a tuple of the form (type, value).
55        """
56        try :
57            return getattr(ippanswer, category)[attribute][0]
58        except KeyError :   
59            return (None, None)
60           
61    def retrieveAttributesFromCUPS(self) :
62        """Retrieve attribute's values from CUPS."""
63        server = pkipplib.CUPS() # TODO : username and password and/or encryption
64        answer = server.getJobAttributes(self.JobId)
65        if answer is None :
66            raise PyKotaToolError, "Network error while querying the CUPS server : %s" \
67                                      % server.lastErrorMessage
68        (dummy, self.Charset) = self.getAttributeTypeAndValue(answer, "attributes-charset", "operation")                             
69        (dummy, self.UserName) = self.getAttributeTypeAndValue(answer, "job-originating-user-name")
70        (dummy, self.Title) = self.getAttributeTypeAndValue(answer, "job-name")
71        (dummy, self.BillingCode) = self.getAttributeTypeAndValue(answer, "job-billing")
72        (dummy, self.OriginatingHostName) = self.getAttributeTypeAndValue(answer, "job-originating-host-name")
73        (dummy, self.UUID) = self.getAttributeTypeAndValue(answer, "job-uuid")
74        (dummy, self.TimeAtCreation) = self.getAttributeTypeAndValue(answer, "time-at-creation")
75        (dummy, self.TimeAtProcessing) = self.getAttributeTypeAndValue(answer, "time-at-processing")
76        (dummy, self.MimeType) = self.getAttributeTypeAndValue(answer, "document-format")
77   
78if __name__ == "__main__" :   
79    import sys
80    if len(sys.argv) != 2 :
81        sys.stderr.write("usage : python cups.py jobid\n")
82    else :   
83        job = Job(int(sys.argv[1]))
84        for attribute in ("Charset", "JobId", "Copies", "FileName", "UserName", 
85                          "Title", "BillingCode", "OriginatingHostName", 
86                          "TimeAtCreation", "TimeAtProcessing", "UUID",
87                          "MimeType") :
88            sys.stdout.write("%s : %s\n" % (attribute, repr(getattr(job, attribute))))
Note: See TracBrowser for help on using the browser.