def render_file(self, path, title, mime_type, force_single_page=False): all_pages = True page_range = '' page_set = 0 nup = 1 cups.resetOptions() if mime_type in ["application/x-cshell", "application/x-perl", "application/x-python", "application/x-shell", "application/x-sh", "text/plain",]: cups.addOption('prettyprint') if nup > 1: cups.addOption('number-up=%d' % nup) if force_single_page: cups.addOption('page-ranges=1') # Force coverpage to 1 page sent_job_id = cups.printFile(self.current_printer, path, title) cups.resetOptions() log.debug("Job ID=%d" % sent_job_id) job_id = 0 time.sleep(1) fax_file = '' complete = False end_time = time.time() + 300.0 # wait for 5 min. max while time.time() < end_time: log.debug("Waiting for fax...") result = list(self.service.CheckForWaitingFax(self.dev.device_uri, prop.username, sent_job_id)) fax_file = str(result[7]) log.debug("Fax file=%s" % fax_file) if fax_file: break if self.check_for_cancel(): log.error("Render canceled. Canceling job #%d..." % sent_job_id) cups.cancelJob(sent_job_id) return '', True time.sleep(1) else: log.error("Timeout waiting for rendering. Canceling job #%d..." % sent_job_id) cups.cancelJob(sent_job_id) return '', False return fax_file, False
def render_file(self, path, title, mime_type, force_single_page=False): all_pages = True page_range = '' page_set = 0 nup = 1 cups.resetOptions() if mime_type in ["application/x-cshell", "application/x-perl", "application/x-python", "application/x-shell", "application/x-sh", "text/plain",]: cups.addOption('prettyprint') if nup > 1: cups.addOption('number-up=%d' % nup) if force_single_page: cups.addOption('page-ranges=1') # Force coverpage to 1 page sent_job_id = cups.printFile(self.current_printer, path, title) cups.resetOptions() log.debug("Job ID=%d" % sent_job_id) job_id = 0 time.sleep(1) fax_file = '' complete = False end_time = time.time() + 300.0 # wait for 5 min. max while time.time() < end_time: log.debug("Waiting for fax... type =%s"%type(self.dev.device_uri)) result = list(self.service.CheckForWaitingFax(self.dev.device_uri, prop.username, sent_job_id)) fax_file = str(result[7]) log.debug("Fax file=%s" % fax_file) if fax_file: break if self.check_for_cancel(): log.error("Render canceled. Canceling job #%d..." % sent_job_id) cups.cancelJob(sent_job_id) return '', True time.sleep(1) else: log.error("Timeout waiting for rendering. Canceling job #%d..." % sent_job_id) cups.cancelJob(sent_job_id) return '', False return fax_file, False
def renderFile(self, f): self.busy = True beginWaitCursor() try: self.last_job_id = cups.printFile(self.printer_name, f, os.path.basename(f)) finally: self.busy = False endWaitCursor()
while True: cups_printers = cups.getPrinters() printer_state = cups.IPP_PRINTER_STATE_STOPPED for p in cups_printers: if p.name == printer_name: printer_state = p.state log.debug("Printer state = %d" % printer_state) if printer_state == cups.IPP_PRINTER_STATE_IDLE: log.debug("Printer name = %s file = %s" % (printer_name, path)) path = to_unicode(path, 'utf-8') sent_job_id = cups.printFile(printer_name, path, os.path.basename(path)) log.info("\nRendering file '%s' (job %d)..." % (path, sent_job_id)) log.debug("Job ID=%d" % sent_job_id) break elif printer_state == cups.IPP_PRINTER_STATE_PROCESSING: log.debug("Waiting for CUPS queue '%s' to become idle." % printer_name) else: log.error("The CUPS queue for '%s' is in a stopped or busy state (%d). Please check the queue and try again." % (printer_name, printer_state)) sys.exit(1) cups.resetOptions() # # Wait for fax to finish rendering #
def processFile(self, path, title=''): # Process an arbitrary file ("Add file...") path = os.path.realpath(path) if not title: title = os.path.basename(path) if os.path.exists(path) and os.access(path, os.R_OK): mime_type = magic.mime_type(path) mime_type_desc = mime_type if mime_type == 'application/hplip-fax': mime_type_desc = self.MIME_TYPES_DESC[mime_type][0] fax_file_fd = file(path, 'r') header = fax_file_fd.read(fax.FILE_HEADER_SIZE) mg, version, pages, hort_dpi, vert_dpi, page_size, \ resolution, encoding, reserved1, reserved2 = self.decode_fax_header(header) if mg != 'hplip_g3': log.error("Invalid file header. Bad magic.") self.form.WarningUI(self.__tr("<b>Invalid HPLIP Fax file.</b><p>Bad magic!")) return self.addFile(path, title, mime_type, mime_type_desc, pages) else: log.debug(repr(mime_type)) try: mime_type_desc = self.MIME_TYPES_DESC[mime_type][0] except KeyError: self.form.WarningUI(self.__tr("<b>You are trying to add a file that cannot be directly faxed with this utility.</b><p>To print this file, use the print command in the application that created it.")) return else: log.debug("Adding file: title='%s' file=%s mime_type=%s mime_desc=%s)" % (title, path, mime_type, mime_type_desc)) all_pages = True page_range = '' page_set = 0 #nup = 1 cups.resetOptions() self.cups_printers = cups.getPrinters() printer_state = cups.IPP_PRINTER_STATE_STOPPED for p in self.cups_printers: if p.name == self.cur_printer: printer_state = p.state log.debug("Printer state = %d" % printer_state) if printer_state == cups.IPP_PRINTER_STATE_IDLE: log.debug("Printing: %s on %s" % (path, self.cur_printer)) sent_job_id = cups.printFile(self.cur_printer, path, os.path.basename(path)) self.last_job_id = sent_job_id job_types[sent_job_id] = mime_type # save for later log.debug("Job ID=%d" % sent_job_id) QApplication.setOverrideCursor(QApplication.waitCursor) self.waitdlg = WaitForm(0, self.__tr("Processing fax file..."), None, self, modal=1) # self.add_fax_canceled self.waitdlg.show() else: self.form.FailureUI(self.__tr("<b>Printer '%1' is in a stopped or error state.</b><p>Check the printer queue in CUPS and try again.").arg(self.cur_printer)) cups.resetOptions() return cups.resetOptions() QApplication.restoreOverrideCursor() else: self.form.FailureUI(self.__tr("<b>Unable to add file '%1' to file list (file not found or insufficient permissions).</b><p>Check the file name and try again.").arg(path))