Changeset 33ea505 for server/lib
- Timestamp:
- Jan 11, 2012, 6:58:43 PM (12 years ago)
- Branches:
- no-cups
- Children:
- 09790fe
- Parents:
- 4126d3d
- git-author:
- Jessica B. Hamrick <jhamrick@…> (01/11/12 18:58:43)
- git-committer:
- Jessica B. Hamrick <jhamrick@…> (01/11/12 18:58:43)
- Location:
- server/lib/gutenbach/server
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
server/lib/gutenbach/server/__init__.py
rd21198f r33ea505 4 4 __all__.extend(errors.__all__) 5 5 6 from job import Job7 __all__.append(' Job')6 from job import GutenbachJob 7 __all__.append('GutenbachJob') 8 8 9 9 from printer import GutenbachPrinter -
server/lib/gutenbach/server/job.py
r34a4e5d r33ea505 1 from . import InvalidJobStateException, MissingDataException1 from .errors import InvalidJobStateException, MissingDataException 2 2 from .player import Player 3 3 from gutenbach.ipp import JobStates as States 4 import logging 4 5 import os 5 import gutenbach.ipp as ipp6 import logging7 6 8 7 # initialize logger 9 8 logger = logging.getLogger(__name__) 10 9 11 class Job(object):10 class GutenbachJob(object): 12 11 13 # for IPP 14 attributes = [ 15 "job-id", 16 "job-name", 17 "job-originating-user-name", 18 "job-k-octets", 19 "job-state", 20 "job-printer-uri" 21 ] 22 23 def __init__(self, job_id=-1, printer=None, creator=None, name=None, size=None): 12 def __init__(self, job_id=None, creator=None, name=None): 24 13 """Create an empty Gutenbach job. 25 14 26 15 """ 27 16 28 self.printer = printer29 17 self.player = None 30 18 … … 32 20 self.creator = creator 33 21 self.name = name 34 self.size = size35 22 self.state = States.HELD 36 23 self.priority = 1 24 self.document = None 37 25 38 self.document_name = None39 self.document_format = None40 self.document_natural_language = None41 self.compression = None42 43 26 def __repr__(self): 44 27 return str(self) … … 67 50 except TypeError: 68 51 self._id = -1 69 70 @property71 def uri(self):72 return self.uris[0]73 74 @property75 def uris(self):76 return ["ipp://localhost/jobs/%d" % self.id,77 "ipp://localhost:8000/jobs/%d" % self.id]78 52 79 53 @property … … 109 83 110 84 """ 111 return self._size112 @size.setter113 def size(self, val):114 85 try: 115 self._size = int(val) 116 except TypeError: 117 self._size = 0 86 size = os.path.getsize(self.document) 87 except: 88 size = 0 89 return size 118 90 119 91 @property … … 135 107 ###################################################################### 136 108 109 def spool(self, document, username=None): 110 if self.state != States.HELD: 111 raise InvalidJobStateException(self.state) 112 113 self.document = document.name 114 self.player = Player(document) 115 self.creator = username 116 self.state = States.PENDING 117 118 logger.debug("document for job %d is '%s'" % (self.id, self.document)) 119 120 137 121 def play(self): 138 """Non-blocking play function. 122 """Non-blocking play function. Sets the job state to 123 PROCESSING. 124 125 Raises 126 ------ 127 InvalidJobStateException 128 If the job is not ready to be played. 139 129 140 130 """ … … 142 132 # make sure the job is waiting to be played and that it's 143 133 # valid 144 if self.state != States.PENDING:134 if not self.is_ready: 145 135 raise InvalidJobStateException(self.state) 146 136 147 137 # and set the state to processing if we're good to go 148 138 logger.info("playing job %s" % str(self)) 139 140 def _completed(): 141 logger.info("completed job %s" % str(self)) 142 self.state = States.COMPLETE 143 self.player = None 144 149 145 self.state = States.PROCESSING 150 self.player.callback = self._completed146 self.player.callback = _completed 151 147 self.player.start() 152 148 153 149 def pause(self): 154 if self.is_playing: 155 self.player.mplayer_pause() 156 self.state = States.STOPPED 150 """Non-blocking pause function. Sets the job state to 151 STOPPED. 152 153 """ 154 155 if not self.is_playing: 156 raise InvalidJobStateException(self.state) 157 158 self.player.mplayer_pause() 159 self.state = States.STOPPED 157 160 158 161 def cancel(self): 162 def _canceled(): 163 logger.info("canceled job %s" % str(self)) 164 self.state = States.CANCELLED 165 self.player = None 166 159 167 if self.is_playing: 160 self.player.callback = self._canceled168 self.player.callback = _canceled 161 169 self.player.mplayer_stop() 170 elif self.is_finished: 171 raise InvalidJobStateException(self.state) 172 162 173 self.state = States.CANCELLED 163 174 164 175 def abort(self): 176 def _aborted(): 177 logger.info("aborted job %s" % str(self)) 178 self.state = States.ABORTED 179 self.player = None 180 165 181 if self.is_playing: 166 self.player.callback = self._aborted182 self.player.callback = _aborted 167 183 self.player.mplayer_stop() 168 self.state = states.ABORTED 169 170 def _completed(self): 171 logger.info("completed job %s" % str(self)) 172 self.state = States.COMPLETE 173 self.player = None 174 175 def _canceled(self): 176 logger.info("canceled job %s" % str(self)) 177 self.state = States.CANCELLED 178 self.player = None 179 180 def _aborted(self): 181 logger.info("aborted job %s" % str(self)) 184 elif self.is_finished: 185 raise InvalidJobStateException(self.state) 186 182 187 self.state = States.ABORTED 183 self.player = None184 185 ######################################################################186 ### IPP Attributes ###187 ######################################################################188 189 @property190 def job_id(self):191 return ipp.JobId(self.id)192 193 @property194 def job_name(self):195 return ipp.JobName(self.name)196 197 @property198 def job_originating_user_name(self):199 return ipp.JobOriginatingUserName(self.creator)200 201 @property202 def job_k_octets(self):203 return ipp.JobKOctets(self.size)204 205 @property206 def job_state(self):207 return ipp.JobState(self.state)208 209 @property210 def job_printer_uri(self):211 return ipp.JobPrinterUri(self.printer.uri)212 188 213 189 214 ######################################################################215 ### IPP Operations ###216 ######################################################################217 218 def cancel_job(self):219 pass220 221 def send_document(self, document, document_name=None,222 document_format=None, document_natural_language=None,223 requesting_user_name=None, compression=None,224 last_document=None):225 226 if self.state != States.HELD:227 raise InvalidJobStateException(self.state)228 229 self.player = Player(document)230 231 if self.size == 0:232 self.size = os.path.getsize(document.name)233 234 self.document_name = str(document_name)235 self.document_format = str(document_format)236 self.document_natural_language = str(document_natural_language)237 self.creator = str(requesting_user_name)238 self.compression = str(compression)239 self.state = States.PENDING240 241 logger.debug("document for job %d is '%s'" % (self.id, self.document_name))242 243 def send_uri(self):244 pass245 246 def get_job_attributes(self, requested_attributes=None):247 if requested_attributes is None:248 requested = self.attributes249 else:250 requested = [a for a in self.attributes if a in requested_attributes]251 252 _attributes = [attr.replace("-", "_") for attr in requested]253 attributes = [getattr(self, attr) for attr in _attributes]254 return attributes255 256 def set_job_attributes(self):257 pass258 259 def restart_job(self):260 pass261 262 def promote_job(self):263 pass -
server/lib/gutenbach/server/player.py
reee389a r33ea505 41 41 self.callback() 42 42 43 self.fh.close() 44 self.fh = None 45 43 46 def mplayer_play(self): 44 47 logger.info("playing file '%s'" % self.fh.name) -
server/lib/gutenbach/server/printer.py
r34a4e5d r33ea505 1 from . import InvalidJobException, InvalidPrinterStateException, InvalidJobStateException2 from . importJob1 from .errors import InvalidJobException, InvalidPrinterStateException, InvalidJobStateException 2 from .job import GutenbachJob 3 3 from gutenbach.ipp import PrinterStates as States 4 4 import gutenbach.ipp as ipp … … 17 17 18 18 # for IPP 19 attributes = [19 printer_attributes = [ 20 20 "printer-uri-supported", 21 21 "uri-authentication-supported", … … 41 41 ] 42 42 43 job_attributes = [ 44 "job-id", 45 "job-name", 46 "job-originating-user-name", 47 "job-k-octets", 48 "job-state", 49 "job-printer-uri" 50 ] 51 43 52 operations = [ 44 53 "print-job", 45 "complete-job", 46 "start-job", 47 "get-job", 54 "validate-job", 48 55 "get-jobs", 56 "print-uri", 57 "create-job", 58 "pause-printer", 59 "resume-printer", 60 "get-printer-attributes", 61 "set-printer-attributes", 62 "cancel-job", 63 "send-document", 64 "send-uri", 65 "get-job-attributes", 66 "set-job-attributes", 67 "restart-job", 68 "promote-job" 49 69 ] 50 70 … … 73 93 def __str__(self): 74 94 return "<Printer '%s'>" % self.name 75 76 ######################################################################77 ### Properties ###78 ######################################################################79 80 @property81 def uris(self):82 uris = ["ipp://localhost:8000/printers/" + self.name,83 "ipp://localhost/printers/" + self.name]84 return uris85 86 @property87 def uri(self):88 return self.uris[0]89 90 @property91 def state(self):92 with self.lock:93 if self.current_job is not None:94 val = States.PROCESSING95 elif len(self.pending_jobs) == 0:96 val = States.IDLE97 else:98 val = States.STOPPED99 return val100 101 @property102 def active_jobs(self):103 with self.lock:104 jobs = self.pending_jobs[:]105 if self.current_job is not None:106 jobs.insert(0, self.current_job.id)107 return jobs108 109 ######################################################################110 ### Methods ###111 ######################################################################112 95 113 96 def run(self): … … 124 107 sys.exit(1) 125 108 time.sleep(0.1) 109 110 ###################################################################### 111 ### Properties ### 112 ###################################################################### 113 114 @property 115 def uris(self): 116 uris = ["ipp://localhost:8000/printers/" + self.name, 117 "ipp://localhost/printers/" + self.name] 118 return uris 119 120 @property 121 def uri(self): 122 return self.uris[0] 123 124 @property 125 def state(self): 126 with self.lock: 127 if self.current_job is not None: 128 val = States.PROCESSING 129 elif len(self.pending_jobs) == 0: 130 val = States.IDLE 131 else: 132 val = States.STOPPED 133 return val 134 135 @property 136 def active_jobs(self): 137 with self.lock: 138 jobs = self.pending_jobs[:] 139 if self.current_job is not None: 140 jobs.insert(0, self.current_job.id) 141 return jobs 142 143 ###################################################################### 144 ### Methods ### 145 ###################################################################### 126 146 127 147 def start_job(self): … … 150 170 self.current_job = None 151 171 152 def stop(self):153 pass154 155 172 def get_job(self, job_id): 156 173 with self.lock: … … 249 266 return ipp.MultipleDocumentJobsSupported(False) 250 267 268 ###################################################################### 269 ### Job IPP Attributes ### 270 ###################################################################### 271 272 def job_id(self, job_id): 273 job = self.get_job(job_id) 274 return ipp.JobId(job.id) 275 276 def job_name(self, job_id): 277 job = self.get_job(job_id) 278 return ipp.JobName(job.name) 279 280 def job_originating_user_name(self, job_id): 281 job = self.get_job(job_id) 282 return ipp.JobOriginatingUserName(job.creator) 283 284 def job_k_octets(self, job_id): 285 job = self.get_job(job_id) 286 return ipp.JobKOctets(job.size) 287 288 def job_state(self, job_id): 289 job = self.get_job(job_id) 290 return ipp.JobState(job.state) 291 292 def job_printer_uri(self, job_id): 293 job = self.get_job(job_id) 294 return ipp.JobPrinterUri(self.uri) 251 295 252 296 ###################################################################### … … 260 304 pass 261 305 262 def get_jobs(self, requesting_user_name="", which_jobs=None): 306 def get_jobs(self, requesting_user_name=None, which_jobs=None, 307 requested_attributes=None): 308 263 309 # Filter by the which-jobs attribute 264 310 if which_jobs is None: … … 278 324 else: 279 325 user_jobs = [job for job in jobs if job.creator == requesting_user_name] 280 281 return user_jobs 326 327 # Get the attributes of each job 328 job_attrs = [self.get_job_attributes( 329 job.id, requested_attributes=requested_attributes) for job in user_jobs] 330 331 return job_attrs 282 332 283 333 def print_uri(self): … … 288 338 self._next_job_id += 1 289 339 290 job = Job(job_id, 291 self, 292 creator=requesting_user_name, 293 name=job_name, 294 size=job_k_octets) 340 job = GutenbachJob( 341 job_id, 342 creator=requesting_user_name, 343 name=job_name) 295 344 296 345 self.jobs[job_id] = job 297 346 self.pending_jobs.append(job_id) 298 347 299 return job 348 return job_id 300 349 301 350 def pause_printer(self): … … 307 356 def get_printer_attributes(self, requested_attributes=None): 308 357 if requested_attributes is None: 309 requested = self. attributes358 requested = self.printer_attributes 310 359 else: 311 requested = [a for a in self.attributes if a in requested_attributes] 360 requested = [a for a in self.printer_attributes \ 361 if a in requested_attributes] 312 362 313 363 _attributes = [attr.replace("-", "_") for attr in requested] … … 317 367 def set_printer_attributes(self): 318 368 pass 369 370 def cancel_job(self, job_id, requesting_user_name=None): 371 job = self.get_job(job_id) 372 try: 373 job.cancel() 374 except InvalidJobStateException: 375 # XXX 376 raise 377 378 def send_document(self, job_id, document, document_name=None, 379 document_format=None, document_natural_language=None, 380 requesting_user_name=None, compression=None, 381 last_document=None): 382 383 job = self.get_job(job_id) 384 job.spool(document, username=requesting_user_name) 385 386 def send_uri(self): 387 pass 388 389 def get_job_attributes(self, job_id, requested_attributes=None): 390 if requested_attributes is None: 391 requested = self.job_attributes 392 else: 393 requested = [a for a in self.job_attributes \ 394 if a in requested_attributes] 395 396 _attributes = [attr.replace("-", "_") for attr in requested] 397 attributes = [getattr(self, attr)(job_id) for attr in _attributes] 398 return attributes 399 400 def set_job_attributes(self): 401 pass 402 403 def restart_job(self): 404 pass 405 406 def promote_job(self): 407 pass -
server/lib/gutenbach/server/requests.py
r34a4e5d r33ea505 258 258 259 259 # get the job attributes and add them to the response 260 job s = self.printer.get_jobs(260 job_attrs = self.printer.get_jobs( 261 261 which_jobs=which_jobs, 262 requesting_user_name=user )263 for job in jobs:264 attrs = job.get_job_attributes(requested_attributes=attributes)262 requesting_user_name=user, 263 requested_attributes=attributes) 264 for attrs in job_attrs: 265 265 response.attribute_groups.append(ipp.AttributeGroup( 266 266 ipp.AttributeTags.JOB, attrs)) … … 354 354 355 355 # get attributes from the printer and add to response 356 job = self.printer.create_job(356 job_id = self.printer.create_job( 357 357 requesting_user_name=requesting_user_name, 358 358 job_name=job_name, 359 359 job_k_octets=job_k_octets) 360 attrs = self.printer.get_job_attributes(job_id) 360 361 response.attribute_groups.append(ipp.AttributeGroup( 361 ipp.AttributeTags.JOB, job.get_job_attributes()))362 ipp.AttributeTags.JOB, attrs)) 362 363 363 364 @handler_for(ipp.OperationCodes.PAUSE_PRINTER) … … 536 537 537 538 if 'requesting-user-name' in operation: 538 user_name = verify_attribute(539 requesting_user_name = verify_attribute( 539 540 operation['requesting-user-name'], ipp.RequestingUserName)[0] 540 541 541 542 try: 542 job = self.printer.get_job(job_id) 543 job.cancel() 543 self.printer.cancel_job(job_id, requesting_user_name=requesting_user_name) 544 544 except InvalidJobException: 545 545 raise ipp.errors.ClientErrorNotFound("bad job: %d" % job_id) … … 681 681 682 682 try: 683 job = self.printer.get_job(job_id)684 job.send_document(683 self.printer.send_document( 684 job_id, 685 685 request.data, 686 requesting_user_name=user_name,687 686 document_name=document_name, 688 compression=compression,689 687 document_format=document_format, 690 688 document_natural_language=document_natural_language, 689 requesting_user_name=user_name, 690 compression=compression, 691 691 last_document=last_document) 692 attrs = job.get_job_attributes()692 attrs = self.printer.get_job_attributes(job_id) 693 693 except InvalidJobException: 694 694 raise ipp.errors.ClientErrorNotFound("bad job: %d" % job_id) … … 796 796 # get the job attributes and add them to the response 797 797 try: 798 job = self.printer.get_job(job_id) 799 attrs = job.get_job_attributes(requested_attributes=requested_attributes) 798 attrs = self.printer.get_job_attributes( 799 job_id, 800 requested_attributes=requested_attributes) 800 801 except InvalidJobException: 801 802 raise ipp.errors.ClientErrorNotFound("bad job: %d" % job_id)
Note: See TracChangeset
for help on using the changeset viewer.