Beispiel #1
0
    def getPrinterName(self, printer_name, device_uri, back_end_filter=device.DEFAULT_BE_FILTER,
                       filter=device.DEFAULT_FILTER):
        """ 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 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("Invalid device URI: %s" % device_uri)
                device_uri = 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 in printers:
                    printer_name_ok = True
                    device_uri_ret = device.getDeviceURIByPrinterName(printer_name)
                else:
                    log.error("Invalid printer name")
                    printer_name = 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]
            log.info("Using printer: %s\n" % printer_name_ret)

        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 printer_name_ret, device_uri_ret
Beispiel #2
0
    def getPrinterName(self,
                       printer_name,
                       device_uri,
                       back_end_filter=device.DEFAULT_BE_FILTER,
                       filter=device.DEFAULT_FILTER):
        """ 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 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("Invalid device URI: %s" % device_uri)
                device_uri = 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 in printers:
                    printer_name_ok = True
                    device_uri_ret = device.getDeviceURIByPrinterName(
                        printer_name)
                else:
                    log.error("Invalid printer name")
                    printer_name = 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]
            log.info("Using printer: %s\n" % printer_name_ret)

        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 printer_name_ret, device_uri_ret
Beispiel #3
0
    def getDeviceUri(self, device_uri=None, printer_name=None, back_end_filter=device.DEFAULT_BE_FILTER,
                     filter=device.DEFAULT_FILTER, devices=None, restrict_to_installed_devices=True):
        """ Validate passed in parameters, and, if in text mode, have user select desired device to use.
            Used for tools that are device-centric and accept -d (and maybe also -p).
            Use the filter(s) to restrict what constitute valid devices.

            Return the matching device URI based on:
            1. Passed in device_uri if it is valid (filter passes)
            2. Corresponding device_uri from the printer_name if it is valid (filter passes) ('*' means default printer)
            3. User input from menu (based on bus and filter)

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

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

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

        scan_uri_flag = False
        if 'hpaio' in back_end_filter:
            scan_uri_flag = True

        device_uri_ok = False
        printer_name_ok = False
        device_uri_ret = None

        if devices is None:
            devices = device.getSupportedCUPSDevices(back_end_filter, filter)
            log.debug(devices)

        if device_uri is not None:
            if device_uri in devices:
                device_uri_ok = True

            elif restrict_to_installed_devices:
                log.error("Invalid device URI: %s" % device_uri)
                device_uri = None

            else:
                device_uri_ok = True

        if printer_name is not None:
            #Find the printer_name in the models of devices
            log.debug(devices)
            for uri in devices:
               log.debug(uri)
               back_end, is_hp, bb, model, serial, dev_file, host, zc, port = \
                            device.parseDeviceURI(uri)
               log.debug("back_end=%s, is_hp=%s, bb=%s, model=%s, serial=%s, dev_file=%s, host=%s, zc=%s, port= %s" % (back_end, is_hp, bb, model, serial, dev_file, host, zc, port))
               if printer_name.lower() == model.lower():
                   printer_name_ok = True 
                   printer_name_device_uri = device_uri = uri
                   device_uri_ok = True
            if printer_name_ok is not True: 
               log.error("Invalid printer name: %s" % printer_name)
               printer_name = None

        if device_uri is not None and printer_name is None and device_uri_ok: # Only device_uri specified
            device_uri_ret = device_uri

        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 == printer_name_device_uri:
                    device_uri_ret = device_uri
                else:
                    log.error("Printer name %s and device URI %s refer to different devices." % (printer_name, device_uri))
                    printer_name, printer_name = None, None

        elif device_uri is None and printer_name is not None and printer_name_ok: # Only printer name specified
            device_uri_ret = device.getDeviceURIByPrinterName(printer_name, scan_uri_flag)

        elif len(devices) == 1: # Nothing specified, and only 1 device avail.
            device_uri_ret = devices.keys()[0]
            log.info("Using device: %s\n" % device_uri_ret)

        if device_uri_ret is None and self.mode == INTERACTIVE_MODE and len(devices):
            device_uri_ret = tui.device_table(devices, scan_uri_flag)

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

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

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

            Return the matching device URI based on:
            1. Passed in device_uri if it is valid (filter passes)
            2. Corresponding device_uri from the printer_name if it is valid (filter passes) ('*' means default printer)
            3. User input from menu (based on bus and filter)

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

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

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

        scan_uri_flag = False
        if 'hpaio' in back_end_filter:
            scan_uri_flag = True

        device_uri_ok = False
        printer_name_ok = False
        device_uri_ret = None

        if devices is None:
            devices = device.getSupportedCUPSDevices(back_end_filter, filter)
            log.debug(devices)

        if device_uri is not None:
            if device_uri in devices:
                device_uri_ok = True

            elif restrict_to_installed_devices:
                log.error("Invalid device URI: %s" % device_uri)
                device_uri = None

            else:
                device_uri_ok = True

        if printer_name is not None:
            #Find the printer_name in the models of devices
            log.debug(devices)
            for uri in devices:
                log.debug(uri)
                back_end, is_hp, bb, model, serial, dev_file, host, zc, port = \
                             device.parseDeviceURI(uri)
                log.debug(
                    "back_end=%s, is_hp=%s, bb=%s, model=%s, serial=%s, dev_file=%s, host=%s, zc=%s, port= %s"
                    % (back_end, is_hp, bb, model, serial, dev_file, host, zc,
                       port))
                if printer_name.lower() == model.lower():
                    printer_name_ok = True
                    printer_name_device_uri = device_uri = uri
                    device_uri_ok = True
            if printer_name_ok is not True:
                log.error("Invalid printer name: %s" % printer_name)
                printer_name = None

        if device_uri is not None and printer_name is None and device_uri_ok:  # Only device_uri specified
            device_uri_ret = device_uri

        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 == printer_name_device_uri:
                    device_uri_ret = device_uri
                else:
                    log.error(
                        "Printer name %s and device URI %s refer to different devices."
                        % (printer_name, device_uri))
                    printer_name, printer_name = None, None

        elif device_uri is None and printer_name is not None and printer_name_ok:  # Only printer name specified
            device_uri_ret = device.getDeviceURIByPrinterName(
                printer_name, scan_uri_flag)

        elif len(devices) == 1:  # Nothing specified, and only 1 device avail.
            device_uri_ret = devices.keys()[0]
            log.info("Using device: %s\n" % device_uri_ret)

        if device_uri_ret is None and self.mode == INTERACTIVE_MODE and len(
                devices):
            device_uri_ret = tui.device_table(devices, scan_uri_flag)

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

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

        return device_uri_ret