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

Revision 3252, 4.3 kB (checked in by jerome, 16 years ago)

Can now print again under limited circumstances.

  • 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 JobTicket :   
33    """A class to hold CUPS print job informations."""
34    def __init__(self, jobid=None, printername=None, copies=1, filename=None, \
35                       options=None) :
36        """Initializes a print job's information."""
37        self.JobId = jobid
38        self.PrinterName = printername
39        self.Copies = copies
40        self.FileName = filename
41        self.Options = options
42        self.Charset = None
43        self.OriginatingUserName = None
44        self.OriginalUserName = None
45        self.Title = None
46        self.BillingCode = None
47        self.OriginatingHostName = None
48        self.TimeAtCreation = None
49        self.TimeAtProcessing = None
50        self.MimeType = None
51        self.UUID = None
52        if self.JobId is not None :
53            self.retrieveAttributesFromCUPS()
54       
55    def getAttributeTypeAndValue(self, ippanswer, attribute, category="job") :   
56        """Retrieves a particular attribute's type and value from an IPP answer.
57       
58           Returns a tuple of the form (type, value).
59        """
60        try :
61            return getattr(ippanswer, category)[attribute][0]
62        except KeyError :   
63            return (None, None)
64           
65    def retrieveAttributesFromCUPS(self) :
66        """Retrieve attribute's values from CUPS."""
67        import os
68        f = open("/tmp/debug", "w")
69        f.write("%s\n" % os.environ.get("CUPS_SERVER", ""))
70        f.close()
71        server = pkipplib.CUPS() # TODO : username and password and/or encryption
72        answer = server.getJobAttributes(self.JobId)
73        if answer is None :
74            raise PyKotaToolError, "Network error while querying the CUPS server : %s" \
75                                      % server.lastErrorMessage
76        (dummy, self.Charset) = self.getAttributeTypeAndValue(answer, "attributes-charset", "operation")
77        (dummy, self.OriginatingUserName) = self.getAttributeTypeAndValue(answer, "job-originating-user-name")
78        (dummy, self.Title) = self.getAttributeTypeAndValue(answer, "job-name")
79        (dummy, self.BillingCode) = self.getAttributeTypeAndValue(answer, "job-billing")
80        (dummy, self.OriginatingHostName) = self.getAttributeTypeAndValue(answer, "job-originating-host-name")
81        (dummy, self.UUID) = self.getAttributeTypeAndValue(answer, "job-uuid")
82        (dummy, self.TimeAtCreation) = self.getAttributeTypeAndValue(answer, "time-at-creation")
83        (dummy, self.TimeAtProcessing) = self.getAttributeTypeAndValue(answer, "time-at-processing")
84        (dummy, self.MimeType) = self.getAttributeTypeAndValue(answer, "document-format")
85        self.OriginalUserName = self.OriginatingUserName[:]
86   
87if __name__ == "__main__" :   
88    import sys
89    if len(sys.argv) != 2 :
90        sys.stderr.write("usage : python cups.py jobid\n")
91    else :   
92        job = JobTicket(int(sys.argv[1]))
93        for attribute in ("Charset", "JobId", "Copies", "FileName", "OriginatingUserName", 
94                          "Title", "BillingCode", "OriginatingHostName", 
95                          "TimeAtCreation", "TimeAtProcessing", "UUID",
96                          "MimeType") :
97            sys.stdout.write("%s : %s\n" % (attribute, repr(getattr(job, attribute))))
Note: See TracBrowser for help on using the browser.