From 2a0bc50113053c2b63eccac91e626dfc3ac634db Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Fri, 22 Jul 2016 18:34:54 +0200 Subject: [PATCH] fine sanding of some brother_ql_* tools --- brother_ql/brother_ql_create.py | 2 +- brother_ql/brother_ql_print.py | 39 ++++++++++++++++++--------------- brother_ql/reader.py | 5 +++-- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/brother_ql/brother_ql_create.py b/brother_ql/brother_ql_create.py index 2446422..86952a8 100755 --- a/brother_ql/brother_ql_create.py +++ b/brother_ql/brother_ql_create.py @@ -42,7 +42,7 @@ def main(): args.model = args.model.upper() if args.list_models: - print('Supported models') + print('Supported models:') print('\n'.join(models)) sys.exit(0) diff --git a/brother_ql/brother_ql_print.py b/brother_ql/brother_ql_print.py index 894dc19..df38b05 100755 --- a/brother_ql/brother_ql_print.py +++ b/brother_ql/brother_ql_print.py @@ -15,7 +15,7 @@ logger = logging.getLogger(__name__) def main(): parser = argparse.ArgumentParser() - parser.add_argument('--backend', choices=available_backends, default='linux_kernel', help='Forces the use of a specific backend') + parser.add_argument('--backend', choices=available_backends, help='Forces the use of a specific backend') parser.add_argument('--list-printers', action='store_true', help='List the devices available with the selected --backend') parser.add_argument('--debug', action='store_true', help='Enable debugging output') parser.add_argument('instruction_file', nargs='?', help='file containing the instructions to be sent to the printer') @@ -31,13 +31,18 @@ def main(): level = logging.DEBUG if args.debug else logging.WARNING logging.basicConfig(level=level) if args.backend == 'network': - logger.warning("Warning: The network backend has not 'readback' functionality. Only writing/sending to the printer works here.") + logger.warning("The network backend doesn't supply any 'readback' functionality. No status reports will be received.") + + selected_backend = None + if args.backend: + selected_backend = args.backend + else: + try: + selected_backend = guess_backend(args.device) + except: + logger.info("No backend stated. Selecting the default linux_kernel backend.") + selected_backend = 'linux_kernel' - try: - selected_backend = guess_backend(args.device) - except: - logger.warning("Couln't guess the backend. Trying with linux_kernel backend.") - selected_backend = 'linux_kernel' be = backend_factory(selected_backend) list_available_devices = be['list_available_devices'] BrotherQLBackend = be['backend_class'] @@ -47,30 +52,28 @@ def main(): print(printer['string_descr']) sys.exit(0) + string_descr = None if not args.device: "We need to search for available devices and select the first." ad = list_available_devices() - selected = None - pprint(available_devices) - for device in ad: - string_descr = device['string_descr'] - instance = device['instance'] - selected = string_descr - break - if not selected: + if not ad: sys.exit("No printer found") + string_descr = ad[0]['string_descr'] + print("Selecting first device %s" % string_descr) else: "A string descriptor for the device was given, let's use it." - selected = args.device + string_descr = args.device - printer = BrotherQLBackend(selected) + printer = BrotherQLBackend(string_descr) start = time.time() with open(args.instruction_file, 'rb') as f: content = f.read() logger.info('Sending instructions to the printer. Total: %d bytes.', len(content)) printer.write(content) - if args.backend in ('network',): return + if selected_backend == 'network': + """ No need to wait for completion. The network backend doesn't support readback. """ + return printing_completed = False waiting_to_receive = False while time.time() - start < 10: diff --git a/brother_ql/reader.py b/brother_ql/reader.py index 5b66820..e65e3b8 100755 --- a/brother_ql/reader.py +++ b/brother_ql/reader.py @@ -130,6 +130,7 @@ def chunker(data, raise_exception=False): else: logger.warning(msg) data = data[1:] + continue opcode_def = OPCODES[opcode] num_bytes = len(opcode) if opcode_def[1] > 0: num_bytes += opcode_def[1] @@ -155,11 +156,11 @@ def interpret_response(data): error_info_2 = data[9] for error_bit in RESP_ERROR_INFORMATION_1_DEF: if error_info_1 & (1 << error_bit): - logging.error('Error: ' + RESP_ERROR_INFORMATION_1_DEF[error_bit]) + logger.error('Error: ' + RESP_ERROR_INFORMATION_1_DEF[error_bit]) errors.append(RESP_ERROR_INFORMATION_1_DEF[error_bit]) for error_bit in RESP_ERROR_INFORMATION_2_DEF: if error_info_2 & (1 << error_bit): - logging.error('Error: ' + RESP_ERROR_INFORMATION_2_DEF[error_bit]) + logger.error('Error: ' + RESP_ERROR_INFORMATION_2_DEF[error_bit]) errors.append(RESP_ERROR_INFORMATION_2_DEF[error_bit]) media_width = data[10]