1 | from exceptions import InvalidJobException, InvalidPrinterStateException |
---|
2 | import os |
---|
3 | import gutenbach.ipp as ipp |
---|
4 | import logging |
---|
5 | |
---|
6 | # initialize logger |
---|
7 | logger = logging.getLogger(__name__) |
---|
8 | |
---|
9 | class Job(object): |
---|
10 | |
---|
11 | # for IPP |
---|
12 | attributes = [ |
---|
13 | "job-id", |
---|
14 | "job-name", |
---|
15 | "job-originating-user-name", |
---|
16 | "job-k-octets", |
---|
17 | "job-state", |
---|
18 | "job-printer-uri" |
---|
19 | ] |
---|
20 | |
---|
21 | def __init__(self, jid, printer, creator="", name="", size=0): |
---|
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 | """ |
---|
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 |
---|
36 | |
---|
37 | def __getattr__(self, attr): |
---|
38 | try: |
---|
39 | return self.__getattribute__(attr) |
---|
40 | except AttributeError: |
---|
41 | pass |
---|
42 | return self.__getattribute__(attr.replace("-", "_")) |
---|
43 | |
---|
44 | def __hasattr__(self, attr): |
---|
45 | try: |
---|
46 | getattr(self, attr) |
---|
47 | return True |
---|
48 | except AttributeError: |
---|
49 | return False |
---|
50 | |
---|
51 | #### Job attributes |
---|
52 | |
---|
53 | @property |
---|
54 | def job_id(self): |
---|
55 | return ipp.JobId(self.jid) |
---|
56 | |
---|
57 | @property |
---|
58 | def job_name(self): |
---|
59 | return ipp.JobName(self.name) |
---|
60 | |
---|
61 | # XXX: we need to actually calculate this! |
---|
62 | @property |
---|
63 | def job_originating_user_name(self): |
---|
64 | return ipp.JobOriginatingUserName(self.creator) |
---|
65 | |
---|
66 | # XXX: we need to actually calculate this! |
---|
67 | @property |
---|
68 | def job_k_octets(self): |
---|
69 | return ipp.JobKOctets(self.size) |
---|
70 | |
---|
71 | @property |
---|
72 | def job_state(self): |
---|
73 | return ipp.JobState(self.status) |
---|
74 | |
---|
75 | @property |
---|
76 | def job_printer_uri(self): |
---|
77 | return ipp.JobPrinterUri(self.printer.uri) |
---|
78 | |
---|
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] |
---|
89 | return attributes |
---|
90 | |
---|
91 | ####### |
---|
92 | |
---|
93 | def play(self): |
---|
94 | if self.status != 'active': |
---|
95 | raise InvalidJobException( |
---|
96 | "Cannot play an inactive job!") |
---|
97 | |
---|
98 | self.status = const.JobStates.PROCESSING |
---|
99 | # TODO: add external call to music player |
---|
100 | print "Playing job %s" % str(self) |
---|
101 | self.printer.complete_job(self.jid) |
---|
102 | |
---|
103 | def finish(self): |
---|
104 | self.status = const.JobStates.COMPLETE |
---|
105 | |
---|
106 | def __repr__(self): |
---|
107 | return str(self) |
---|
108 | |
---|
109 | def __str__(self): |
---|
110 | return "<Job %d '%s'>" % (self.jid if self.jid is not None else -1, self.name) |
---|