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 | # for IPP |
---|
10 | attributes = [ |
---|
11 | "job-id", |
---|
12 | "job-name", |
---|
13 | "job-originating-user-name", |
---|
14 | "job-k-octets", |
---|
15 | "job-state", |
---|
16 | "job-printer-uri" |
---|
17 | ] |
---|
18 | |
---|
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 | |
---|
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 |
---|
49 | |
---|
50 | @property |
---|
51 | def job_id(self): |
---|
52 | return self.jid |
---|
53 | |
---|
54 | @property |
---|
55 | def job_name(self): |
---|
56 | return self.name |
---|
57 | |
---|
58 | # XXX: we need to actually calculate this! |
---|
59 | @property |
---|
60 | def job_originating_user_name(self): |
---|
61 | return "jhamrick" |
---|
62 | |
---|
63 | # XXX: we need to actually calculate this! |
---|
64 | @property |
---|
65 | def job_k_octets(self): |
---|
66 | return "job-k-octets" |
---|
67 | |
---|
68 | @property |
---|
69 | def job_state(self): |
---|
70 | return self.status |
---|
71 | |
---|
72 | @property |
---|
73 | def job_printer_uri(self): |
---|
74 | return self.printer.uri |
---|
75 | |
---|
76 | def get_job_attributes(self, request): |
---|
77 | attributes = [(attr, getattr(self, attr)) for attr in self.attributes] |
---|
78 | return attributes |
---|
79 | |
---|
80 | ####### |
---|
81 | |
---|
82 | def enqueue(self, printer, job_id): |
---|
83 | if self.status != None: |
---|
84 | raise InvalidJobException( |
---|
85 | "Cannot enqueue a job that has " + \ |
---|
86 | "already been initialized!") |
---|
87 | self.printer = printer |
---|
88 | self.jid = job_id |
---|
89 | self.status = const.JobStates.PENDING |
---|
90 | |
---|
91 | def play(self): |
---|
92 | if self.status != 'active': |
---|
93 | raise InvalidJobException( |
---|
94 | "Cannot play an inactive job!") |
---|
95 | |
---|
96 | self.status = const.JobStates.PROCESSING |
---|
97 | # TODO: add external call to music player |
---|
98 | print "Playing job %s" % str(self) |
---|
99 | self.printer.complete_job(self.jid) |
---|
100 | |
---|
101 | def finish(self): |
---|
102 | self.status = const.JobStates.COMPLETE |
---|
103 | |
---|
104 | def __repr__(self): |
---|
105 | return str(self) |
---|
106 | |
---|
107 | def __str__(self): |
---|
108 | return "<Job %d '%s'>" % \ |
---|
109 | (self.jid if self.jid is not None else -1, \ |
---|
110 | self.document) |
---|