1 | from . import InvalidJobException, InvalidPrinterStateException |
---|
2 | from gutenbach.ipp import JobStates as States |
---|
3 | import os |
---|
4 | import gutenbach.ipp as ipp |
---|
5 | import logging |
---|
6 | import subprocess |
---|
7 | import time |
---|
8 | |
---|
9 | # initialize logger |
---|
10 | logger = logging.getLogger(__name__) |
---|
11 | |
---|
12 | class Job(object): |
---|
13 | |
---|
14 | # for IPP |
---|
15 | attributes = [ |
---|
16 | "job-id", |
---|
17 | "job-name", |
---|
18 | "job-originating-user-name", |
---|
19 | "job-k-octets", |
---|
20 | "job-state", |
---|
21 | "job-printer-uri" |
---|
22 | ] |
---|
23 | |
---|
24 | def __init__(self, job_id=-1, printer=None, creator=None, name=None, size=None): |
---|
25 | """Create an empty Gutenbach job. |
---|
26 | |
---|
27 | """ |
---|
28 | |
---|
29 | self.printer = printer |
---|
30 | self.player = None |
---|
31 | |
---|
32 | self.id = job_id |
---|
33 | self.creator = creator |
---|
34 | self.name = name |
---|
35 | self.size = size |
---|
36 | self.status = States.HELD |
---|
37 | |
---|
38 | self.document = None |
---|
39 | self.document_name = None |
---|
40 | self.document_format = None |
---|
41 | self.document_natural_language = None |
---|
42 | self.compression = None |
---|
43 | |
---|
44 | def __repr__(self): |
---|
45 | return str(self) |
---|
46 | |
---|
47 | def __str__(self): |
---|
48 | return "<Job %d '%s'>" % (self.id, self.name) |
---|
49 | |
---|
50 | ###################################################################### |
---|
51 | ### Properties ### |
---|
52 | ###################################################################### |
---|
53 | |
---|
54 | @property |
---|
55 | def id(self): |
---|
56 | """Unique job identifier. Should be a positive integer, |
---|
57 | except when unassigned, when it defaults to -1. |
---|
58 | |
---|
59 | """ |
---|
60 | return self._id |
---|
61 | @id.setter |
---|
62 | def id(self, val): |
---|
63 | try: |
---|
64 | self._id = int(val) |
---|
65 | except TypeError: |
---|
66 | self._id = -1 |
---|
67 | |
---|
68 | @property |
---|
69 | def creator(self): |
---|
70 | """The user who created the job; analogous to the IPP |
---|
71 | requesting-user-name. |
---|
72 | |
---|
73 | """ |
---|
74 | return self._creator |
---|
75 | @creator.setter |
---|
76 | def creator(self, val): |
---|
77 | if val is None: |
---|
78 | self._creator = "" |
---|
79 | else: |
---|
80 | self._creator = str(val) |
---|
81 | |
---|
82 | @property |
---|
83 | def name(self): |
---|
84 | """The job's name. |
---|
85 | |
---|
86 | """ |
---|
87 | return self._name |
---|
88 | @name.setter |
---|
89 | def name(self, val): |
---|
90 | if val is None: |
---|
91 | self._name = "" |
---|
92 | else: |
---|
93 | self._name = str(val) |
---|
94 | |
---|
95 | @property |
---|
96 | def size(self): |
---|
97 | """The size of the job in bytes. |
---|
98 | |
---|
99 | """ |
---|
100 | if self.document: |
---|
101 | size = os.path.getsize(self.document.name) |
---|
102 | else: |
---|
103 | size = self._size |
---|
104 | return size |
---|
105 | @size.setter |
---|
106 | def size(self, val): |
---|
107 | try: |
---|
108 | self._size = int(val) |
---|
109 | except TypeError: |
---|
110 | self._size = 0 |
---|
111 | |
---|
112 | ###################################################################### |
---|
113 | ### Methods ### |
---|
114 | ###################################################################### |
---|
115 | |
---|
116 | def play(self): |
---|
117 | logger.info("playing job %s" % str(self)) |
---|
118 | # TODO: add external call to music player |
---|
119 | self.status = States.PROCESSING |
---|
120 | self.player = subprocess.Popen( |
---|
121 | "/usr/bin/mplayer -quiet %s" % self.document.name, |
---|
122 | shell=True) |
---|
123 | #stderr=subprocess.PIPE, |
---|
124 | #stdout=subprocess.PIPE) |
---|
125 | while self.player.poll() is None: |
---|
126 | time.sleep(0.1) |
---|
127 | logger.info("mplayer finished with code %d" % self.player.returncode) |
---|
128 | #if self.player.returncode < 0: |
---|
129 | # logger.error(self.player.stderr) |
---|
130 | #logger.debug(self.player.stdout) |
---|
131 | self.player = None |
---|
132 | self.printer.complete_job(self.id) |
---|
133 | |
---|
134 | def finish(self): |
---|
135 | logger.info("finished job %s" % str(self)) |
---|
136 | self.status = States.COMPLETE |
---|
137 | |
---|
138 | ###################################################################### |
---|
139 | ### IPP Attributes ### |
---|
140 | ###################################################################### |
---|
141 | |
---|
142 | @property |
---|
143 | def job_id(self): |
---|
144 | return ipp.JobId(self.id) |
---|
145 | |
---|
146 | @property |
---|
147 | def job_name(self): |
---|
148 | return ipp.JobName(self.name) |
---|
149 | |
---|
150 | # XXX: we need to actually calculate this! |
---|
151 | @property |
---|
152 | def job_originating_user_name(self): |
---|
153 | return ipp.JobOriginatingUserName(self.creator) |
---|
154 | |
---|
155 | # XXX: we need to actually calculate this! |
---|
156 | @property |
---|
157 | def job_k_octets(self): |
---|
158 | return ipp.JobKOctets(self.size) |
---|
159 | |
---|
160 | @property |
---|
161 | def job_state(self): |
---|
162 | return ipp.JobState(self.status) |
---|
163 | |
---|
164 | @property |
---|
165 | def job_printer_uri(self): |
---|
166 | return ipp.JobPrinterUri(self.printer.uri) |
---|
167 | |
---|
168 | |
---|
169 | ###################################################################### |
---|
170 | ### IPP Operations ### |
---|
171 | ###################################################################### |
---|
172 | |
---|
173 | def cancel_job(self): |
---|
174 | pass |
---|
175 | |
---|
176 | def send_document(self, |
---|
177 | document, |
---|
178 | document_name=None, |
---|
179 | document_format=None, |
---|
180 | document_natural_language=None, |
---|
181 | requesting_user_name=None, |
---|
182 | compression=None, |
---|
183 | last_document=None): |
---|
184 | |
---|
185 | if self.status != States.HELD: |
---|
186 | raise InvalidJobStateException(self.status) |
---|
187 | |
---|
188 | self.document = document |
---|
189 | self.document_name = str(document_name) |
---|
190 | self.document_format = str(document_format) |
---|
191 | self.document_natural_language = str(document_natural_language) |
---|
192 | self.creator = str(requesting_user_name) |
---|
193 | self.compression = str(compression) |
---|
194 | self.status = States.PENDING |
---|
195 | |
---|
196 | logger.debug("document for job %d is '%s'" % (self.id, self.document_name)) |
---|
197 | |
---|
198 | def send_uri(self): |
---|
199 | pass |
---|
200 | |
---|
201 | def get_job_attributes(self, requested_attributes=None): |
---|
202 | if requested_attributes is None: |
---|
203 | requested = self.attributes |
---|
204 | else: |
---|
205 | requested = [a for a in self.attributes if a in requested_attributes] |
---|
206 | |
---|
207 | _attributes = [attr.replace("-", "_") for attr in requested] |
---|
208 | attributes = [getattr(self, attr) for attr in _attributes] |
---|
209 | return attributes |
---|
210 | |
---|
211 | def set_job_attributes(self): |
---|
212 | pass |
---|
213 | |
---|
214 | def restart_job(self): |
---|
215 | pass |
---|
216 | |
---|
217 | def promote_job(self): |
---|
218 | pass |
---|