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