def addFileFromUI(self, f, title='', num_pages=0): f = os.path.abspath(os.path.expanduser(f)) log.debug("Trying to add file: %s" % f) if os.path.exists(f) and os.access(f, os.R_OK): mime_type = magic.mime_type(f) mime_type_desc = mime_type log.debug("File type of file %s: %s" % (f, mime_type)) try: mime_type_desc = MIME_TYPES_DESC[mime_type][0] except KeyError: if self.typ == FILETABLE_TYPE_PRINT: FailureUI( self, self.__tr( "<b>You are trying to add a file '%s' that cannot be directly printed with this utility.</b><p>To print this file, use the print command in the application that created it.<p>Note: Click <i>Show Valid Types...</i> to view a list of compatible file types that can be directly printed from this utility." % f), self.__tr("HP Device Manager")) else: FailureUI( self, self.__tr( "<b>You are trying to add a file '%s' that cannot be directly faxed with this utility.</b><p>To fax this file, use the print command in the application that created it (using the appropriate fax print queue).<p>Note: Click <i>Show Valid Types...</i> to view a list of compatible file types that can be directly added to the fax file list in this utility." % f), self.__tr("HP Device Manager")) else: if self.typ == FILETABLE_TYPE_PRINT: self.addFile(f, mime_type, mime_type_desc, title, num_pages) else: self.fax_add_callback(f) else: FailureUI( self, self.__tr( "<b>Unable to add file '%s' to file list (file not found or insufficient permissions).</b><p>Check the file name and try again." % f), self.__tr("HP Device Manager"))
def addFile(self, path): path = os.path.realpath(path) if os.path.exists(path) and os.access(path, os.R_OK): mime_type = magic.mime_type(path) mime_type_desc = mime_type log.debug(mime_type) try: mime_type_desc = self.MIME_TYPES_DESC[mime_type][0] except KeyError: self.form.FailureUI( self.__tr( "<b>You are trying to add a file that cannot be directly printed with this utility.</b><p>To print this file, use the print command in the application that created it." ) ) else: log.debug("Adding file %s (%s,%s)" % (path, mime_type, mime_type_desc)) self.file_list.append((path, mime_type, mime_type_desc)) 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) ) self.updateFileList()
def addFile(self, path): path = os.path.realpath(path) if os.path.exists(path) and os.access(path, os.R_OK): mime_type = magic.mime_type(path) mime_type_desc = mime_type log.debug(mime_type) try: mime_type_desc = self.MIME_TYPES_DESC[mime_type][0] except KeyError: self.form.FailureUI( self.__tr( "<b>You are trying to add a file that cannot be directly printed with this utility.</b><p>To print this file, use the print command in the application that created it." )) else: log.debug("Adding file %s (%s,%s)" % (path, mime_type, mime_type_desc)) self.file_list.append((path, mime_type, mime_type_desc)) 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)) self.updateFileList()
def addFileFromUI(self, f, title='', num_pages=0): f = os.path.abspath(os.path.expanduser(f)) log.debug("Trying to add file: %s" % f) if os.path.exists(f) and os.access(f, os.R_OK): mime_type = magic.mime_type(f) mime_type_desc = mime_type log.debug("File type of file %s: %s" % (f, mime_type)) try: mime_type_desc = MIME_TYPES_DESC[mime_type][0] except KeyError: if self.typ == FILETABLE_TYPE_PRINT: FailureUI(self, self.__tr("<b>You are trying to add a file '%s' that cannot be directly printed with this utility.</b><p>To print this file, use the print command in the application that created it.<p>Note: Click <i>Show Valid Types...</i> to view a list of compatible file types that can be directly printed from this utility."%f), self.__tr("HP Device Manager")) else: FailureUI(self, self.__tr("<b>You are trying to add a file '%s' that cannot be directly faxed with this utility.</b><p>To fax this file, use the print command in the application that created it (using the appropriate fax print queue).<p>Note: Click <i>Show Valid Types...</i> to view a list of compatible file types that can be directly added to the fax file list in this utility."%f), self.__tr("HP Device Manager")) else: if self.typ == FILETABLE_TYPE_PRINT: self.addFile(f, mime_type, mime_type_desc, title, num_pages) else: self.fax_add_callback(f) else: FailureUI(self, self.__tr("<b>Unable to add file '%s' to file list (file not found or insufficient permissions).</b><p>Check the file name and try again."%f), self.__tr("HP Device Manager"))
# stat = to_string_utf8(stat) # except OSError : # pass # except : # log.exception() # sys.exit(1) # if stat.strip('\n') == 'Enforcing' : # log.error('Unable to add file. Please disable SeLinux.\nEither disable it manually or run hp-doctor from terminal.') # sys.exit(0) for f in mod.args: path = os.path.realpath(f) log.debug(path) if os.path.exists(path): mime_type = magic.mime_type(path) log.debug(mime_type) else: log.error("File '%s' does not exist." % path) sys.exit(1) if mime_type not in allowable_mime_types: log.error("File '%s' has a non-allowed mime-type of '%s'" % (path, mime_type)) sys.exit(1) log.info(log.bold("Using fax %s (%s)" % (printer_name, device_uri))) #ok, lock_file = utils.lock_app('%s-%s' % (__mod__, printer_name), True) mod.lockInstance(printer_name) try:
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))