[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 | |
---|
[ef387cf] | 17 | Parameters |
---|
| 18 | ---------- |
---|
| 19 | job_id : integer |
---|
| 20 | A unique id for this job. |
---|
| 21 | creator : string |
---|
| 22 | The user creating the job. |
---|
| 23 | name : string |
---|
| 24 | The human-readable name of the job. |
---|
| 25 | priority : integer |
---|
| 26 | The priority of the job, used for ordering. |
---|
| 27 | document : file object |
---|
| 28 | A file object containing the job data. |
---|
| 29 | |
---|
[776a659] | 30 | """ |
---|
[ee8e6d0] | 31 | |
---|
[b01b6d1] | 32 | self.player = None |
---|
[345c476] | 33 | self.document = None |
---|
[ee8e6d0] | 34 | |
---|
[b01b6d1] | 35 | self.id = job_id |
---|
| 36 | self.creator = creator |
---|
| 37 | self.name = name |
---|
[345c476] | 38 | self.priority = priority |
---|
[ef387cf] | 39 | |
---|
[2620618] | 40 | self._why_done = None |
---|
[345c476] | 41 | |
---|
| 42 | if document is not None: |
---|
| 43 | self.spool(document) |
---|
[ee8e6d0] | 44 | |
---|
[b01b6d1] | 45 | def __repr__(self): |
---|
| 46 | return str(self) |
---|
| 47 | |
---|
| 48 | def __str__(self): |
---|
| 49 | return "<Job %d '%s'>" % (self.id, self.name) |
---|
[e58af05] | 50 | |
---|
[eee389a] | 51 | def __cmp__(self, other): |
---|
[ef387cf] | 52 | """Compares two GutenbachJobs based on their priorities. |
---|
| 53 | |
---|
| 54 | """ |
---|
[eee389a] | 55 | return cmp(self.priority, other.priority) |
---|
| 56 | |
---|
[b01b6d1] | 57 | ###################################################################### |
---|
| 58 | ### Properties ### |
---|
| 59 | ###################################################################### |
---|
[1a63bf7] | 60 | |
---|
[b01b6d1] | 61 | @property |
---|
| 62 | def id(self): |
---|
[ef387cf] | 63 | """Unique job identifier (integer). Should be a positive |
---|
| 64 | integer, except when unassigned, when it defaults to -1. |
---|
[b01b6d1] | 65 | |
---|
| 66 | """ |
---|
| 67 | return self._id |
---|
| 68 | @id.setter |
---|
| 69 | def id(self, val): |
---|
[1a63bf7] | 70 | try: |
---|
[345c476] | 71 | self._id = max(int(val), -1) |
---|
[951ab1b] | 72 | except: |
---|
[b01b6d1] | 73 | self._id = -1 |
---|
| 74 | |
---|
| 75 | @property |
---|
[345c476] | 76 | def priority(self): |
---|
[ef387cf] | 77 | """Job priority (integer). Should be a nonzero positive |
---|
| 78 | integer; defaults to 1 when unassigned. |
---|
| 79 | |
---|
| 80 | """ |
---|
[345c476] | 81 | return self._priority |
---|
| 82 | @priority.setter |
---|
| 83 | def priority(self, val): |
---|
| 84 | try: |
---|
| 85 | self._priority = max(int(val), 1) |
---|
[951ab1b] | 86 | except: |
---|
[345c476] | 87 | self._priority = 1 |
---|
| 88 | |
---|
| 89 | @property |
---|
[b01b6d1] | 90 | def creator(self): |
---|
[ef387cf] | 91 | """The user who created the job (string). Defaults to an |
---|
| 92 | empty string. |
---|
[b01b6d1] | 93 | |
---|
| 94 | """ |
---|
| 95 | return self._creator |
---|
| 96 | @creator.setter |
---|
| 97 | def creator(self, val): |
---|
| 98 | if val is None: |
---|
| 99 | self._creator = "" |
---|
| 100 | else: |
---|
| 101 | self._creator = str(val) |
---|
| 102 | |
---|
| 103 | @property |
---|
| 104 | def name(self): |
---|
[ef387cf] | 105 | """The job's human-readable name (string). Defaults to an |
---|
| 106 | empty string. |
---|
[b01b6d1] | 107 | |
---|
| 108 | """ |
---|
| 109 | return self._name |
---|
| 110 | @name.setter |
---|
| 111 | def name(self, val): |
---|
| 112 | if val is None: |
---|
| 113 | self._name = "" |
---|
| 114 | else: |
---|
| 115 | self._name = str(val) |
---|
| 116 | |
---|
| 117 | @property |
---|
| 118 | def size(self): |
---|
[ef387cf] | 119 | """The size of the job in octets/bytes (integer). Defaults to |
---|
| 120 | 0 if no document is specified or if there is an error reading |
---|
| 121 | the document. |
---|
[1a63bf7] | 122 | |
---|
[b01b6d1] | 123 | """ |
---|
[ee8e6d0] | 124 | try: |
---|
[33ea505] | 125 | size = os.path.getsize(self.document) |
---|
| 126 | except: |
---|
| 127 | size = 0 |
---|
| 128 | return size |
---|
[b01b6d1] | 129 | |
---|
[951ab1b] | 130 | ###################################################################### |
---|
| 131 | ### State ### |
---|
| 132 | ###################################################################### |
---|
| 133 | |
---|
[eee389a] | 134 | @property |
---|
[345c476] | 135 | def is_valid(self): |
---|
| 136 | """Whether the job is ready to be manipulated (spooled, |
---|
| 137 | played, etc). Note that playing the job still requires it to |
---|
| 138 | be spooled first. |
---|
| 139 | |
---|
| 140 | """ |
---|
[ef387cf] | 141 | |
---|
[345c476] | 142 | return self.id > 0 and \ |
---|
| 143 | self.priority > 0 |
---|
[eee389a] | 144 | |
---|
| 145 | @property |
---|
| 146 | def is_ready(self): |
---|
[ef387cf] | 147 | """Whether the job is ready to be played; i.e., it has all the |
---|
| 148 | necessary data to actually play the audio data. |
---|
[345c476] | 149 | |
---|
| 150 | """ |
---|
[ef387cf] | 151 | |
---|
[345c476] | 152 | return self.is_valid and \ |
---|
| 153 | self.player is not None and \ |
---|
| 154 | not self.player.is_playing and \ |
---|
| 155 | not self._why_done == "cancelled" and \ |
---|
| 156 | not self._why_done == "aborted" |
---|
[eee389a] | 157 | |
---|
| 158 | @property |
---|
[345c476] | 159 | def is_playing(self): |
---|
| 160 | """Whether the job is currently playing (regardless of whether |
---|
| 161 | it's paused). |
---|
| 162 | |
---|
| 163 | """ |
---|
[ef387cf] | 164 | |
---|
[345c476] | 165 | return self.is_valid and \ |
---|
| 166 | self.player is not None and \ |
---|
| 167 | self.player.is_playing |
---|
| 168 | |
---|
| 169 | @property |
---|
| 170 | def is_paused(self): |
---|
| 171 | """Whether the job is currently paused. |
---|
| 172 | |
---|
| 173 | """ |
---|
[ef387cf] | 174 | |
---|
[345c476] | 175 | return self.is_valid and \ |
---|
| 176 | self.player is not None and \ |
---|
| 177 | self.player.is_paused |
---|
| 178 | |
---|
| 179 | @property |
---|
| 180 | def is_done(self): |
---|
[951ab1b] | 181 | """Whether the job is done playing, regardless of whether it |
---|
| 182 | completed successfully or not. |
---|
| 183 | |
---|
| 184 | """ |
---|
[ef387cf] | 185 | |
---|
[345c476] | 186 | return (self.is_valid and \ |
---|
| 187 | self.player is not None and \ |
---|
| 188 | self.player.is_done) or \ |
---|
| 189 | (self._why_done == "cancelled" or \ |
---|
| 190 | self._why_done == "aborted") |
---|
| 191 | |
---|
| 192 | @property |
---|
[951ab1b] | 193 | def is_completed(self): |
---|
| 194 | """Whether the job completed successfully. |
---|
| 195 | |
---|
| 196 | """ |
---|
[ef387cf] | 197 | |
---|
[951ab1b] | 198 | return self.is_done and self._why_done == "completed" |
---|
| 199 | |
---|
| 200 | @property |
---|
| 201 | def is_cancelled(self): |
---|
| 202 | """Whether the job was cancelled. |
---|
| 203 | |
---|
| 204 | """ |
---|
[ef387cf] | 205 | |
---|
[951ab1b] | 206 | return self.is_done and self._why_done == "cancelled" |
---|
| 207 | |
---|
| 208 | @property |
---|
| 209 | def is_aborted(self): |
---|
| 210 | """Whether the job was aborted. |
---|
| 211 | |
---|
| 212 | """ |
---|
[ef387cf] | 213 | |
---|
[951ab1b] | 214 | return self.is_done and self._why_done == "aborted" |
---|
| 215 | |
---|
| 216 | @property |
---|
[345c476] | 217 | def state(self): |
---|
[ef387cf] | 218 | """State status codes (these are equivalent to the IPP |
---|
| 219 | job-state status codes). State transitions are as follows: |
---|
[2620618] | 220 | |
---|
[ef387cf] | 221 | HELD ---> PENDING ---> PROCESSING <--> STOPPED (aka paused) |
---|
| 222 | ^ |---> CANCELLED |
---|
| 223 | | |---> ABORTED |
---|
| 224 | | |---> COMPLETE ---| |
---|
| 225 | |--------------------------------| |
---|
[2620618] | 226 | |
---|
[a2b0582] | 227 | """ |
---|
[ef387cf] | 228 | |
---|
[345c476] | 229 | if self.is_ready: |
---|
| 230 | state = States.PENDING |
---|
| 231 | elif self.is_playing and not self.is_paused: |
---|
| 232 | state = States.PROCESSING |
---|
| 233 | elif self.is_playing and self.is_paused: |
---|
| 234 | state = States.STOPPED |
---|
[951ab1b] | 235 | elif self.is_completed: |
---|
[345c476] | 236 | state = States.COMPLETE |
---|
[951ab1b] | 237 | elif self.is_cancelled: |
---|
[345c476] | 238 | state = States.CANCELLED |
---|
[951ab1b] | 239 | elif self.is_aborted: |
---|
[345c476] | 240 | state = States.ABORTED |
---|
| 241 | else: |
---|
| 242 | state = States.HELD |
---|
| 243 | return state |
---|
| 244 | |
---|
[b01b6d1] | 245 | ###################################################################### |
---|
| 246 | ### Methods ### |
---|
| 247 | ###################################################################### |
---|
| 248 | |
---|
[951ab1b] | 249 | @staticmethod |
---|
| 250 | def verify_document(document): |
---|
[ef387cf] | 251 | """Verifies that a document has the 'name', 'read', and |
---|
| 252 | 'close' attributes (i.e., it should be like a file object). |
---|
| 253 | |
---|
| 254 | """ |
---|
| 255 | |
---|
[951ab1b] | 256 | if not hasattr(document, "name"): |
---|
| 257 | raise errors.InvalidDocument, "no name attribute" |
---|
| 258 | if not hasattr(document, "read"): |
---|
| 259 | raise errors.InvalidDocument, "no read attribute" |
---|
| 260 | if not hasattr(document, "close"): |
---|
| 261 | raise errors.InvalidDocument, "no close attribute" |
---|
| 262 | |
---|
| 263 | def spool(self, document=None): |
---|
[ef387cf] | 264 | """Non-blocking spool. Job must be valid (see |
---|
| 265 | 'GutenbachJob.is_valid'), and the document must be an open |
---|
| 266 | file handler. |
---|
[951ab1b] | 267 | |
---|
| 268 | Raises |
---|
| 269 | ------ |
---|
| 270 | InvalidDocument |
---|
| 271 | If the document is not valid. |
---|
| 272 | InvalidJobStateException |
---|
| 273 | If the job is not valid or it is already |
---|
| 274 | spooled/ready/finished. |
---|
| 275 | |
---|
| 276 | """ |
---|
| 277 | |
---|
| 278 | if not self.is_valid or self.state != States.HELD: |
---|
| 279 | raise errors.InvalidJobStateException(self.state) |
---|
| 280 | self.verify_document(document) |
---|
[33ea505] | 281 | self.document = document.name |
---|
| 282 | self.player = Player(document) |
---|
| 283 | logger.debug("document for job %d is '%s'" % (self.id, self.document)) |
---|
| 284 | |
---|
[b01b6d1] | 285 | def play(self): |
---|
[ef387cf] | 286 | """Non-blocking play. Job must be ready (see |
---|
| 287 | 'GutenbachJob.is_ready'). |
---|
[33ea505] | 288 | |
---|
| 289 | Raises |
---|
| 290 | ------ |
---|
| 291 | InvalidJobStateException |
---|
| 292 | If the job is not ready to be played. |
---|
[eee389a] | 293 | |
---|
| 294 | """ |
---|
| 295 | |
---|
| 296 | # make sure the job is waiting to be played and that it's |
---|
| 297 | # valid |
---|
[33ea505] | 298 | if not self.is_ready: |
---|
[951ab1b] | 299 | raise errors.InvalidJobStateException(self.state) |
---|
[eee389a] | 300 | |
---|
| 301 | # and set the state to processing if we're good to go |
---|
[b01b6d1] | 302 | logger.info("playing job %s" % str(self)) |
---|
[33ea505] | 303 | |
---|
| 304 | def _completed(): |
---|
| 305 | logger.info("completed job %s" % str(self)) |
---|
[345c476] | 306 | self._why_done = "completed" |
---|
[33ea505] | 307 | self.player.callback = _completed |
---|
[d21198f] | 308 | self.player.start() |
---|
[eee389a] | 309 | |
---|
| 310 | def pause(self): |
---|
[ef387cf] | 311 | """Non-blocking pause. Job must be playing (see |
---|
| 312 | 'GutenbachJob.is_playing'). |
---|
[951ab1b] | 313 | |
---|
| 314 | Raises |
---|
| 315 | ------ |
---|
| 316 | InvalidJobStateException |
---|
| 317 | If the job is not playing. |
---|
[33ea505] | 318 | |
---|
| 319 | """ |
---|
| 320 | |
---|
| 321 | if not self.is_playing: |
---|
[951ab1b] | 322 | raise errors.InvalidJobStateException(self.state) |
---|
[33ea505] | 323 | self.player.mplayer_pause() |
---|
[34a4e5d] | 324 | |
---|
| 325 | def cancel(self): |
---|
[ef387cf] | 326 | """Non-blocking cancel. The job must not have been previously |
---|
| 327 | aborted or completed (though this method will succeed if it |
---|
| 328 | was previously cancelled). This should be used to stop the |
---|
| 329 | job following an external request. |
---|
[951ab1b] | 330 | |
---|
| 331 | Raises |
---|
| 332 | ------ |
---|
| 333 | InvalidJobStateException |
---|
| 334 | If the job has already finished. |
---|
| 335 | |
---|
| 336 | """ |
---|
| 337 | |
---|
[345c476] | 338 | def _cancelled(): |
---|
| 339 | logger.info("cancelled job %s" % str(self)) |
---|
| 340 | self._why_done = "cancelled" |
---|
[33ea505] | 341 | |
---|
[34a4e5d] | 342 | if self.is_playing: |
---|
[345c476] | 343 | self.player.callback = _cancelled |
---|
[34a4e5d] | 344 | self.player.mplayer_stop() |
---|
[345c476] | 345 | elif self.is_done and not self._why_done == "cancelled": |
---|
[951ab1b] | 346 | raise errors.InvalidJobStateException(self.state) |
---|
[345c476] | 347 | else: |
---|
| 348 | _cancelled() |
---|
[eee389a] | 349 | |
---|
[34a4e5d] | 350 | def abort(self): |
---|
[ef387cf] | 351 | """Non-blocking abort. The job must not have been previously |
---|
| 352 | cancelled or completed (though this method will succeed if it |
---|
| 353 | was previously aborted). This should be used to stop the job |
---|
| 354 | following internal errors. |
---|
[951ab1b] | 355 | |
---|
| 356 | Raises |
---|
| 357 | ------ |
---|
| 358 | InvalidJobStateException |
---|
| 359 | If the job has already finished. |
---|
| 360 | |
---|
| 361 | """ |
---|
| 362 | |
---|
[33ea505] | 363 | def _aborted(): |
---|
| 364 | logger.info("aborted job %s" % str(self)) |
---|
[345c476] | 365 | self._why_done = "aborted" |
---|
[33ea505] | 366 | |
---|
[34a4e5d] | 367 | if self.is_playing: |
---|
[33ea505] | 368 | self.player.callback = _aborted |
---|
[eee389a] | 369 | self.player.mplayer_stop() |
---|
[345c476] | 370 | elif self.is_done and not self._why_done == "aborted": |
---|
[951ab1b] | 371 | raise errors.InvalidJobStateException(self.state) |
---|
[345c476] | 372 | else: |
---|
| 373 | _aborted() |
---|
[776a659] | 374 | |
---|
[609a9b0] | 375 | def restart(self): |
---|
| 376 | # XXX: Todo |
---|
| 377 | pass |
---|