def opgrp_hdr(req): """Return a OPERATION group with standard header fields set mostly from request""" opgrp = ipp.ippgroup(ipp.IPP_TAG_OPERATION) charset = ipp.ippvalue(ipp.IPP_TAG_CHARSET) opgrp.setvalue(set_copy_or_default(req, charset, 'attributes-charset')) lang = ipp.ippvalue(ipp.IPP_TAG_LANGUAGE) opgrp.setvalue( set_copy_or_default(req, lang, 'attributes-natural-language')) return opgrp
def get_printers(req): """Get the list of printers and requested attributes""" # Get the list of requested attributes from the request req_al = req.find_attribute('requested-attributes') try: # Just get the requested attributes strings if req_al: req_al = attrib_tools.expand_multi_attributes(req_al) # Now get the list of available printers ptr_list = Config_data.list_printers() # Set up response response = ipp.ipp(request=req) response.setrespcode(ipp.IPP_OK, req.id) response.pushvalue(opgrp_hdr(req)) for ptr in ptr_list: ptrgrp = ipp.ippgroup(ipp.IPP_TAG_PRINTER) ptratts = Config_data.get_config(ptr, req_al) for attitem in ptratts: name, typenum, attlist = attitem val = ipp.ippvalue(typenum) val.setnamevalues(name, attlist) ptrgrp.setvalue(val) response.pushvalue(ptrgrp) return response.generate() except ValueError: return ipp_status(req, 'IPP_INTERNAL_ERROR', 'Error in get_printers()')
def ipp_status(req, code, msg): """Deliver a status message back to sender""" lmsg = "Op code %s returning result %s - %s" % ( ipp.op_to_name[req.statuscode], code, msg) syslog.syslog(syslog.LOG_WARNING, lmsg) response = ipp.ipp(request=req) response.setrespcode(code, req.id) # If code is a string it gets converted opgrp = opgrp_hdr(req) msgv = ipp.ippvalue(ipp.IPP_TAG_TEXT) msgv.setnamevalues('status-message', msg) opgrp.setvalue(msgv) response.pushvalue(opgrp) return response.generate()
def created_job_ok(req, jobnum): """Return response that document was sent OK""" response = ipp.ipp(request=req) response.setrespcode(ipp.IPP_OK, req.id) opgrp = opgrp_hdr(req) msg = ipp.ippvalue(ipp.IPP_TAG_TEXT) msg.setnamevalues('status-message', 'successful-ok') opgrp.setvalue(msg) response.pushvalue(opgrp) jobgrp = ipp.ippgroup(ipp.IPP_TAG_JOB) idval = ipp.ippvalue(ipp.IPP_TAG_INTEGER) idval.setnamevalues('job-id', jobnum) jobgrp.setvalue(idval) iduri = "ipp://%s:631/jobs/%d" % (Config_data.serverip(), jobnum) idval = ipp.ippvalue(ipp.IPP_TAG_URI) idval.setnamevalues('job-uri', iduri) jobgrp.setvalue(idval) jobstat = ipp.ippvalue(ipp.IPP_TAG_ENUM) jobstat.setnamevalues('job-state', ipp.IPP_JOB_PROCESSING) jobgrp.setvalue(jobstat) response.pushvalue(jobgrp) return response.generate()
def get_ptr_attributes(req, pname, al): """Get given attributes for given printer""" # Set up response response = ipp.ipp(request=req) response.setrespcode(ipp.IPP_OK, req.id) response.pushvalue(opgrp_hdr(req)) # Printer group stuff ptrgrp = ipp.ippgroup(ipp.IPP_TAG_PRINTER) ptratts = Config_data.get_config(pname, al) # Add each attribute for attitem in ptratts: name, typenum, attlist = attitem val = ipp.ippvalue(typenum) val.setnamevalues(name, attlist) ptrgrp.setvalue(val) response.pushvalue(ptrgrp) return response.generate()