[695] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[695] | 3 | # |
---|
[3249] | 4 | # PyKota : Print Quotas for CUPS |
---|
[695] | 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[695] | 11 | # |
---|
[873] | 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 |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[695] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[3249] | 23 | |
---|
| 24 | """This module implements some CUPS specific classes.""" |
---|
| 25 | |
---|
| 26 | from pykota.tool import PyKotaToolError |
---|
| 27 | try : |
---|
| 28 | from pkipplib import pkipplib |
---|
| 29 | except ImportError : |
---|
| 30 | raise RuntimeError, "The python-pkipplib module is now mandatory. You can download pkipplib from http://www.pykota.com/" |
---|
| 31 | |
---|
[3252] | 32 | class JobTicket : |
---|
[3249] | 33 | """A class to hold CUPS print job informations.""" |
---|
[3252] | 34 | def __init__(self, jobid=None, printername=None, copies=1, filename=None, \ |
---|
| 35 | options=None) : |
---|
[3249] | 36 | """Initializes a print job's information.""" |
---|
| 37 | self.JobId = jobid |
---|
[3252] | 38 | self.PrinterName = printername |
---|
[3249] | 39 | self.Copies = copies |
---|
| 40 | self.FileName = filename |
---|
[3252] | 41 | self.Options = options |
---|
[3249] | 42 | self.Charset = None |
---|
[3252] | 43 | self.OriginatingUserName = None |
---|
| 44 | self.OriginalUserName = None |
---|
[3249] | 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.""" |
---|
[3252] | 67 | import os |
---|
| 68 | f = open("/tmp/debug", "w") |
---|
| 69 | f.write("%s\n" % os.environ.get("CUPS_SERVER", "")) |
---|
| 70 | f.close() |
---|
[3249] | 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 |
---|
[3252] | 76 | (dummy, self.Charset) = self.getAttributeTypeAndValue(answer, "attributes-charset", "operation") |
---|
| 77 | (dummy, self.OriginatingUserName) = self.getAttributeTypeAndValue(answer, "job-originating-user-name") |
---|
[3249] | 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") |
---|
[3252] | 85 | self.OriginalUserName = self.OriginatingUserName[:] |
---|
[3249] | 86 | |
---|
| 87 | if __name__ == "__main__" : |
---|
| 88 | import sys |
---|
| 89 | if len(sys.argv) != 2 : |
---|
| 90 | sys.stderr.write("usage : python cups.py jobid\n") |
---|
| 91 | else : |
---|
[3252] | 92 | job = JobTicket(int(sys.argv[1])) |
---|
| 93 | for attribute in ("Charset", "JobId", "Copies", "FileName", "OriginatingUserName", |
---|
[3249] | 94 | "Title", "BillingCode", "OriginatingHostName", |
---|
| 95 | "TimeAtCreation", "TimeAtProcessing", "UUID", |
---|
| 96 | "MimeType") : |
---|
| 97 | sys.stdout.write("%s : %s\n" % (attribute, repr(getattr(job, attribute)))) |
---|