| 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 | |
| 32 | class 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 | |
| 78 | if __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)))) |