[33ea505] | 1 | from .errors import InvalidJobException, InvalidPrinterStateException, InvalidJobStateException |
---|
| 2 | from .job import GutenbachJob |
---|
[b01b6d1] | 3 | from gutenbach.ipp import PrinterStates as States |
---|
[b2e077a] | 4 | import gutenbach.ipp as ipp |
---|
| 5 | import logging |
---|
| 6 | import time |
---|
[eee389a] | 7 | import threading |
---|
| 8 | import traceback |
---|
| 9 | import sys |
---|
[57bc2dc] | 10 | import tempfile |
---|
[cf0d7e8] | 11 | from . import sync |
---|
[eee389a] | 12 | |
---|
[d04a689] | 13 | |
---|
| 14 | # initialize logger |
---|
| 15 | logger = logging.getLogger(__name__) |
---|
[776a659] | 16 | |
---|
[eee389a] | 17 | class GutenbachPrinter(threading.Thread): |
---|
[b2e077a] | 18 | |
---|
[1a63bf7] | 19 | # for IPP |
---|
[33ea505] | 20 | printer_attributes = [ |
---|
[b2e077a] | 21 | "printer-uri-supported", |
---|
| 22 | "uri-authentication-supported", |
---|
| 23 | "uri-security-supported", |
---|
| 24 | "printer-name", |
---|
| 25 | "printer-state", |
---|
| 26 | "printer-state-reasons", |
---|
| 27 | "ipp-versions-supported", |
---|
| 28 | "operations-supported", |
---|
| 29 | "charset-configured", |
---|
| 30 | "charset-supported", |
---|
| 31 | "natural-language-configured", |
---|
| 32 | "generated-natural-language-supported", |
---|
| 33 | "document-format-default", |
---|
| 34 | "document-format-supported", |
---|
| 35 | "printer-is-accepting-jobs", |
---|
| 36 | "queued-job-count", |
---|
| 37 | "pdl-override-supported", |
---|
| 38 | "printer-up-time", |
---|
[f6e2532] | 39 | "compression-supported", |
---|
| 40 | "multiple-operation-time-out", |
---|
| 41 | "multiple-document-jobs-supported", |
---|
[1a63bf7] | 42 | ] |
---|
[b2e077a] | 43 | |
---|
[33ea505] | 44 | job_attributes = [ |
---|
| 45 | "job-id", |
---|
| 46 | "job-name", |
---|
| 47 | "job-originating-user-name", |
---|
| 48 | "job-k-octets", |
---|
| 49 | "job-state", |
---|
| 50 | "job-printer-uri" |
---|
| 51 | ] |
---|
| 52 | |
---|
[f6e2532] | 53 | operations = [ |
---|
| 54 | "print-job", |
---|
[33ea505] | 55 | "validate-job", |
---|
[f6e2532] | 56 | "get-jobs", |
---|
[33ea505] | 57 | "print-uri", |
---|
| 58 | "create-job", |
---|
| 59 | "pause-printer", |
---|
| 60 | "resume-printer", |
---|
| 61 | "get-printer-attributes", |
---|
| 62 | "set-printer-attributes", |
---|
| 63 | "cancel-job", |
---|
| 64 | "send-document", |
---|
| 65 | "send-uri", |
---|
| 66 | "get-job-attributes", |
---|
| 67 | "set-job-attributes", |
---|
| 68 | "restart-job", |
---|
| 69 | "promote-job" |
---|
[f6e2532] | 70 | ] |
---|
| 71 | |
---|
[609a9b0] | 72 | def __init__(self, name, config, *args, **kwargs): |
---|
[eee389a] | 73 | |
---|
[d21198f] | 74 | super(GutenbachPrinter, self).__init__(*args, **kwargs) |
---|
| 75 | |
---|
| 76 | self.name = name |
---|
[609a9b0] | 77 | self.config = config |
---|
[d21198f] | 78 | self.time_created = int(time.time()) |
---|
[b2e077a] | 79 | |
---|
[d21198f] | 80 | self.finished_jobs = [] |
---|
| 81 | self.pending_jobs = [] |
---|
| 82 | self.current_job = None |
---|
| 83 | self.jobs = {} |
---|
[b2e077a] | 84 | |
---|
[d21198f] | 85 | self.lock = threading.RLock() |
---|
[33528b4] | 86 | self._running = False |
---|
[d21198f] | 87 | self.paused = False |
---|
[776a659] | 88 | |
---|
[d21198f] | 89 | # CUPS ignores jobs with id 0, so we have to start at 1 |
---|
| 90 | self._next_job_id = 1 |
---|
[b01b6d1] | 91 | |
---|
[33528b4] | 92 | @sync |
---|
[b01b6d1] | 93 | def __repr__(self): |
---|
| 94 | return str(self) |
---|
| 95 | |
---|
[33528b4] | 96 | @sync |
---|
[b01b6d1] | 97 | def __str__(self): |
---|
[33528b4] | 98 | return "<Printer '%s'>" % self.name |
---|
[b01b6d1] | 99 | |
---|
[33ea505] | 100 | def run(self): |
---|
[33528b4] | 101 | self._running = True |
---|
| 102 | while self._running: |
---|
[d42236e] | 103 | with self.lock: |
---|
| 104 | try: |
---|
[33528b4] | 105 | if self.current_job is None: |
---|
[33ea505] | 106 | self.start_job() |
---|
[345c476] | 107 | elif self.current_job.is_done: |
---|
[33ea505] | 108 | self.complete_job() |
---|
[d42236e] | 109 | except: |
---|
| 110 | self._running = False |
---|
| 111 | logger.fatal(traceback.format_exc()) |
---|
| 112 | break |
---|
[33ea505] | 113 | time.sleep(0.1) |
---|
| 114 | |
---|
[33528b4] | 115 | def stop(self): |
---|
| 116 | with self.lock: |
---|
[d42236e] | 117 | for job in self.jobs.keys(): |
---|
| 118 | try: |
---|
| 119 | self.jobs[job].abort() |
---|
| 120 | del self.jobs[job] |
---|
| 121 | except InvalidJobStateException: |
---|
| 122 | pass |
---|
| 123 | |
---|
[33528b4] | 124 | self._running = False |
---|
| 125 | if self.ident is not None and self.isAlive(): |
---|
| 126 | self.join() |
---|
| 127 | |
---|
[b01b6d1] | 128 | ###################################################################### |
---|
| 129 | ### Properties ### |
---|
| 130 | ###################################################################### |
---|
| 131 | |
---|
| 132 | @property |
---|
[7e29e6a] | 133 | def name(self): |
---|
| 134 | return self._name |
---|
| 135 | @name.setter |
---|
| 136 | def name(self, val): |
---|
| 137 | try: |
---|
| 138 | self._name = str(val) |
---|
| 139 | except: |
---|
| 140 | self._name = "gutenbach-printer" |
---|
| 141 | |
---|
| 142 | @property |
---|
| 143 | def config(self): |
---|
| 144 | return self._config |
---|
| 145 | @config.setter |
---|
| 146 | def config(self, val): |
---|
| 147 | try: |
---|
| 148 | _config = dict(val).copy() |
---|
| 149 | except: |
---|
| 150 | raise ValueError, "not a dictionary" |
---|
| 151 | if 'ipp-versions' not in _config: |
---|
| 152 | raise ValueError, "missing ipp-versions" |
---|
| 153 | self._config = _config |
---|
| 154 | |
---|
| 155 | @property |
---|
[b01b6d1] | 156 | def uris(self): |
---|
| 157 | uris = ["ipp://localhost:8000/printers/" + self.name, |
---|
| 158 | "ipp://localhost/printers/" + self.name] |
---|
| 159 | return uris |
---|
| 160 | |
---|
| 161 | @property |
---|
| 162 | def uri(self): |
---|
| 163 | return self.uris[0] |
---|
| 164 | |
---|
| 165 | @property |
---|
[cf0d7e8] | 166 | @sync |
---|
[eee389a] | 167 | def state(self): |
---|
[d42236e] | 168 | if self.is_running and not self.paused: |
---|
| 169 | if len(self.active_jobs) > 0: |
---|
| 170 | state = States.PROCESSING |
---|
| 171 | else: |
---|
| 172 | state = States.IDLE |
---|
[cf0d7e8] | 173 | else: |
---|
[5e70cc2] | 174 | state = States.STOPPED |
---|
[d42236e] | 175 | |
---|
[5e70cc2] | 176 | return state |
---|
[eee389a] | 177 | |
---|
| 178 | @property |
---|
[cf0d7e8] | 179 | @sync |
---|
[eee389a] | 180 | def active_jobs(self): |
---|
[cf0d7e8] | 181 | jobs = self.pending_jobs[:] |
---|
| 182 | if self.current_job is not None: |
---|
| 183 | jobs.insert(0, self.current_job.id) |
---|
[eee389a] | 184 | return jobs |
---|
[b01b6d1] | 185 | |
---|
[33528b4] | 186 | @property |
---|
| 187 | def is_running(self): |
---|
| 188 | running = self.ident is not None and self.isAlive() and self._running |
---|
| 189 | return running |
---|
| 190 | |
---|
[b01b6d1] | 191 | ###################################################################### |
---|
| 192 | ### Methods ### |
---|
| 193 | ###################################################################### |
---|
| 194 | |
---|
[cf0d7e8] | 195 | @sync |
---|
[5e70cc2] | 196 | def assert_running(self): |
---|
| 197 | if not self.is_running: |
---|
| 198 | raise RuntimeError, "%s not started" % str(self) |
---|
| 199 | |
---|
| 200 | @sync |
---|
[eee389a] | 201 | def start_job(self): |
---|
[5e70cc2] | 202 | self.assert_running() |
---|
[33528b4] | 203 | if not self.paused and self.current_job is None: |
---|
[cf0d7e8] | 204 | try: |
---|
[d42236e] | 205 | job_id = self.pending_jobs.pop(0) |
---|
[cf0d7e8] | 206 | self.current_job = self.get_job(job_id) |
---|
| 207 | self.current_job.play() |
---|
| 208 | except IndexError: |
---|
| 209 | self.current_job = None |
---|
[eee389a] | 210 | |
---|
[cf0d7e8] | 211 | @sync |
---|
[eee389a] | 212 | def complete_job(self): |
---|
[5e70cc2] | 213 | self.assert_running() |
---|
[33528b4] | 214 | if not self.paused and self.current_job is not None: |
---|
| 215 | try: |
---|
| 216 | if not self.current_job.is_done: |
---|
| 217 | self.current_job.stop() |
---|
| 218 | finally: |
---|
| 219 | self.finished_jobs.append(self.current_job.id) |
---|
| 220 | self.current_job = None |
---|
[1a63bf7] | 221 | |
---|
[cf0d7e8] | 222 | @sync |
---|
[eee389a] | 223 | def get_job(self, job_id): |
---|
[5e70cc2] | 224 | self.assert_running() |
---|
[cf0d7e8] | 225 | if job_id not in self.jobs: |
---|
| 226 | raise InvalidJobException(job_id) |
---|
| 227 | return self.jobs[job_id] |
---|
[b01b6d1] | 228 | |
---|
| 229 | ###################################################################### |
---|
| 230 | ### IPP Attributes ### |
---|
| 231 | ###################################################################### |
---|
[1a63bf7] | 232 | |
---|
[b2e077a] | 233 | @property |
---|
| 234 | def printer_uri_supported(self): |
---|
[5e70cc2] | 235 | self.assert_running() |
---|
[793432f] | 236 | return ipp.PrinterUriSupported(self.uri) |
---|
[9da7428] | 237 | @printer_uri_supported.setter |
---|
| 238 | def printer_uri_supported(self, val): |
---|
[5e70cc2] | 239 | self.assert_running() |
---|
[9da7428] | 240 | raise ipp.errors.AttributesNotSettable("printer-uri-supported") |
---|
[1a63bf7] | 241 | |
---|
[b2e077a] | 242 | @property |
---|
| 243 | def uri_authentication_supported(self): |
---|
[5e70cc2] | 244 | self.assert_running() |
---|
[793432f] | 245 | return ipp.UriAuthenticationSupported("none") |
---|
[9da7428] | 246 | @uri_authentication_supported.setter |
---|
| 247 | def uri_authentication_supported(self, val): |
---|
[5e70cc2] | 248 | self.assert_running() |
---|
[9da7428] | 249 | raise ipp.errors.AttributesNotSettable("uri-authentication-supported") |
---|
[b2e077a] | 250 | |
---|
| 251 | @property |
---|
| 252 | def uri_security_supported(self): |
---|
[5e70cc2] | 253 | self.assert_running() |
---|
[793432f] | 254 | return ipp.UriSecuritySupported("none") |
---|
[9da7428] | 255 | @uri_security_supported.setter |
---|
| 256 | def uri_security_supported(self, val): |
---|
[5e70cc2] | 257 | self.assert_running() |
---|
[9da7428] | 258 | raise ipp.errors.AttributesNotSettable("uri-security-supported") |
---|
[b2e077a] | 259 | |
---|
| 260 | @property |
---|
| 261 | def printer_name(self): |
---|
[5e70cc2] | 262 | self.assert_running() |
---|
[793432f] | 263 | return ipp.PrinterName(self.name) |
---|
[9da7428] | 264 | @printer_name.setter |
---|
| 265 | def printer_name(self, val): |
---|
[5e70cc2] | 266 | self.assert_running() |
---|
[9da7428] | 267 | raise ipp.errors.AttributesNotSettable("printer-name") |
---|
[1a63bf7] | 268 | |
---|
[b2e077a] | 269 | @property |
---|
| 270 | def printer_state(self): |
---|
[5e70cc2] | 271 | self.assert_running() |
---|
[b01b6d1] | 272 | return ipp.PrinterState(self.state) |
---|
[9da7428] | 273 | @printer_state.setter |
---|
| 274 | def printer_state(self, val): |
---|
[5e70cc2] | 275 | self.assert_running() |
---|
[9da7428] | 276 | raise ipp.errors.AttributesNotSettable("printer-state") |
---|
[1a63bf7] | 277 | |
---|
[b2e077a] | 278 | @property |
---|
| 279 | def printer_state_reasons(self): |
---|
[5e70cc2] | 280 | self.assert_running() |
---|
[793432f] | 281 | return ipp.PrinterStateReasons("none") |
---|
[9da7428] | 282 | @printer_state_reasons.setter |
---|
| 283 | def printer_state_reasons(self, val): |
---|
[5e70cc2] | 284 | self.assert_running() |
---|
[9da7428] | 285 | raise ipp.errors.AttributesNotSettable("printer-state-reasons") |
---|
[b2e077a] | 286 | |
---|
| 287 | @property |
---|
| 288 | def ipp_versions_supported(self): |
---|
[5e70cc2] | 289 | self.assert_running() |
---|
[609a9b0] | 290 | return ipp.IppVersionsSupported(*self.config['ipp-versions']) |
---|
[9da7428] | 291 | @ipp_versions_supported.setter |
---|
| 292 | def ipp_versions_supported(self, val): |
---|
[5e70cc2] | 293 | self.assert_running() |
---|
[9da7428] | 294 | raise ipp.errors.AttributesNotSettable("ipp-versions-supported") |
---|
[1a63bf7] | 295 | |
---|
[f6e2532] | 296 | # XXX: We should query ourself for the supported operations |
---|
[b2e077a] | 297 | @property |
---|
| 298 | def operations_supported(self): |
---|
[5e70cc2] | 299 | self.assert_running() |
---|
[793432f] | 300 | return ipp.OperationsSupported(ipp.OperationCodes.GET_JOBS) |
---|
[9da7428] | 301 | @operations_supported.setter |
---|
| 302 | def operations_supported(self, val): |
---|
[5e70cc2] | 303 | self.assert_running() |
---|
[9da7428] | 304 | raise ipp.errors.AttributesNotSettable("operations-supported") |
---|
[b2e077a] | 305 | |
---|
| 306 | @property |
---|
| 307 | def charset_configured(self): |
---|
[5e70cc2] | 308 | self.assert_running() |
---|
[9da7428] | 309 | return ipp.CharsetConfigured("utf-8") # XXX |
---|
| 310 | @charset_configured.setter |
---|
| 311 | def charset_configured(self, val): |
---|
[5e70cc2] | 312 | self.assert_running() |
---|
[9da7428] | 313 | raise ipp.errors.AttributesNotSettable("charset-configured") |
---|
| 314 | |
---|
[b2e077a] | 315 | @property |
---|
| 316 | def charset_supported(self): |
---|
[5e70cc2] | 317 | self.assert_running() |
---|
[9da7428] | 318 | return ipp.CharsetSupported("utf-8") # XXX |
---|
| 319 | @charset_supported.setter |
---|
| 320 | def charset_supported(self, val): |
---|
[5e70cc2] | 321 | self.assert_running() |
---|
[9da7428] | 322 | raise ipp.errors.AttributesNotSettable("charset-supported") |
---|
[b2e077a] | 323 | |
---|
| 324 | @property |
---|
| 325 | def natural_language_configured(self): |
---|
[5e70cc2] | 326 | self.assert_running() |
---|
[793432f] | 327 | return ipp.NaturalLanguageConfigured("en-us") |
---|
[9da7428] | 328 | @natural_language_configured.setter |
---|
| 329 | def natural_language_configured(self, val): |
---|
[5e70cc2] | 330 | self.assert_running() |
---|
[9da7428] | 331 | raise ipp.errors.AttributesNotSettable("natural-language-configured") |
---|
[b2e077a] | 332 | |
---|
| 333 | @property |
---|
| 334 | def generated_natural_language_supported(self): |
---|
[5e70cc2] | 335 | self.assert_running() |
---|
[793432f] | 336 | return ipp.GeneratedNaturalLanguageSupported("en-us") |
---|
[9da7428] | 337 | @generated_natural_language_supported.setter |
---|
| 338 | def generated_natural_language_supported(self, val): |
---|
[5e70cc2] | 339 | self.assert_running() |
---|
[9da7428] | 340 | raise ipp.errors.AttributesNotSettable("generated-natural-language-supported") |
---|
[b2e077a] | 341 | |
---|
| 342 | @property |
---|
| 343 | def document_format_default(self): |
---|
[5e70cc2] | 344 | self.assert_running() |
---|
[793432f] | 345 | return ipp.DocumentFormatDefault("application/octet-stream") |
---|
[9da7428] | 346 | @document_format_default.setter |
---|
| 347 | def document_format_default(self, val): |
---|
[5e70cc2] | 348 | self.assert_running() |
---|
[9da7428] | 349 | raise ipp.errors.AttributesNotSettable("document-format-default") |
---|
[b2e077a] | 350 | |
---|
| 351 | @property |
---|
| 352 | def document_format_supported(self): |
---|
[5e70cc2] | 353 | self.assert_running() |
---|
[793432f] | 354 | return ipp.DocumentFormatSupported("application/octet-stream", "audio/mp3") |
---|
[9da7428] | 355 | @document_format_supported.setter |
---|
| 356 | def document_format_supported(self, val): |
---|
[5e70cc2] | 357 | self.assert_running() |
---|
[9da7428] | 358 | raise ipp.errors.AttributesNotSettable("document-format-supported") |
---|
[b2e077a] | 359 | |
---|
| 360 | @property |
---|
| 361 | def printer_is_accepting_jobs(self): |
---|
[5e70cc2] | 362 | self.assert_running() |
---|
[793432f] | 363 | return ipp.PrinterIsAcceptingJobs(True) |
---|
[9da7428] | 364 | @printer_is_accepting_jobs.setter |
---|
| 365 | def printer_is_accepting_jobs(self, val): |
---|
[5e70cc2] | 366 | self.assert_running() |
---|
[9da7428] | 367 | raise ipp.errors.AttributesNotSettable("printer-is-accepting-jobs") |
---|
[b2e077a] | 368 | |
---|
| 369 | @property |
---|
| 370 | def queued_job_count(self): |
---|
[5e70cc2] | 371 | self.assert_running() |
---|
[793432f] | 372 | return ipp.QueuedJobCount(len(self.active_jobs)) |
---|
[9da7428] | 373 | @queued_job_count.setter |
---|
| 374 | def queued_job_count(self, val): |
---|
[5e70cc2] | 375 | self.assert_running() |
---|
[9da7428] | 376 | raise ipp.errors.AttributesNotSettable("queued-job-count") |
---|
[b2e077a] | 377 | |
---|
| 378 | @property |
---|
| 379 | def pdl_override_supported(self): |
---|
[5e70cc2] | 380 | self.assert_running() |
---|
[793432f] | 381 | return ipp.PdlOverrideSupported("not-attempted") |
---|
[9da7428] | 382 | @pdl_override_supported.setter |
---|
| 383 | def pdl_override_supported(self, val): |
---|
[5e70cc2] | 384 | self.assert_running() |
---|
[9da7428] | 385 | raise ipp.errors.AttributesNotSettable("pdl-override-supported") |
---|
[b2e077a] | 386 | |
---|
| 387 | @property |
---|
| 388 | def printer_up_time(self): |
---|
[5e70cc2] | 389 | self.assert_running() |
---|
[793432f] | 390 | return ipp.PrinterUpTime(int(time.time()) - self.time_created) |
---|
[9da7428] | 391 | @printer_up_time.setter |
---|
| 392 | def printer_up_time(self, val): |
---|
[5e70cc2] | 393 | self.assert_running() |
---|
[9da7428] | 394 | raise ipp.errors.AttributesNotSettable("printer-up-time") |
---|
[b2e077a] | 395 | |
---|
| 396 | @property |
---|
| 397 | def compression_supported(self): |
---|
[5e70cc2] | 398 | self.assert_running() |
---|
[793432f] | 399 | return ipp.CompressionSupported("none") |
---|
[9da7428] | 400 | @compression_supported.setter |
---|
| 401 | def compression_supported(self, val): |
---|
[5e70cc2] | 402 | self.assert_running() |
---|
[9da7428] | 403 | raise ipp.errors.AttributesNotSettable("compression-supported") |
---|
[b2e077a] | 404 | |
---|
[f6e2532] | 405 | @property |
---|
| 406 | def multiple_operation_time_out(self): |
---|
[5e70cc2] | 407 | self.assert_running() |
---|
[793432f] | 408 | return ipp.MultipleOperationTimeOut(240) |
---|
[9da7428] | 409 | @multiple_operation_time_out.setter |
---|
| 410 | def multiple_operation_time_out(self, val): |
---|
[5e70cc2] | 411 | self.assert_running() |
---|
[9da7428] | 412 | raise ipp.errors.AttributesNotSettable("multiple-operation-time-out") |
---|
[f6e2532] | 413 | |
---|
| 414 | @property |
---|
| 415 | def multiple_document_jobs_supported(self): |
---|
[5e70cc2] | 416 | self.assert_running() |
---|
[793432f] | 417 | return ipp.MultipleDocumentJobsSupported(False) |
---|
[9da7428] | 418 | @multiple_document_jobs_supported.setter |
---|
| 419 | def multiple_document_jobs_supported(self, val): |
---|
[5e70cc2] | 420 | self.assert_running() |
---|
[9da7428] | 421 | raise ipp.errors.AttributesNotSettable("multiple-document-jobs-supported") |
---|
[f6e2532] | 422 | |
---|
[33ea505] | 423 | ###################################################################### |
---|
| 424 | ### Job IPP Attributes ### |
---|
| 425 | ###################################################################### |
---|
| 426 | |
---|
| 427 | def job_id(self, job_id): |
---|
[5e70cc2] | 428 | self.assert_running() |
---|
[33ea505] | 429 | job = self.get_job(job_id) |
---|
| 430 | return ipp.JobId(job.id) |
---|
| 431 | |
---|
| 432 | def job_name(self, job_id): |
---|
[5e70cc2] | 433 | self.assert_running() |
---|
[33ea505] | 434 | job = self.get_job(job_id) |
---|
| 435 | return ipp.JobName(job.name) |
---|
| 436 | |
---|
| 437 | def job_originating_user_name(self, job_id): |
---|
[5e70cc2] | 438 | self.assert_running() |
---|
[33ea505] | 439 | job = self.get_job(job_id) |
---|
| 440 | return ipp.JobOriginatingUserName(job.creator) |
---|
| 441 | |
---|
| 442 | def job_k_octets(self, job_id): |
---|
[5e70cc2] | 443 | self.assert_running() |
---|
[33ea505] | 444 | job = self.get_job(job_id) |
---|
| 445 | return ipp.JobKOctets(job.size) |
---|
| 446 | |
---|
| 447 | def job_state(self, job_id): |
---|
[5e70cc2] | 448 | self.assert_running() |
---|
[33ea505] | 449 | job = self.get_job(job_id) |
---|
| 450 | return ipp.JobState(job.state) |
---|
| 451 | |
---|
| 452 | def job_printer_uri(self, job_id): |
---|
[5e70cc2] | 453 | self.assert_running() |
---|
[33ea505] | 454 | job = self.get_job(job_id) |
---|
| 455 | return ipp.JobPrinterUri(self.uri) |
---|
[ee8e6d0] | 456 | |
---|
[b01b6d1] | 457 | ###################################################################### |
---|
| 458 | ### IPP Operations ### |
---|
| 459 | ###################################################################### |
---|
| 460 | |
---|
[5e70cc2] | 461 | @sync |
---|
[57bc2dc] | 462 | def print_job(self, document, document_name=None, document_format=None, |
---|
| 463 | document_natural_language=None, requesting_user_name=None, |
---|
| 464 | compression=None, job_name=None, job_k_octets=None): |
---|
[b3a56af] | 465 | """RFC 2911: 3.2.1 Print-Job Operation |
---|
| 466 | |
---|
| 467 | This REQUIRED operation allows a client to submit a print job |
---|
| 468 | with only one document and supply the document data (rather |
---|
| 469 | than just a reference to the data). See Section 15 for the |
---|
| 470 | suggested steps for processing create operations and their |
---|
| 471 | Operation and Job Template attributes. |
---|
| 472 | |
---|
| 473 | Parameters |
---|
| 474 | ---------- |
---|
| 475 | document (file) |
---|
| 476 | an open file handler to the document |
---|
| 477 | document_name (string) |
---|
| 478 | the name of the document |
---|
| 479 | document_format (string) |
---|
| 480 | the encoding/format of the document |
---|
| 481 | document_natural_language (string) |
---|
| 482 | if the document is a text file, what language it is in |
---|
| 483 | requesting_user_name (string) |
---|
| 484 | the user name of the job owner |
---|
| 485 | compression (string) |
---|
| 486 | the form of compression used on the file |
---|
| 487 | job_name (string) |
---|
| 488 | the name that the job should be called |
---|
| 489 | job_k_octets (int) |
---|
| 490 | the size of the job in bytes |
---|
[b01b6d1] | 491 | |
---|
[b3a56af] | 492 | """ |
---|
| 493 | |
---|
[5e70cc2] | 494 | self.assert_running() |
---|
| 495 | |
---|
[57bc2dc] | 496 | # create the job |
---|
| 497 | job_id = self.create_job( |
---|
| 498 | requesting_user_name=requesting_user_name, |
---|
| 499 | job_name=job_name, |
---|
| 500 | job_k_octets=job_k_octets) |
---|
| 501 | |
---|
| 502 | # send the document |
---|
| 503 | self.send_document( |
---|
| 504 | job_id, |
---|
| 505 | document, |
---|
| 506 | document_name=document_name, |
---|
| 507 | document_format=document_format, |
---|
| 508 | document_natural_language=document_natural_language, |
---|
| 509 | requesting_user_name=requesting_user_name, |
---|
| 510 | compression=compression, |
---|
| 511 | last_document=False) |
---|
| 512 | |
---|
| 513 | return job_id |
---|
| 514 | |
---|
[5e70cc2] | 515 | @sync |
---|
[7e29e6a] | 516 | def validate_job(self, document_name=None, document_format=None, |
---|
| 517 | document_natural_language=None, requesting_user_name=None, |
---|
| 518 | compression=None, job_name=None, job_k_octets=None): |
---|
[b3a56af] | 519 | """RFC 2911: 3.2.3 Validate-Job Operation |
---|
| 520 | |
---|
| 521 | This REQUIRED operation is similar to the Print-Job operation |
---|
| 522 | (section 3.2.1) except that a client supplies no document data |
---|
| 523 | and the Printer allocates no resources (i.e., it does not |
---|
| 524 | create a new Job object). This operation is used only to |
---|
| 525 | verify capabilities of a printer object against whatever |
---|
| 526 | attributes are supplied by the client in the Validate-Job |
---|
| 527 | request. By using the Validate-Job operation a client can |
---|
| 528 | validate that an identical Print-Job operation (with the |
---|
| 529 | document data) would be accepted. The Validate-Job operation |
---|
| 530 | also performs the same security negotiation as the Print-Job |
---|
| 531 | operation (see section 8), so that a client can check that the |
---|
| 532 | client and Printer object security requirements can be met |
---|
| 533 | before performing a Print-Job operation. |
---|
| 534 | |
---|
| 535 | The Validate-Job operation does not accept a 'document-uri' |
---|
| 536 | attribute in order to allow a client to check that the same |
---|
| 537 | Print-URI operation will be accepted, since the client doesn't |
---|
| 538 | send the data with the Print-URI operation. The client SHOULD |
---|
| 539 | just issue the Print-URI request. |
---|
| 540 | |
---|
| 541 | Parameters |
---|
| 542 | ---------- |
---|
| 543 | document (file) |
---|
| 544 | an open file handler to the document |
---|
| 545 | document_name (string) |
---|
| 546 | the name of the document |
---|
| 547 | document_format (string) |
---|
| 548 | the encoding/format of the document |
---|
| 549 | document_natural_language (string) |
---|
| 550 | if the document is a text file, what language it is in |
---|
| 551 | requesting_user_name (string) |
---|
| 552 | the user name of the job owner |
---|
| 553 | compression (string) |
---|
| 554 | the form of compression used on the file |
---|
| 555 | job_name (string) |
---|
| 556 | the name that the job should be called |
---|
| 557 | job_k_octets (int) |
---|
| 558 | the size of the job in bytes |
---|
[57bc2dc] | 559 | |
---|
[b3a56af] | 560 | """ |
---|
| 561 | |
---|
[5e70cc2] | 562 | self.assert_running() |
---|
| 563 | |
---|
[57bc2dc] | 564 | job_id = self._next_job_id |
---|
| 565 | job = GutenbachJob( |
---|
| 566 | job_id, |
---|
| 567 | creator=requesting_user_name, |
---|
| 568 | name=job_name) |
---|
| 569 | job.spool(tempfile.TemporaryFile()) |
---|
| 570 | job.abort() |
---|
| 571 | del job |
---|
[b01b6d1] | 572 | |
---|
[5e70cc2] | 573 | @sync |
---|
[33ea505] | 574 | def get_jobs(self, requesting_user_name=None, which_jobs=None, |
---|
| 575 | requested_attributes=None): |
---|
[b3a56af] | 576 | """RFC 2911: 3.2.6 Get-Jobs Operation |
---|
| 577 | |
---|
| 578 | This REQUIRED operation allows a client to retrieve the list |
---|
| 579 | of Job objects belonging to the target Printer object. The |
---|
| 580 | client may also supply a list of Job attribute names and/or |
---|
| 581 | attribute group names. A group of Job object attributes will |
---|
| 582 | be returned for each Job object that is returned. |
---|
| 583 | |
---|
| 584 | This operation is similar to the Get-Job-Attributes operation, |
---|
| 585 | except that this Get-Jobs operation returns attributes from |
---|
| 586 | possibly more than one object. |
---|
| 587 | |
---|
| 588 | Parameters |
---|
| 589 | ---------- |
---|
| 590 | requesting_user_name (string) |
---|
| 591 | the user name of the job owner, used as a filter |
---|
| 592 | which_jobs (string) |
---|
| 593 | a filter for the types of jobs to return: |
---|
| 594 | * 'completed' -- only jobs that have finished |
---|
| 595 | * 'not-completed' -- processing or pending jobs |
---|
| 596 | this defaults to 'not-completed' |
---|
| 597 | requested_attributes (list) |
---|
| 598 | the job attributes to return |
---|
| 599 | |
---|
| 600 | """ |
---|
[33ea505] | 601 | |
---|
[5e70cc2] | 602 | self.assert_running() |
---|
| 603 | |
---|
[b01b6d1] | 604 | # Filter by the which-jobs attribute |
---|
| 605 | if which_jobs is None: |
---|
[34a4e5d] | 606 | which_jobs = "not-completed" |
---|
| 607 | |
---|
| 608 | if which_jobs == "completed": |
---|
[b01b6d1] | 609 | jobs = [self.jobs[job_id] for job_id in self.finished_jobs] |
---|
| 610 | elif which_jobs == "not-completed": |
---|
| 611 | jobs = [self.jobs[job_id] for job_id in self.active_jobs] |
---|
[ee8e6d0] | 612 | else: |
---|
[b01b6d1] | 613 | raise ipp.errors.ClientErrorAttributes( |
---|
| 614 | which_jobs, ipp.WhichJobs(which_jobs)) |
---|
[b2e077a] | 615 | |
---|
[b01b6d1] | 616 | # Filter by username |
---|
| 617 | if requesting_user_name is None: |
---|
| 618 | user_jobs = jobs |
---|
| 619 | else: |
---|
| 620 | user_jobs = [job for job in jobs if job.creator == requesting_user_name] |
---|
[33ea505] | 621 | |
---|
| 622 | # Get the attributes of each job |
---|
| 623 | job_attrs = [self.get_job_attributes( |
---|
| 624 | job.id, requested_attributes=requested_attributes) for job in user_jobs] |
---|
[ee8e6d0] | 625 | |
---|
[33ea505] | 626 | return job_attrs |
---|
[ee8e6d0] | 627 | |
---|
[5e70cc2] | 628 | @sync |
---|
[b01b6d1] | 629 | def print_uri(self): |
---|
[b3a56af] | 630 | """RFC 2911: 3.2.2 Print-URI Operation |
---|
| 631 | |
---|
| 632 | This OPTIONAL operation is identical to the Print-Job |
---|
| 633 | operation (section 3.2.1) except that a client supplies a URI |
---|
| 634 | reference to the document data using the 'document-uri' (uri) |
---|
| 635 | operation attribute (in Group 1) rather than including the |
---|
| 636 | document data itself. Before returning the response, the |
---|
| 637 | Printer MUST validate that the Printer supports the retrieval |
---|
| 638 | method (e.g., http, ftp, etc.) implied by the URI, and MUST |
---|
| 639 | check for valid URI syntax. If the client-supplied URI scheme |
---|
| 640 | is not supported, i.e. the value is not in the Printer |
---|
| 641 | object's 'referenced-uri-scheme-supported' attribute, the |
---|
| 642 | Printer object MUST reject the request and return the |
---|
| 643 | 'client-error-uri- scheme-not-supported' status code. |
---|
| 644 | |
---|
| 645 | If the Printer object supports this operation, it MUST support |
---|
| 646 | the 'reference-uri-schemes-supported' Printer attribute (see |
---|
| 647 | section 4.4.27). |
---|
| 648 | |
---|
| 649 | It is up to the IPP object to interpret the URI and |
---|
| 650 | subsequently 'pull' the document from the source referenced by |
---|
| 651 | the URI string. |
---|
| 652 | |
---|
| 653 | """ |
---|
| 654 | |
---|
[5e70cc2] | 655 | self.assert_running() |
---|
[b3a56af] | 656 | # XXX: todo |
---|
[b01b6d1] | 657 | |
---|
[5e70cc2] | 658 | @sync |
---|
[d42236e] | 659 | def create_job(self, requesting_user_name=None, |
---|
| 660 | job_name=None, job_k_octets=None): |
---|
[b3a56af] | 661 | """RFC 2911: 3.2.4 Create-Job Operation |
---|
| 662 | |
---|
| 663 | This OPTIONAL operation is similar to the Print-Job operation |
---|
| 664 | (section 3.2.1) except that in the Create-Job request, a |
---|
| 665 | client does not supply document data or any reference to |
---|
| 666 | document data. Also, the client does not supply any of the |
---|
| 667 | 'document-name', 'document- format', 'compression', or |
---|
| 668 | 'document-natural-language' operation attributes. This |
---|
| 669 | operation is followed by one or more Send-Document or Send-URI |
---|
| 670 | operations. In each of those operation requests, the client |
---|
| 671 | OPTIONALLY supplies the 'document-name', 'document-format', |
---|
| 672 | and 'document-natural-language' attributes for each document |
---|
| 673 | in the multi-document Job object. |
---|
| 674 | |
---|
| 675 | Parameters |
---|
| 676 | ---------- |
---|
| 677 | requesting_user_name (string) |
---|
| 678 | the user name of the job owner |
---|
| 679 | job_name (string) |
---|
| 680 | the name that the job should be called |
---|
| 681 | job_k_octets (int) |
---|
| 682 | the size of the job in bytes |
---|
[57bc2dc] | 683 | |
---|
[b3a56af] | 684 | """ |
---|
| 685 | |
---|
[5e70cc2] | 686 | self.assert_running() |
---|
| 687 | |
---|
[eee389a] | 688 | job_id = self._next_job_id |
---|
| 689 | self._next_job_id += 1 |
---|
[ee8e6d0] | 690 | |
---|
[33ea505] | 691 | job = GutenbachJob( |
---|
| 692 | job_id, |
---|
| 693 | creator=requesting_user_name, |
---|
| 694 | name=job_name) |
---|
[57bc2dc] | 695 | |
---|
[ee8e6d0] | 696 | self.jobs[job_id] = job |
---|
[33ea505] | 697 | return job_id |
---|
[776a659] | 698 | |
---|
[fa3e2c6] | 699 | @sync |
---|
[b01b6d1] | 700 | def pause_printer(self): |
---|
[b3a56af] | 701 | """RFC 2911: 3.2.7 Pause-Printer Operation |
---|
| 702 | |
---|
| 703 | This OPTIONAL operation allows a client to stop the Printer |
---|
| 704 | object from scheduling jobs on all its devices. Depending on |
---|
| 705 | implementation, the Pause-Printer operation MAY also stop the |
---|
| 706 | Printer from processing the current job or jobs. Any job that |
---|
| 707 | is currently being printed is either stopped as soon as the |
---|
| 708 | implementation permits or is completed, depending on |
---|
| 709 | implementation. The Printer object MUST still accept create |
---|
| 710 | operations to create new jobs, but MUST prevent any jobs from |
---|
| 711 | entering the 'processing' state. |
---|
| 712 | |
---|
| 713 | If the Pause-Printer operation is supported, then the |
---|
| 714 | Resume-Printer operation MUST be supported, and vice-versa. |
---|
| 715 | |
---|
| 716 | The IPP Printer MUST accept the request in any state and |
---|
| 717 | transition the Printer to the indicated new 'printer-state' |
---|
| 718 | before returning as follows: |
---|
| 719 | |
---|
| 720 | Current New Reasons Reponse |
---|
| 721 | -------------------------------------------------------------- |
---|
| 722 | 'idle' 'stopped' 'paused' 'successful-ok' |
---|
| 723 | 'processing' 'processing' 'moving-to-paused' 'successful-ok' |
---|
| 724 | 'processing' 'stopped' 'paused' 'successful-ok' |
---|
| 725 | 'stopped' 'stopped' 'paused' 'successful-ok' |
---|
[fa3e2c6] | 726 | |
---|
| 727 | """ |
---|
[33528b4] | 728 | |
---|
[5e70cc2] | 729 | self.assert_running() |
---|
[33528b4] | 730 | if not self.paused: |
---|
| 731 | if self.current_job is not None and self.current_job.is_playing: |
---|
| 732 | self.current_job.pause() |
---|
| 733 | self.paused = True |
---|
| 734 | logger.info("%s paused", str(self)) |
---|
[fa3e2c6] | 735 | |
---|
| 736 | @sync |
---|
[b01b6d1] | 737 | def resume_printer(self): |
---|
[b3a56af] | 738 | """RFC 2911: 3.2.8 Resume-Printer Operation |
---|
| 739 | |
---|
| 740 | This operation allows a client to resume the Printer object |
---|
| 741 | scheduling jobs on all its devices. The Printer object MUST |
---|
| 742 | remove the 'paused' and 'moving-to-paused' values from the |
---|
| 743 | Printer object's 'printer-state-reasons' attribute, if |
---|
| 744 | present. If there are no other reasons to keep a device |
---|
| 745 | paused (such as media-jam), the IPP Printer is free to |
---|
| 746 | transition itself to the 'processing' or 'idle' states, |
---|
| 747 | depending on whether there are jobs to be processed or not, |
---|
| 748 | respectively, and the device(s) resume processing jobs. |
---|
| 749 | |
---|
| 750 | If the Pause-Printer operation is supported, then the |
---|
| 751 | Resume-Printer operation MUST be supported, and vice-versa. |
---|
| 752 | |
---|
| 753 | The IPP Printer removes the 'printer-stopped' value from any |
---|
| 754 | job's 'job-state-reasons' attributes contained in that |
---|
| 755 | Printer. |
---|
| 756 | |
---|
| 757 | The IPP Printer MUST accept the request in any state, |
---|
| 758 | transition the Printer object to the indicated new state as |
---|
| 759 | follows: |
---|
| 760 | |
---|
| 761 | Current New Response |
---|
| 762 | --------------------------------------------- |
---|
| 763 | 'idle' 'idle' 'successful-ok' |
---|
| 764 | 'processing' 'processing' 'successful-ok' |
---|
| 765 | 'stopped' 'processing' 'successful-ok' |
---|
| 766 | 'stopped' 'idle' 'successful-ok' |
---|
[fa3e2c6] | 767 | |
---|
| 768 | """ |
---|
[33528b4] | 769 | |
---|
[5e70cc2] | 770 | self.assert_running() |
---|
[33528b4] | 771 | if self.paused: |
---|
| 772 | if self.current_job is not None: |
---|
| 773 | self.current_job.resume() |
---|
| 774 | self.paused = False |
---|
| 775 | logger.info("%s unpaused", str(self)) |
---|
[776a659] | 776 | |
---|
[57bc2dc] | 777 | @sync |
---|
[b01b6d1] | 778 | def get_printer_attributes(self, requested_attributes=None): |
---|
[b3a56af] | 779 | """RFC 2911: 3.2.5 Get-Printer-Attributes Operation |
---|
| 780 | |
---|
| 781 | This REQUIRED operation allows a client to request the values |
---|
| 782 | of the attributes of a Printer object. |
---|
| 783 | |
---|
| 784 | In the request, the client supplies the set of Printer |
---|
| 785 | attribute names and/or attribute group names in which the |
---|
| 786 | requester is interested. In the response, the Printer object |
---|
| 787 | returns a corresponding attribute set with the appropriate |
---|
| 788 | attribute values filled in. |
---|
| 789 | |
---|
| 790 | Parameters |
---|
| 791 | ---------- |
---|
| 792 | requested_attributes (list) |
---|
| 793 | the attributes to return |
---|
| 794 | |
---|
| 795 | """ |
---|
| 796 | |
---|
[5e70cc2] | 797 | self.assert_running() |
---|
[b01b6d1] | 798 | if requested_attributes is None: |
---|
[33ea505] | 799 | requested = self.printer_attributes |
---|
[e58af05] | 800 | else: |
---|
[33ea505] | 801 | requested = [a for a in self.printer_attributes \ |
---|
| 802 | if a in requested_attributes] |
---|
[b2e077a] | 803 | |
---|
[b01b6d1] | 804 | _attributes = [attr.replace("-", "_") for attr in requested] |
---|
| 805 | attributes = [getattr(self, attr) for attr in _attributes] |
---|
| 806 | return attributes |
---|
[776a659] | 807 | |
---|
[57bc2dc] | 808 | @sync |
---|
[7e29e6a] | 809 | def set_printer_attributes(self, attributes): |
---|
[5e70cc2] | 810 | self.assert_running() |
---|
[9da7428] | 811 | for attr in attributes: |
---|
| 812 | try: |
---|
| 813 | setattr(self, attr, attributes[attr]) |
---|
| 814 | except AttributeError: |
---|
| 815 | raise ipp.errors.ClientErrorAttributes |
---|
[33ea505] | 816 | |
---|
[57bc2dc] | 817 | @sync |
---|
[33ea505] | 818 | def cancel_job(self, job_id, requesting_user_name=None): |
---|
[b3a56af] | 819 | """RFC 2911: 3.3.3 Cancel-Job Operation |
---|
| 820 | |
---|
| 821 | This REQUIRED operation allows a client to cancel a Print Job |
---|
| 822 | from the time the job is created up to the time it is |
---|
| 823 | completed, canceled, or aborted. Since a Job might already be |
---|
| 824 | printing by the time a Cancel-Job is received, some media |
---|
| 825 | sheet pages might be printed before the job is actually |
---|
| 826 | terminated. |
---|
| 827 | |
---|
| 828 | The IPP object MUST accept or reject the request based on the |
---|
| 829 | job's current state and transition the job to the indicated |
---|
| 830 | new state as follows: |
---|
| 831 | |
---|
| 832 | Current State New State Response |
---|
| 833 | ----------------------------------------------------------------- |
---|
| 834 | pending canceled successful-ok |
---|
| 835 | pending-held canceled successful-ok |
---|
| 836 | processing canceled successful-ok |
---|
| 837 | processing processing successful-ok See Rule 1 |
---|
| 838 | processing processing client-error-not-possible See Rule 2 |
---|
| 839 | processing-stopped canceled successful-ok |
---|
| 840 | processing-stopped processing-stopped successful-ok See Rule 1 |
---|
| 841 | processing-stopped processing-stopped client-error-not-possible See Rule 2 |
---|
| 842 | completed completed client-error-not-possible |
---|
| 843 | canceled canceled client-error-not-possible |
---|
| 844 | aborted aborted client-error-not-possible |
---|
| 845 | |
---|
| 846 | Rule 1: If the implementation requires some measurable time to |
---|
| 847 | cancel the job in the 'processing' or 'processing-stopped' job |
---|
| 848 | states, the IPP object MUST add the 'processing-to-stop-point' |
---|
| 849 | value to the job's 'job-state-reasons' attribute and then |
---|
| 850 | transition the job to the 'canceled' state when the processing |
---|
| 851 | ceases (see section 4.3.8). |
---|
| 852 | |
---|
| 853 | Rule 2: If the Job object already has the |
---|
| 854 | 'processing-to-stop-point' value in its 'job-state-reasons' |
---|
| 855 | attribute, then the Printer object MUST reject a Cancel-Job |
---|
| 856 | operation. |
---|
| 857 | |
---|
| 858 | Parameters |
---|
| 859 | ---------- |
---|
| 860 | job_id (integer) |
---|
| 861 | the id of the job to cancel |
---|
| 862 | requesting_user_name (string) |
---|
| 863 | the name of the job's owner |
---|
| 864 | |
---|
| 865 | """ |
---|
| 866 | |
---|
[5e70cc2] | 867 | self.assert_running() |
---|
[33ea505] | 868 | job = self.get_job(job_id) |
---|
| 869 | try: |
---|
| 870 | job.cancel() |
---|
| 871 | except InvalidJobStateException: |
---|
| 872 | # XXX |
---|
| 873 | raise |
---|
| 874 | |
---|
[57bc2dc] | 875 | @sync |
---|
[33ea505] | 876 | def send_document(self, job_id, document, document_name=None, |
---|
| 877 | document_format=None, document_natural_language=None, |
---|
| 878 | requesting_user_name=None, compression=None, |
---|
| 879 | last_document=None): |
---|
[b3a56af] | 880 | """RFC 2911: 3.3.1 Send-Document Operation |
---|
| 881 | |
---|
| 882 | This OPTIONAL operation allows a client to create a |
---|
| 883 | multi-document Job object that is initially 'empty' (contains |
---|
| 884 | no documents). In the Create-Job response, the Printer object |
---|
| 885 | returns the Job object's URI (the 'job-uri' attribute) and the |
---|
| 886 | Job object's 32-bit identifier (the 'job-id' attribute). For |
---|
| 887 | each new document that the client desires to add, the client |
---|
| 888 | uses a Send-Document operation. Each Send- Document Request |
---|
| 889 | contains the entire stream of document data for one document. |
---|
| 890 | |
---|
| 891 | If the Printer supports this operation but does not support |
---|
| 892 | multiple documents per job, the Printer MUST reject subsequent |
---|
| 893 | Send-Document operations supplied with data and return the |
---|
| 894 | 'server-error-multiple- document-jobs-not-supported'. However, |
---|
| 895 | the Printer MUST accept the first document with a 'true' or |
---|
| 896 | 'false' value for the 'last-document' operation attribute (see |
---|
| 897 | below), so that clients MAY always submit one document jobs |
---|
| 898 | with a 'false' value for 'last-document' in the first |
---|
| 899 | Send-Document and a 'true' for 'last-document' in the second |
---|
| 900 | Send-Document (with no data). |
---|
| 901 | |
---|
| 902 | Since the Create-Job and the send operations (Send-Document or |
---|
| 903 | Send- URI operations) that follow could occur over an |
---|
| 904 | arbitrarily long period of time for a particular job, a client |
---|
| 905 | MUST send another send operation within an IPP Printer defined |
---|
| 906 | minimum time interval after the receipt of the previous |
---|
| 907 | request for the job. If a Printer object supports the |
---|
| 908 | Create-Job and Send-Document operations, the Printer object |
---|
| 909 | MUST support the 'multiple-operation-time-out' attribute (see |
---|
| 910 | section 4.4.31). This attribute indicates the minimum number |
---|
| 911 | of seconds the Printer object will wait for the next send |
---|
| 912 | operation before taking some recovery action. |
---|
| 913 | |
---|
| 914 | An IPP object MUST recover from an errant client that does not |
---|
| 915 | supply a send operation, sometime after the minimum time |
---|
| 916 | interval specified by the Printer object's |
---|
| 917 | 'multiple-operation-time-out' attribute. |
---|
| 918 | |
---|
| 919 | Parameters |
---|
| 920 | ---------- |
---|
| 921 | job_id (integer) |
---|
| 922 | the id of the job to send the document |
---|
| 923 | document (file) |
---|
| 924 | an open file handler to the document |
---|
| 925 | document_name (string) |
---|
| 926 | the name of the document |
---|
| 927 | document_format (string) |
---|
| 928 | the encoding/format of the document |
---|
| 929 | document_natural_language (string) |
---|
| 930 | if the document is a text file, what language it is in |
---|
| 931 | requesting_user_name (string) |
---|
| 932 | the user name of the job owner |
---|
| 933 | compression (string) |
---|
| 934 | the form of compression used on the file |
---|
| 935 | last_document (boolean) |
---|
| 936 | whether or not this is the last document in this job |
---|
[33ea505] | 937 | |
---|
[b3a56af] | 938 | """ |
---|
| 939 | |
---|
[5e70cc2] | 940 | self.assert_running() |
---|
[33ea505] | 941 | job = self.get_job(job_id) |
---|
[345c476] | 942 | job.spool(document) |
---|
[d42236e] | 943 | if 'dryrun' in self.config and self.config['dryrun']: |
---|
| 944 | job.player._dryrun = True |
---|
| 945 | self.pending_jobs.append(job_id) |
---|
| 946 | |
---|
[57bc2dc] | 947 | @sync |
---|
[c1dc25f] | 948 | def send_uri(self, job_id, document_uri, document_name=None, |
---|
| 949 | document_format=None, document_natural_language=None, |
---|
| 950 | requesting_user_name=None, compression=None, |
---|
| 951 | last_document=None): |
---|
[b3a56af] | 952 | """RFC 2911: 3.2.2 Send URI |
---|
| 953 | |
---|
| 954 | This OPTIONAL operation is identical to the Send-Document |
---|
| 955 | operation (see section 3.3.1) except that a client MUST supply |
---|
| 956 | a URI reference ('document-uri' operation attribute) rather |
---|
| 957 | than the document data itself. If a Printer object supports |
---|
| 958 | this operation, clients can use both Send-URI or Send-Document |
---|
| 959 | operations to add new documents to an existing multi-document |
---|
| 960 | Job object. However, if a client needs to indicate that the |
---|
| 961 | previous Send-URI or Send-Document was the last document, the |
---|
| 962 | client MUST use the Send-Document operation with no document |
---|
| 963 | data and the 'last-document' flag set to 'true' (rather than |
---|
| 964 | using a Send-URI operation with no 'document-uri' operation |
---|
| 965 | attribute). |
---|
| 966 | |
---|
| 967 | If a Printer object supports this operation, it MUST also |
---|
| 968 | support the Print-URI operation (see section 3.2.2). |
---|
| 969 | |
---|
| 970 | The Printer object MUST validate the syntax and URI scheme of |
---|
| 971 | the supplied URI before returning a response, just as in the |
---|
| 972 | Print-URI operation. The IPP Printer MAY validate the |
---|
| 973 | accessibility of the document as part of the operation or |
---|
| 974 | subsequently (see section 3.2.2). |
---|
| 975 | |
---|
| 976 | Parameters |
---|
| 977 | ---------- |
---|
| 978 | job_id (integer) |
---|
| 979 | the id of the job to send the uri |
---|
| 980 | document_uri (string) |
---|
| 981 | the uri of the document |
---|
| 982 | document_name (string) |
---|
| 983 | the name of the document |
---|
| 984 | document_format (string) |
---|
| 985 | the encoding/format of the document |
---|
| 986 | document_natural_language (string) |
---|
| 987 | if the document is a text file, what language it is in |
---|
| 988 | requesting_user_name (string) |
---|
| 989 | the user name of the job owner |
---|
| 990 | compression (string) |
---|
| 991 | the form of compression used on the file |
---|
| 992 | last_document (boolean) |
---|
| 993 | whether or not this is the last document in this job |
---|
| 994 | |
---|
| 995 | """ |
---|
[5e70cc2] | 996 | |
---|
| 997 | self.assert_running() |
---|
[c1dc25f] | 998 | job = self.get_job(job_id) |
---|
| 999 | # XXX: need to validate URI |
---|
| 1000 | # XXX: need to deal with the URI stream? |
---|
[33ea505] | 1001 | |
---|
[d42236e] | 1002 | #job.spool_uri(document_uri) |
---|
| 1003 | #if 'dryrun' in self.config and self.config['dryrun']: |
---|
| 1004 | # job.player._dryrun = True |
---|
| 1005 | #self.pending_jobs.append(job_id) |
---|
| 1006 | |
---|
[57bc2dc] | 1007 | @sync |
---|
[33ea505] | 1008 | def get_job_attributes(self, job_id, requested_attributes=None): |
---|
[b3a56af] | 1009 | """RFC 2911: 3.3.4 Get-Job-Attributes Operation |
---|
| 1010 | |
---|
| 1011 | This REQUIRED operation allows a client to request the values |
---|
| 1012 | of attributes of a Job object and it is almost identical to |
---|
| 1013 | the Get- Printer-Attributes operation (see section 3.2.5). The |
---|
| 1014 | only differences are that the operation is directed at a Job |
---|
| 1015 | object rather than a Printer object, there is no |
---|
| 1016 | 'document-format' operation attribute used when querying a Job |
---|
| 1017 | object, and the returned attribute group is a set of Job |
---|
| 1018 | object attributes rather than a set of Printer object |
---|
| 1019 | attributes. |
---|
| 1020 | |
---|
| 1021 | For Jobs, the possible names of attribute groups are: |
---|
| 1022 | - 'job-template': the subset of the Job Template attributes |
---|
| 1023 | that apply to a Job object (the first column of the table |
---|
| 1024 | in Section 4.2) that the implementation supports for Job |
---|
| 1025 | objects. |
---|
| 1026 | - 'job-description': the subset of the Job Description |
---|
| 1027 | attributes specified in Section 4.3 that the |
---|
| 1028 | implementation supports for Job objects. |
---|
| 1029 | - 'all': the special group 'all' that includes all |
---|
| 1030 | attributes that the implementation supports for Job |
---|
| 1031 | objects. |
---|
| 1032 | |
---|
| 1033 | Since a client MAY request specific attributes or named |
---|
| 1034 | groups, there is a potential that there is some overlap. For |
---|
| 1035 | example, if a client requests, 'job-name' and |
---|
| 1036 | 'job-description', the client is actually requesting the |
---|
| 1037 | 'job-name' attribute once by naming it explicitly, and once by |
---|
| 1038 | inclusion in the 'job-description' group. In such cases, the |
---|
| 1039 | Printer object NEED NOT return the attribute only once in the |
---|
| 1040 | response even if it is requested multiple times. The client |
---|
| 1041 | SHOULD NOT request the same attribute in multiple ways. |
---|
| 1042 | |
---|
| 1043 | It is NOT REQUIRED that a Job object support all attributes |
---|
| 1044 | belonging to a group (since some attributes are |
---|
| 1045 | OPTIONAL). However it is REQUIRED that each Job object support |
---|
| 1046 | all these group names. |
---|
| 1047 | |
---|
| 1048 | Parameters |
---|
| 1049 | ---------- |
---|
| 1050 | job_id (integer) |
---|
| 1051 | the id of the job to send the uri |
---|
| 1052 | requested_attributes (list) |
---|
| 1053 | the attributes to return |
---|
| 1054 | |
---|
| 1055 | """ |
---|
[5e70cc2] | 1056 | |
---|
| 1057 | self.assert_running() |
---|
[33ea505] | 1058 | if requested_attributes is None: |
---|
| 1059 | requested = self.job_attributes |
---|
| 1060 | else: |
---|
| 1061 | requested = [a for a in self.job_attributes \ |
---|
| 1062 | if a in requested_attributes] |
---|
| 1063 | |
---|
| 1064 | _attributes = [attr.replace("-", "_") for attr in requested] |
---|
| 1065 | attributes = [getattr(self, attr)(job_id) for attr in _attributes] |
---|
| 1066 | return attributes |
---|
| 1067 | |
---|
[57bc2dc] | 1068 | @sync |
---|
[9da7428] | 1069 | def set_job_attributes(self, job_id, attributes): |
---|
[5e70cc2] | 1070 | self.assert_running() |
---|
[9da7428] | 1071 | job = self.get_job(job_id) |
---|
| 1072 | for attr in attributes: |
---|
| 1073 | if attr in ("job-id", "job-k-octets", "job-state", "job-printer-uri"): |
---|
| 1074 | raise ipp.errors.ClientErrorAttributesNotSettable(attr) |
---|
| 1075 | elif attr == "job-name": |
---|
| 1076 | job.name = attributes[attr] |
---|
| 1077 | elif attr == "job-originating-user-name": |
---|
| 1078 | job.creator = attributes[attr] # XXX: do we want this? |
---|
[57bc2dc] | 1079 | |
---|
| 1080 | @sync |
---|
[c1cebbc] | 1081 | def restart_job(self, job_id, requesting_user_name=None): |
---|
[5e70cc2] | 1082 | self.assert_running() |
---|
[c1cebbc] | 1083 | job = self.get_job(job_id) |
---|
| 1084 | try: |
---|
| 1085 | job.restart() |
---|
| 1086 | except InvalidJobStateException: |
---|
| 1087 | # XXX |
---|
| 1088 | raise ipp.errors.ClientErrorNotPossible |
---|
[33ea505] | 1089 | |
---|
[57bc2dc] | 1090 | self.finished_jobs.remove(job_id) |
---|
| 1091 | self.pending_jobs.append(job_id) |
---|
[33ea505] | 1092 | |
---|
[57bc2dc] | 1093 | @sync |
---|
[c1cebbc] | 1094 | def promote_job(self, job_id, requesting_user_name=None): |
---|
[c500bc2] | 1095 | # According to RFC 3998, we need to put the job at the front |
---|
| 1096 | # of the queue (so that when the currently playing job |
---|
| 1097 | # completes, this one will go next |
---|
| 1098 | |
---|
[5e70cc2] | 1099 | self.assert_running() |
---|
[c500bc2] | 1100 | job = self.get_job(job_id) |
---|
| 1101 | job.priority = 1 # XXX we need to actually do something |
---|
| 1102 | # correct here |
---|