[776a659] | 1 | import os |
---|
| 2 | from exceptions import InvalidJobException, \ |
---|
| 3 | InvalidPrinterStateException |
---|
| 4 | |
---|
| 5 | class Job(object): |
---|
| 6 | |
---|
| 7 | def __init__(self, document=None): |
---|
| 8 | """Initialize a Gutenbach job. |
---|
| 9 | |
---|
| 10 | This sets the status to 'initializing' and optionally sets the |
---|
| 11 | document to print to the value of document. |
---|
| 12 | """ |
---|
| 13 | |
---|
| 14 | self._jobid = None |
---|
| 15 | self._status = 'initializing' |
---|
| 16 | self._document = document |
---|
| 17 | self._printer = None |
---|
| 18 | |
---|
| 19 | @property |
---|
| 20 | def jobid(self): |
---|
| 21 | return self._jobid |
---|
| 22 | |
---|
| 23 | @jobid.setter |
---|
| 24 | def jobid(self, val): |
---|
| 25 | raise AttributeError("Setting jobid is illegal!") |
---|
| 26 | |
---|
| 27 | @property |
---|
| 28 | def document(self): |
---|
| 29 | return self._document |
---|
| 30 | |
---|
| 31 | @document.setter |
---|
| 32 | def document(self, path): |
---|
| 33 | if not os.path.exists(path): |
---|
| 34 | raise IOError("Document '%s' does not exist!" % path) |
---|
| 35 | self._document = path |
---|
| 36 | |
---|
| 37 | @property |
---|
| 38 | def status(self): |
---|
| 39 | return self._status |
---|
| 40 | |
---|
| 41 | @status.setter |
---|
| 42 | def status(self, val): |
---|
| 43 | raise AttributeError( |
---|
| 44 | "Setting status directly is illegal! " + \ |
---|
| 45 | "Please use enqueue(), play(), or finish().") |
---|
| 46 | |
---|
| 47 | @property |
---|
| 48 | def printer(self): |
---|
| 49 | return self._printer |
---|
| 50 | |
---|
| 51 | @printer.setter |
---|
| 52 | def printer(self, val): |
---|
| 53 | raise AttributeError( |
---|
| 54 | "Setting printer directly is illegal! " + \ |
---|
| 55 | "Please use enqueue().") |
---|
| 56 | |
---|
[5d24a81] | 57 | def enqueue(self, printer, jobid): |
---|
[776a659] | 58 | if self.status != 'initializing': |
---|
| 59 | raise InvalidJobException( |
---|
| 60 | "Cannot enqueue a job that has " + \ |
---|
| 61 | "already been initialized!") |
---|
| 62 | self._printer = printer |
---|
[5d24a81] | 63 | self._jobid = jobid |
---|
[776a659] | 64 | self._status = 'active' |
---|
| 65 | |
---|
| 66 | def play(self): |
---|
| 67 | if self.status != 'active': |
---|
| 68 | raise InvalidJobException( |
---|
| 69 | "Cannot play an inactive job!") |
---|
| 70 | |
---|
| 71 | self._status = 'playing' |
---|
| 72 | # TODO: add external call to music player |
---|
[5d24a81] | 73 | print "Playing job %s" % str(self) |
---|
[776a659] | 74 | self.printer.complete_job(self.jobid) |
---|
| 75 | |
---|
[5d24a81] | 76 | def finish(self): |
---|
[776a659] | 77 | self._status = 'finished' |
---|
| 78 | |
---|
| 79 | def __repr__(self): |
---|
| 80 | return str(self) |
---|
| 81 | |
---|
| 82 | def __str__(self): |
---|
[5d24a81] | 83 | return "<Job %d '%s'>" % \ |
---|
| 84 | (self.jobid if self.jobid is not None else -1, \ |
---|
| 85 | self.document) |
---|