Debian Codegraph @

brother_ql/brother_ql_print.py

raw
1 #!/usr/bin/env python
2
3 """
4 Testing the packaged version of the Linux Kernel backend
5 """
6
7 import argparse, logging, sys
8
9 from brother_ql.backends import backend_factory, guess_backend, available_backends
10 from brother_ql.backends.helpers import discover, send
11 from brother_ql.output_helpers import log_discovered_devices, textual_description_discovered_devices
12
13 logger = logging.getLogger(__name__)
14
15 def main():
16
17 # Command line parsing...
18 parser = argparse.ArgumentParser()
19 parser.add_argument('--backend', choices=available_backends, help='Forces the use of a specific backend')
20 parser.add_argument('--list-printers', action='store_true', help='List the devices available with the selected --backend')
21 parser.add_argument('--debug', action='store_true', help='Enable debugging output')
22 parser.add_argument('instruction_file', nargs='?', help='file containing the instructions to be sent to the printer')
23 parser.add_argument('printer', metavar='PRINTER_IDENTIFIER', nargs='?', help='Identifier string specifying the printer. If not specified, selects the first detected device.')
24 args = parser.parse_args()
25
26 if args.list_printers and not args.backend:
27 parser.error('Please specify the backend in order to list available devices.')
28
29 if not args.list_printers and not args.instruction_file:
30 parser.error("the following arguments are required: instruction_file")
31
32 # Reading the instruction input file into content variable
33 if args.instruction_file == '-':
34 try:
35 content = sys.stdin.buffer.read()
36 except AttributeError:
37 content = sys.stdin.read()
38 else:
39 with open(args.instruction_file, 'rb') as f:
40 content = f.read()
41
42 # Setting up the requested level of logging.
43 level = logging.DEBUG if args.debug else logging.INFO
44 logging.basicConfig(level=level)
45
46 # State any shortcomings of this software early on.
47 if args.backend == 'network':
48 logger.warning("The network backend doesn't supply any 'readback' functionality. No status reports will be received.")
49
50 # Select the backend based: Either explicitly stated or derived from identifier. Otherwise: Default.
51 selected_backend = None
52 if args.backend:
53 selected_backend = args.backend
54 else:
55 try:
56 selected_backend = guess_backend(args.printer)
57 except:
58 logger.info("No backend stated. Selecting the default linux_kernel backend.")
59 selected_backend = 'linux_kernel'
60
61 # List any printers found, if explicitly asked to do so or if no identifier has been provided.
62 if args.list_printers or not args.printer:
63 available_devices = discover(backend_identifier=selected_backend)
64 log_discovered_devices(available_devices)
65
66 if args.list_printers:
67 print(textual_description_discovered_devices(available_devices))
68 sys.exit(0)
69
70 # Determine the identifier. Either selecting the explicitly stated one or using the first found device.
71 identifier = None
72 if not args.printer:
73 "We need to search for available devices and select the first."
74 if not available_devices:
75 sys.exit("No printer found")
76 identifier = available_devices[0]['identifier']
77 print("Selecting first device %s" % identifier)
78 else:
79 "A string identifier for the device was given, let's use it."
80 identifier = args.printer
81
82 # Finally, do the actual printing.
83 send(instructions=content, printer_identifier=identifier, backend_identifier=selected_backend, blocking=True)
84
85 if __name__ == "__main__": main()