diff --git a/brother_ql/printing.py b/brother_ql/backends/helpers.py similarity index 72% rename from brother_ql/printing.py rename to brother_ql/backends/helpers.py index 6159bc0..fdbac8e 100755 --- a/brother_ql/printing.py +++ b/brother_ql/backends/helpers.py @@ -1,7 +1,10 @@ #!/usr/bin/env python """ -The implementation of printing with the brother_ql package. +Helpers for the subpackage brother_ql.backends + +* device discovery +* printing """ import logging, time @@ -11,17 +14,28 @@ from brother_ql.reader import interpret_response logger = logging.getLogger(__name__) -def send(instructions, printer_identifier=None, backend_name=None, blocking=True): +def discover(backend_identifier='linux_kernel'): + + be = backend_factory(backend_identifier) + list_available_devices = be['list_available_devices'] + BrotherQLBackend = be['backend_class'] + + available_devices = list_available_devices() + return available_devices + +def send(instructions, printer_identifier=None, backend_identifier=None, blocking=True): """ - instructions: Bytes containing the instructions to be sent to the printer. - printer_identifier: String descriptor for the printer. - backend_name: Can enforce the use of a specific backend. - blocking: Boolean indicating whether the print() call should wait for completion. + Send instruction bytes to a printer. + + :param bytes instructions: The instructions to be sent to the printer. + :param str printer_identifier: Identifier for the printer. + :param str backend_identifier: Can enforce the use of a specific backend. + :param bool blocking: Indicates whether the function call should block while waiting for the completion of the printing. """ selected_backend = None - if backend_name: - selected_backend = backend_name + if backend_identifier: + selected_backend = backend_identifier else: try: selected_backend = guess_backend(printer_identifier) diff --git a/brother_ql/brother_ql_print.py b/brother_ql/brother_ql_print.py index 6f3bb13..cbec231 100755 --- a/brother_ql/brother_ql_print.py +++ b/brother_ql/brother_ql_print.py @@ -7,8 +7,8 @@ Testing the packaged version of the Linux Kernel backend import argparse, logging, sys from brother_ql.backends import backend_factory, guess_backend, available_backends -from brother_ql.printing import send -from brother_ql.discovering import discover, pretty_print_discovered_devices +from brother_ql.backends.helpers import discover, send +from brother_ql.output_helpers import log_discovered_devices, textual_description_discovered_devices logger = logging.getLogger(__name__) @@ -60,12 +60,11 @@ def main(): # List any printers found, if explicitly asked to do so or if no identifier has been provided. if args.list_printers or not args.printer: - available_devices = discover(backend=selected_backend) - pretty_print_discovered_devices(available_devices) + available_devices = discover(backend_identifier=selected_backend) + log_discovered_devices(available_devices) if args.list_printers: - for printer in available_devices: - print(printer['identifier']) + print(textual_description_discovered_devices(available_devices)) sys.exit(0) # Determine the identifier. Either selecting the explicitly stated one or using the first found device. @@ -81,6 +80,6 @@ def main(): identifier = args.printer # Finally, do the actual printing. - send(instructions=content, printer_identifier=identifier, backend_name=selected_backend, block=True) + send(instructions=content, printer_identifier=identifier, backend_identifier=selected_backend, blocking=True) if __name__ == "__main__": main() diff --git a/brother_ql/discovering.py b/brother_ql/discovering.py deleted file mode 100755 index 34835dd..0000000 --- a/brother_ql/discovering.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python - -""" -Printer device discovery using the different brother_ql.backends -""" - -import logging - -from brother_ql.backends import backend_factory, guess_backend, available_backends - -logger = logging.getLogger(__name__) - -def discover(backend_name='linux_kernel'): - - be = backend_factory(selected_backend) - list_available_devices = be['list_available_devices'] - BrotherQLBackend = be['backend_class'] - - available_devices = list_available_devices() - return available_devices - -def pretty_print_discovered_devices(available_devices): - for ad in available_devices: - result = {'model': 'unknown'} - result.update(ad) - logger.info(" Found a label printer: {identifier} (model: {model})".format(**result)) - - for printer in available_devices: - print(printer['identifier']) - sys.exit(0) diff --git a/brother_ql/output_helpers.py b/brother_ql/output_helpers.py new file mode 100644 index 0000000..2817b7a --- /dev/null +++ b/brother_ql/output_helpers.py @@ -0,0 +1,37 @@ +import logging + +from brother_ql.devicedependent import label_type_specs +from brother_ql.devicedependent import DIE_CUT_LABEL, ENDLESS_LABEL, ROUND_DIE_CUT_LABEL + +logger = logging.getLogger(__name__) + +def textual_label_description(labels_to_include): + output = "Supported label sizes:\n" + output = "" + fmt = " {label_size:9s} {dots_printable:14s} {label_descr:26s}\n" + output += fmt.format(label_size="Name", dots_printable="Printable px", label_descr="Description") + #output += fmt.format(label_size="", dots_printable="width x height", label_descr="") + for label_size in labels_to_include: + s = label_type_specs[label_size] + if s['kind'] in (DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL): + dp_fmt = "{0:4d} x {1:4d}" + elif s['kind'] == ENDLESS_LABEL: + dp_fmt = "{0:4d}" + else: + dp_fmt = " - unknown - " + dots_printable = dp_fmt.format(*s['dots_printable']) + label_descr = s['name'] + output += fmt.format(label_size=label_size, dots_printable=dots_printable, label_descr=label_descr) + return output + +def log_discovered_devices(available_devices, level=logging.INFO): + for ad in available_devices: + result = {'model': 'unknown'} + result.update(ad) + logger.log(level, " Found a label printer: {identifier} (model: {model})".format(**result)) + +def textual_description_discovered_devices(available_devices): + output = "" + for ad in available_devices: + output += ad['identifier'] + return output