[d04a689] | 1 | from exceptions import InvalidJobException, InvalidPrinterStateException |
---|
[776a659] | 2 | import os |
---|
[d04a689] | 3 | |
---|
| 4 | # initialize logger |
---|
| 5 | logger = logging.getLogger(__name__) |
---|
[776a659] | 6 | |
---|
| 7 | class Job(object): |
---|
| 8 | |
---|
[1a63bf7] | 9 | # for IPP |
---|
[b2e077a] | 10 | attributes = [ |
---|
| 11 | "job-id", |
---|
| 12 | "job-name", |
---|
| 13 | "job-originating-user-name", |
---|
| 14 | "job-k-octets", |
---|
| 15 | "job-state", |
---|
| 16 | "job-printer-uri" |
---|
[1a63bf7] | 17 | ] |
---|
[b2e077a] | 18 | |
---|
[776a659] | 19 | def __init__(self, document=None): |
---|
| 20 | """Initialize a Gutenbach job. |
---|
| 21 | |
---|
| 22 | This sets the status to 'initializing' and optionally sets the |
---|
| 23 | document to print to the value of document. |
---|
| 24 | """ |
---|
| 25 | |
---|
[1a63bf7] | 26 | self.jid = None |
---|
| 27 | self.name = document |
---|
| 28 | self.status = None |
---|
| 29 | self.document = document |
---|
| 30 | self.printer = None |
---|
| 31 | |
---|
| 32 | def __getattr__(self, attr): |
---|
| 33 | try: |
---|
| 34 | return super(Job, self).__getattr__(attr) |
---|
| 35 | except AttributeError: |
---|
| 36 | pass |
---|
| 37 | |
---|
| 38 | return super(Job, self).__getattr__( |
---|
| 39 | attr.replace("-", "_")) |
---|
| 40 | |
---|
| 41 | def __hasattr__(self, attr): |
---|
| 42 | has = super(Job, self).__hasattr__(attr) |
---|
| 43 | if not has: |
---|
| 44 | has = super(Job, self).__hasattr__( |
---|
| 45 | attr.replace("-", "_")) |
---|
| 46 | return has |
---|
| 47 | |
---|
| 48 | #### Job attributes |
---|
[776a659] | 49 | |
---|
| 50 | @property |
---|
[b2e077a] | 51 | def job_id(self): |
---|
[1a63bf7] | 52 | return self.jid |
---|
[b2e077a] | 53 | |
---|
| 54 | @property |
---|
| 55 | def job_name(self): |
---|
[1a63bf7] | 56 | return self.name |
---|
[776a659] | 57 | |
---|
[b2e077a] | 58 | # XXX: we need to actually calculate this! |
---|
| 59 | @property |
---|
| 60 | def job_originating_user_name(self): |
---|
[1a63bf7] | 61 | return "jhamrick" |
---|
[776a659] | 62 | |
---|
[b2e077a] | 63 | # XXX: we need to actually calculate this! |
---|
| 64 | @property |
---|
| 65 | def job_k_octets(self): |
---|
[1a63bf7] | 66 | return "job-k-octets" |
---|
[b2e077a] | 67 | |
---|
| 68 | @property |
---|
| 69 | def job_state(self): |
---|
[1a63bf7] | 70 | return self.status |
---|
[b2e077a] | 71 | |
---|
| 72 | @property |
---|
| 73 | def job_printer_uri(self): |
---|
[1a63bf7] | 74 | return self.printer.uri |
---|
[b2e077a] | 75 | |
---|
| 76 | def get_job_attributes(self, request): |
---|
[1a63bf7] | 77 | attributes = [(attr, getattr(self, attr)) for attr in self.attributes] |
---|
[b2e077a] | 78 | return attributes |
---|
| 79 | |
---|
| 80 | ####### |
---|
[776a659] | 81 | |
---|
[b2e077a] | 82 | def enqueue(self, printer, job_id): |
---|
[1a63bf7] | 83 | if self.status != None: |
---|
[776a659] | 84 | raise InvalidJobException( |
---|
| 85 | "Cannot enqueue a job that has " + \ |
---|
| 86 | "already been initialized!") |
---|
[1a63bf7] | 87 | self.printer = printer |
---|
| 88 | self.jid = job_id |
---|
| 89 | self.status = const.JobStates.PENDING |
---|
[776a659] | 90 | |
---|
| 91 | def play(self): |
---|
| 92 | if self.status != 'active': |
---|
| 93 | raise InvalidJobException( |
---|
| 94 | "Cannot play an inactive job!") |
---|
| 95 | |
---|
[1a63bf7] | 96 | self.status = const.JobStates.PROCESSING |
---|
[776a659] | 97 | # TODO: add external call to music player |
---|
[5d24a81] | 98 | print "Playing job %s" % str(self) |
---|
[1a63bf7] | 99 | self.printer.complete_job(self.jid) |
---|
[776a659] | 100 | |
---|
[5d24a81] | 101 | def finish(self): |
---|
[1a63bf7] | 102 | self.status = const.JobStates.COMPLETE |
---|
[776a659] | 103 | |
---|
| 104 | def __repr__(self): |
---|
| 105 | return str(self) |
---|
| 106 | |
---|
| 107 | def __str__(self): |
---|
[5d24a81] | 108 | return "<Job %d '%s'>" % \ |
---|
[1a63bf7] | 109 | (self.jid if self.jid is not None else -1, \ |
---|
| 110 | self.document) |
---|