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