[951ab1b] | 1 | from . import errors |
---|
[eee389a] | 2 | from .player import Player |
---|
[b01b6d1] | 3 | from gutenbach.ipp import JobStates as States |
---|
[ee8e6d0] | 4 | import logging |
---|
[33ea505] | 5 | import os |
---|
[2620618] | 6 | import time |
---|
[d04a689] | 7 | |
---|
| 8 | # initialize logger |
---|
| 9 | logger = logging.getLogger(__name__) |
---|
[776a659] | 10 | |
---|
[33ea505] | 11 | class GutenbachJob(object): |
---|
[b2e077a] | 12 | |
---|
[345c476] | 13 | def __init__(self, job_id=None, creator=None, name=None, |
---|
| 14 | priority=None, document=None): |
---|
[b01b6d1] | 15 | """Create an empty Gutenbach job. |
---|
[e58af05] | 16 | |
---|
[776a659] | 17 | """ |
---|
[ee8e6d0] | 18 | |
---|
[b01b6d1] | 19 | self.player = None |
---|
[345c476] | 20 | self.document = None |
---|
[ee8e6d0] | 21 | |
---|
[b01b6d1] | 22 | self.id = job_id |
---|
| 23 | self.creator = creator |
---|
| 24 | self.name = name |
---|
[345c476] | 25 | self.priority = priority |
---|
[2620618] | 26 | self._why_done = None |
---|
[345c476] | 27 | |
---|
| 28 | if document is not None: |
---|
| 29 | self.spool(document) |
---|
[ee8e6d0] | 30 | |
---|
[b01b6d1] | 31 | def __repr__(self): |
---|
| 32 | return str(self) |
---|
| 33 | |
---|
| 34 | def __str__(self): |
---|
| 35 | return "<Job %d '%s'>" % (self.id, self.name) |
---|
[e58af05] | 36 | |
---|
[eee389a] | 37 | def __cmp__(self, other): |
---|
| 38 | return cmp(self.priority, other.priority) |
---|
| 39 | |
---|
[b01b6d1] | 40 | ###################################################################### |
---|
| 41 | ### Properties ### |
---|
| 42 | ###################################################################### |
---|
[1a63bf7] | 43 | |
---|
[b01b6d1] | 44 | @property |
---|
| 45 | def id(self): |
---|
| 46 | """Unique job identifier. Should be a positive integer, |
---|
| 47 | except when unassigned, when it defaults to -1. |
---|
| 48 | |
---|
| 49 | """ |
---|
| 50 | return self._id |
---|
| 51 | @id.setter |
---|
| 52 | def id(self, val): |
---|
[1a63bf7] | 53 | try: |
---|
[345c476] | 54 | self._id = max(int(val), -1) |
---|
[951ab1b] | 55 | except: |
---|
[b01b6d1] | 56 | self._id = -1 |
---|
| 57 | |
---|
| 58 | @property |
---|
[345c476] | 59 | def priority(self): |
---|
| 60 | return self._priority |
---|
| 61 | @priority.setter |
---|
| 62 | def priority(self, val): |
---|
| 63 | try: |
---|
| 64 | self._priority = max(int(val), 1) |
---|
[951ab1b] | 65 | except: |
---|
[345c476] | 66 | self._priority = 1 |
---|
| 67 | |
---|
| 68 | @property |
---|
[b01b6d1] | 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. |
---|
[1a63bf7] | 98 | |
---|
[b01b6d1] | 99 | """ |
---|
[ee8e6d0] | 100 | try: |
---|
[33ea505] | 101 | size = os.path.getsize(self.document) |
---|
| 102 | except: |
---|
| 103 | size = 0 |
---|
| 104 | return size |
---|
[b01b6d1] | 105 | |
---|
[951ab1b] | 106 | ###################################################################### |
---|
| 107 | ### State ### |
---|
| 108 | ###################################################################### |
---|
| 109 | |
---|
[eee389a] | 110 | @property |
---|
[345c476] | 111 | def is_valid(self): |
---|
| 112 | """Whether the job is ready to be manipulated (spooled, |
---|
| 113 | played, etc). Note that playing the job still requires it to |
---|
| 114 | be spooled first. |
---|
| 115 | |
---|
| 116 | """ |
---|
| 117 | return self.id > 0 and \ |
---|
| 118 | self.priority > 0 |
---|
[eee389a] | 119 | |
---|
| 120 | @property |
---|
| 121 | def is_ready(self): |
---|
[345c476] | 122 | """Whether the job is ready to be played. |
---|
| 123 | |
---|
| 124 | """ |
---|
| 125 | return self.is_valid and \ |
---|
| 126 | self.player is not None and \ |
---|
| 127 | not self.player.is_playing and \ |
---|
| 128 | not self._why_done == "cancelled" and \ |
---|
| 129 | not self._why_done == "aborted" |
---|
[eee389a] | 130 | |
---|
| 131 | @property |
---|
[345c476] | 132 | def is_playing(self): |
---|
| 133 | """Whether the job is currently playing (regardless of whether |
---|
| 134 | it's paused). |
---|
| 135 | |
---|
| 136 | """ |
---|
| 137 | return self.is_valid and \ |
---|
| 138 | self.player is not None and \ |
---|
| 139 | self.player.is_playing |
---|
| 140 | |
---|
| 141 | @property |
---|
| 142 | def is_paused(self): |
---|
| 143 | """Whether the job is currently paused. |
---|
| 144 | |
---|
| 145 | """ |
---|
| 146 | return self.is_valid and \ |
---|
| 147 | self.player is not None and \ |
---|
| 148 | self.player.is_paused |
---|
| 149 | |
---|
| 150 | @property |
---|
| 151 | def is_done(self): |
---|
[951ab1b] | 152 | """Whether the job is done playing, regardless of whether it |
---|
| 153 | completed successfully or not. |
---|
| 154 | |
---|
| 155 | """ |
---|
[345c476] | 156 | return (self.is_valid and \ |
---|
| 157 | self.player is not None and \ |
---|
| 158 | self.player.is_done) or \ |
---|
| 159 | (self._why_done == "cancelled" or \ |
---|
| 160 | self._why_done == "aborted") |
---|
| 161 | |
---|
| 162 | @property |
---|
[951ab1b] | 163 | def is_completed(self): |
---|
| 164 | """Whether the job completed successfully. |
---|
| 165 | |
---|
| 166 | """ |
---|
| 167 | return self.is_done and self._why_done == "completed" |
---|
| 168 | |
---|
| 169 | @property |
---|
| 170 | def is_cancelled(self): |
---|
| 171 | """Whether the job was cancelled. |
---|
| 172 | |
---|
| 173 | """ |
---|
| 174 | return self.is_done and self._why_done == "cancelled" |
---|
| 175 | |
---|
| 176 | @property |
---|
| 177 | def is_aborted(self): |
---|
| 178 | """Whether the job was aborted. |
---|
| 179 | |
---|
| 180 | """ |
---|
| 181 | return self.is_done and self._why_done == "aborted" |
---|
| 182 | |
---|
| 183 | @property |
---|
[345c476] | 184 | def state(self): |
---|
[2620618] | 185 | """State status codes; equivalent to the IPP job-state status |
---|
| 186 | codes. |
---|
| 187 | |
---|
[a2b0582] | 188 | State transitions are as follows: |
---|
[2620618] | 189 | HELD ---> PENDING ---> PROCESSING <--> STOPPED (aka paused) |
---|
| 190 | ^ |---> CANCELLED |
---|
| 191 | | |---> ABORTED |
---|
| 192 | | |---> COMPLETE ---| |
---|
| 193 | |--------------------------------| |
---|
| 194 | |
---|
[a2b0582] | 195 | """ |
---|
[345c476] | 196 | if self.is_ready: |
---|
| 197 | state = States.PENDING |
---|
| 198 | elif self.is_playing and not self.is_paused: |
---|
| 199 | state = States.PROCESSING |
---|
| 200 | elif self.is_playing and self.is_paused: |
---|
| 201 | state = States.STOPPED |
---|
[951ab1b] | 202 | elif self.is_completed: |
---|
[345c476] | 203 | state = States.COMPLETE |
---|
[951ab1b] | 204 | elif self.is_cancelled: |
---|
[345c476] | 205 | state = States.CANCELLED |
---|
[951ab1b] | 206 | elif self.is_aborted: |
---|
[345c476] | 207 | state = States.ABORTED |
---|
| 208 | else: |
---|
| 209 | state = States.HELD |
---|
| 210 | return state |
---|
| 211 | |
---|
[b01b6d1] | 212 | ###################################################################### |
---|
| 213 | ### Methods ### |
---|
| 214 | ###################################################################### |
---|
| 215 | |
---|
[951ab1b] | 216 | @staticmethod |
---|
| 217 | def verify_document(document): |
---|
| 218 | if not hasattr(document, "name"): |
---|
| 219 | raise errors.InvalidDocument, "no name attribute" |
---|
| 220 | if not hasattr(document, "read"): |
---|
| 221 | raise errors.InvalidDocument, "no read attribute" |
---|
| 222 | if not hasattr(document, "close"): |
---|
| 223 | raise errors.InvalidDocument, "no close attribute" |
---|
| 224 | |
---|
| 225 | def spool(self, document=None): |
---|
| 226 | """Non-blocking spool. Job must be valid, and the document |
---|
| 227 | must be an open file handler. |
---|
| 228 | |
---|
| 229 | Raises |
---|
| 230 | ------ |
---|
| 231 | InvalidDocument |
---|
| 232 | If the document is not valid. |
---|
| 233 | InvalidJobStateException |
---|
| 234 | If the job is not valid or it is already |
---|
| 235 | spooled/ready/finished. |
---|
| 236 | |
---|
| 237 | """ |
---|
| 238 | |
---|
| 239 | if not self.is_valid or self.state != States.HELD: |
---|
| 240 | raise errors.InvalidJobStateException(self.state) |
---|
| 241 | self.verify_document(document) |
---|
[33ea505] | 242 | self.document = document.name |
---|
| 243 | self.player = Player(document) |
---|
| 244 | logger.debug("document for job %d is '%s'" % (self.id, self.document)) |
---|
| 245 | |
---|
[b01b6d1] | 246 | def play(self): |
---|
[951ab1b] | 247 | """Non-blocking play. Job must be ready. |
---|
[33ea505] | 248 | |
---|
| 249 | Raises |
---|
| 250 | ------ |
---|
| 251 | InvalidJobStateException |
---|
| 252 | If the job is not ready to be played. |
---|
[eee389a] | 253 | |
---|
| 254 | """ |
---|
| 255 | |
---|
| 256 | # make sure the job is waiting to be played and that it's |
---|
| 257 | # valid |
---|
[33ea505] | 258 | if not self.is_ready: |
---|
[951ab1b] | 259 | raise errors.InvalidJobStateException(self.state) |
---|
[eee389a] | 260 | |
---|
| 261 | # and set the state to processing if we're good to go |
---|
[b01b6d1] | 262 | logger.info("playing job %s" % str(self)) |
---|
[33ea505] | 263 | |
---|
| 264 | def _completed(): |
---|
| 265 | logger.info("completed job %s" % str(self)) |
---|
[345c476] | 266 | self._why_done = "completed" |
---|
[33ea505] | 267 | self.player.callback = _completed |
---|
[d21198f] | 268 | self.player.start() |
---|
[eee389a] | 269 | |
---|
| 270 | def pause(self): |
---|
[951ab1b] | 271 | """Non-blocking pause. Job must be playing. |
---|
| 272 | |
---|
| 273 | Raises |
---|
| 274 | ------ |
---|
| 275 | InvalidJobStateException |
---|
| 276 | If the job is not playing. |
---|
[33ea505] | 277 | |
---|
| 278 | """ |
---|
| 279 | |
---|
| 280 | if not self.is_playing: |
---|
[951ab1b] | 281 | raise errors.InvalidJobStateException(self.state) |
---|
[33ea505] | 282 | self.player.mplayer_pause() |
---|
[34a4e5d] | 283 | |
---|
| 284 | def cancel(self): |
---|
[951ab1b] | 285 | """Non-blocking cancel. The job must not have previously |
---|
| 286 | finished (i.e., cannot be aborted, cancelled, or completed). |
---|
| 287 | This should be used to stop the job following an external |
---|
| 288 | request. |
---|
| 289 | |
---|
| 290 | Raises |
---|
| 291 | ------ |
---|
| 292 | InvalidJobStateException |
---|
| 293 | If the job has already finished. |
---|
| 294 | |
---|
| 295 | """ |
---|
| 296 | |
---|
[345c476] | 297 | def _cancelled(): |
---|
| 298 | logger.info("cancelled job %s" % str(self)) |
---|
| 299 | self._why_done = "cancelled" |
---|
[33ea505] | 300 | |
---|
[34a4e5d] | 301 | if self.is_playing: |
---|
[345c476] | 302 | self.player.callback = _cancelled |
---|
[34a4e5d] | 303 | self.player.mplayer_stop() |
---|
[345c476] | 304 | elif self.is_done and not self._why_done == "cancelled": |
---|
[951ab1b] | 305 | raise errors.InvalidJobStateException(self.state) |
---|
[345c476] | 306 | else: |
---|
| 307 | _cancelled() |
---|
[eee389a] | 308 | |
---|
[34a4e5d] | 309 | def abort(self): |
---|
[951ab1b] | 310 | """Non-blocking abort. The job must not have previously |
---|
| 311 | finished (i.e., cannot be aborted, cancelled, or completed). |
---|
| 312 | This should be used to stop the job following internal errors. |
---|
| 313 | |
---|
| 314 | Raises |
---|
| 315 | ------ |
---|
| 316 | InvalidJobStateException |
---|
| 317 | If the job has already finished. |
---|
| 318 | |
---|
| 319 | """ |
---|
| 320 | |
---|
[33ea505] | 321 | def _aborted(): |
---|
| 322 | logger.info("aborted job %s" % str(self)) |
---|
[345c476] | 323 | self._why_done = "aborted" |
---|
[33ea505] | 324 | |
---|
[34a4e5d] | 325 | if self.is_playing: |
---|
[33ea505] | 326 | self.player.callback = _aborted |
---|
[eee389a] | 327 | self.player.mplayer_stop() |
---|
[345c476] | 328 | elif self.is_done and not self._why_done == "aborted": |
---|
[951ab1b] | 329 | raise errors.InvalidJobStateException(self.state) |
---|
[345c476] | 330 | else: |
---|
| 331 | _aborted() |
---|
[776a659] | 332 | |
---|
| 333 | |
---|