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 | |
---|
57 | def enqueue(self, printer): |
---|
58 | if self.status != 'initializing': |
---|
59 | raise InvalidJobException( |
---|
60 | "Cannot enqueue a job that has " + \ |
---|
61 | "already been initialized!") |
---|
62 | self._printer = printer |
---|
63 | self._status = 'active' |
---|
64 | |
---|
65 | def play(self): |
---|
66 | if self.status != 'active': |
---|
67 | raise InvalidJobException( |
---|
68 | "Cannot play an inactive job!") |
---|
69 | |
---|
70 | self._status = 'playing' |
---|
71 | # TODO: add external call to music player |
---|
72 | self.printer.complete_job(self.jobid) |
---|
73 | |
---|
74 | def finished(self): |
---|
75 | self._status = 'finished' |
---|
76 | |
---|
77 | def __repr__(self): |
---|
78 | return str(self) |
---|
79 | |
---|
80 | def __str__(self): |
---|
81 | return "<Job %d>" % self.jobid |
---|