예제 #1
0
    def getPrinterName(self, printer_name, device_uri, back_end_filter=device.DEFAULT_BE_FILTER,
                       filter=device.DEFAULT_FILTER, restrict_to_installed_devices=True):
        """ Validate passed in parameters, and, if in text mode, have user select desired printer to use.
            Used for tools that are printer queue-centric and accept -p (and maybe also -d).
            Use the filter(s) to restrict what constitute valid printers.

            Return the matching printer_name based on:
            1. Passed in printer_name if it is valid (filter passes) ('*' means default printer)
            2. From single printer_name of corresponding passed in device_uri (filter passes)
            3. User input from menu (CUPS printer list, filtered) [or if > 1 queue for device_uri]

            device_uri and printer_name can both be specified if they correspond to the same device.

            Returns:
                (printer_name|None, device_uri|None) (tuple)
                (returns None if passed in printer_name is invalid or device_uri doesn't correspond to printer_name)
        """

        log.debug("getPrinterName(%s, %s, %s, %s)" % (device_uri, printer_name, back_end_filter, filter))
        log.debug("Mode=%s" % self.mode)

        device_uri_ok = False
        printer_name_ok = False
        printer_name_ret = None
        device_uri_ret = None

        printers = device.getSupportedCUPSPrinterNames(back_end_filter, filter)
        log.debug(printers)

        if not printers:
            log.error("No device found that support this feature.")
            return False, None, None

        if device_uri is not None:
            devices = device.getSupportedCUPSDevices(back_end_filter, filter)
            if device_uri in devices:
                device_uri_ok = True
                device_uri_ret = device_uri
            else:
                log.error("'%s' device doesn't support this feature (or) Invalid device URI" % device_uri)
                device_uri = None
                if restrict_to_installed_devices:
                    return False, None, None

        if printer_name is not None:
            if printer_name == '*':
                from prnt import cups
                default_printer = cups.getDefaultPrinter()
                if default_printer is not None:
                    printer_name_ret = default_printer
                else:
                    log.error("CUPS default printer not set")
                    printer_name = None

            else:
                if printer_name.lower() in [p.lower() for p in printers]:
                    printer_name_ok = True
                    device_uri_ret = device.getDeviceURIByPrinterName(printer_name)
                else:
                    log.error("'%s' device doesn't support this feature (or) Invalid printer name" % printer_name)
                    printer_name = None
                    if restrict_to_installed_devices:
                        return False, None, None

        if device_uri is not None and printer_name is None and device_uri_ok: # Only device_uri specified
            if len(devices[device_uri]) == 1:
                printer_name_ret = devices[device_uri][0]

        elif device_uri is not None and printer_name is not None: # Both specified
            if device_uri_ok and printer_name_ok:
                if device_uri == device_uri_ret:
                    printer_name_ret = printer_name
                else:
                    log.error("Printer name and device URI refer to different devices.")

        elif device_uri is None and printer_name is not None and printer_name_ok: # Only printer name specified
            printer_name_ret = printer_name

        elif len(printers) == 1: # nothing specified, and only 1 avail. printer
            printer_name_ret = printers[0]

        if printer_name_ret is None and self.mode in (INTERACTIVE_MODE, NON_INTERACTIVE_MODE) and len(printers):
            printer_name_ret = tui.printer_table(printers)

        if printer_name_ret is not None and device_uri_ret is None:
            device_uri_ret = device.getDeviceURIByPrinterName(printer_name_ret)

        if device_uri_ret is not None:
            user_conf.set('last_used', 'device_uri', device_uri_ret)

        if printer_name_ret is not None:
            user_conf.set('last_used', 'printer_name', printer_name_ret)

        else:
            if self.mode in (INTERACTIVE_MODE, NON_INTERACTIVE_MODE):
                log.error("No printer selected/specified or that supports this functionality.")
                sys.exit(1)
            else:
                log.debug("No printer selected/specified")

        return True, printer_name_ret, device_uri_ret
예제 #2
0
    def getPrinterName(self,
                       printer_name,
                       device_uri,
                       back_end_filter=device.DEFAULT_BE_FILTER,
                       filter=device.DEFAULT_FILTER,
                       restrict_to_installed_devices=True):
        """ Validate passed in parameters, and, if in text mode, have user select desired printer to use.
            Used for tools that are printer queue-centric and accept -p (and maybe also -d).
            Use the filter(s) to restrict what constitute valid printers.

            Return the matching printer_name based on:
            1. Passed in printer_name if it is valid (filter passes) ('*' means default printer)
            2. From single printer_name of corresponding passed in device_uri (filter passes)
            3. User input from menu (CUPS printer list, filtered) [or if > 1 queue for device_uri]

            device_uri and printer_name can both be specified if they correspond to the same device.

            Returns:
                (printer_name|None, device_uri|None) (tuple)
                (returns None if passed in printer_name is invalid or device_uri doesn't correspond to printer_name)
        """

        log.debug("getPrinterName(%s, %s, %s, %s)" %
                  (device_uri, printer_name, back_end_filter, filter))
        log.debug("Mode=%s" % self.mode)

        device_uri_ok = False
        printer_name_ok = False
        printer_name_ret = None
        device_uri_ret = None

        printers = device.getSupportedCUPSPrinterNames(back_end_filter, filter)
        log.debug(printers)

        if not printers:
            log.error("No device found that support this feature.")
            return False, None, None

        if device_uri is not None:
            devices = device.getSupportedCUPSDevices(back_end_filter, filter)
            if device_uri in devices:
                device_uri_ok = True
                device_uri_ret = device_uri
            else:
                log.error(
                    "'%s' device doesn't support this feature (or) Invalid device URI"
                    % device_uri)
                device_uri = None
                if restrict_to_installed_devices:
                    return False, None, None

        if printer_name is not None:
            if printer_name == '*':
                from prnt import cups
                default_printer = cups.getDefaultPrinter()
                if default_printer is not None:
                    printer_name_ret = default_printer
                else:
                    log.error("CUPS default printer not set")
                    printer_name = None

            else:
                if printer_name.lower() in [p.lower() for p in printers]:
                    printer_name_ok = True
                    device_uri_ret = device.getDeviceURIByPrinterName(
                        printer_name)
                else:
                    log.error(
                        "'%s' device doesn't support this feature (or) Invalid printer name"
                        % printer_name)
                    printer_name = None
                    if restrict_to_installed_devices:
                        return False, None, None

        if device_uri is not None and printer_name is None and device_uri_ok:  # Only device_uri specified
            if len(devices[device_uri]) == 1:
                printer_name_ret = devices[device_uri][0]

        elif device_uri is not None and printer_name is not None:  # Both specified
            if device_uri_ok and printer_name_ok:
                if device_uri == device_uri_ret:
                    printer_name_ret = printer_name
                else:
                    log.error(
                        "Printer name and device URI refer to different devices."
                    )

        elif device_uri is None and printer_name is not None and printer_name_ok:  # Only printer name specified
            printer_name_ret = printer_name

        elif len(
                printers) == 1:  # nothing specified, and only 1 avail. printer
            printer_name_ret = printers[0]

        if printer_name_ret is None and self.mode in (
                INTERACTIVE_MODE, NON_INTERACTIVE_MODE) and len(printers):
            printer_name_ret = tui.printer_table(printers)

        if printer_name_ret is not None and device_uri_ret is None:
            device_uri_ret = device.getDeviceURIByPrinterName(printer_name_ret)

        if device_uri_ret is not None:
            user_conf.set('last_used', 'device_uri', device_uri_ret)

        if printer_name_ret is not None:
            user_conf.set('last_used', 'printer_name', printer_name_ret)

        else:
            if self.mode in (INTERACTIVE_MODE, NON_INTERACTIVE_MODE):
                log.error(
                    "No printer selected/specified or that supports this functionality."
                )
                sys.exit(1)
            else:
                log.debug("No printer selected/specified")

        return True, printer_name_ret, device_uri_ret