2016-03-13 00:50:57 +00:00
|
|
|
#!/usr/bin/env python
|
2014-08-16 00:20:02 +00:00
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-03-13 00:50:57 +00:00
|
|
|
import argparse
|
2016-03-16 23:21:05 +00:00
|
|
|
import sys
|
2016-03-14 21:03:46 +00:00
|
|
|
import six
|
2016-03-16 20:47:51 +00:00
|
|
|
from . import config
|
2016-03-13 00:50:57 +00:00
|
|
|
|
2016-03-16 16:26:57 +00:00
|
|
|
# Used in demo method
|
|
|
|
# Key: The name of escpos function and the argument passed on the CLI. Some
|
|
|
|
# manual translation is done in the case of barcodes_a -> barcode.
|
|
|
|
# Value: A list of dictionaries to pass to the escpos function as arguments.
|
2016-03-16 23:24:23 +00:00
|
|
|
DEMO_FUNCTIONS = {
|
2016-03-15 23:01:07 +00:00
|
|
|
'text': [
|
2016-03-28 16:53:45 +00:00
|
|
|
{'txt': 'Hello, World!\n',}
|
2016-03-15 23:01:07 +00:00
|
|
|
],
|
|
|
|
'qr': [
|
|
|
|
{'text': 'This tests a QR code'},
|
|
|
|
{'text': 'https://en.wikipedia.org/'}
|
|
|
|
],
|
|
|
|
'barcodes_a': [
|
2016-03-16 23:24:23 +00:00
|
|
|
{'bc': 'UPC-A', 'code': '13243546576'},
|
|
|
|
{'bc': 'UPC-E', 'code': '132435'},
|
|
|
|
{'bc': 'EAN13', 'code': '1324354657687'},
|
2016-03-18 18:42:56 +00:00
|
|
|
{'bc': 'EAN8', 'code': '1324354'},
|
2016-03-20 03:11:58 +00:00
|
|
|
{'bc': 'CODE39', 'code': 'TEST'},
|
2016-03-16 23:24:23 +00:00
|
|
|
{'bc': 'ITF', 'code': '55867492279103'},
|
|
|
|
{'bc': 'NW7', 'code': 'A00000000A'},
|
2016-03-15 23:01:07 +00:00
|
|
|
],
|
|
|
|
'barcodes_b': [
|
2016-03-16 23:24:23 +00:00
|
|
|
{'bc': 'UPC-A', 'code': '13243546576', 'function_type': 'B'},
|
|
|
|
{'bc': 'UPC-E', 'code': '132435', 'function_type': 'B'},
|
|
|
|
{'bc': 'EAN13', 'code': '1324354657687', 'function_type': 'B'},
|
2016-03-18 18:42:56 +00:00
|
|
|
{'bc': 'EAN8', 'code': '1324354', 'function_type': 'B'},
|
2016-03-20 03:11:58 +00:00
|
|
|
{'bc': 'CODE39', 'code': 'TEST', 'function_type': 'B'},
|
2016-03-16 23:24:23 +00:00
|
|
|
{'bc': 'ITF', 'code': '55867492279103', 'function_type': 'B'},
|
|
|
|
{'bc': 'NW7', 'code': 'A00000000A', 'function_type': 'B'},
|
|
|
|
{'bc': 'CODE93', 'code': 'A00000000A', 'function_type': 'B'},
|
|
|
|
{'bc': 'CODE93', 'code': '1324354657687', 'function_type': 'B'},
|
2016-03-20 03:11:58 +00:00
|
|
|
{'bc': 'CODE128A', 'code': 'TEST', 'function_type': 'B'},
|
|
|
|
{'bc': 'CODE128B', 'code': 'TEST', 'function_type': 'B'},
|
|
|
|
{'bc': 'CODE128C', 'code': 'TEST', 'function_type': 'B'},
|
2016-03-16 23:24:23 +00:00
|
|
|
{'bc': 'GS1-128', 'code': '00123456780000000001', 'function_type': 'B'},
|
|
|
|
{'bc': 'GS1 DataBar Omnidirectional', 'code': '0000000000000', 'function_type': 'B'},
|
|
|
|
{'bc': 'GS1 DataBar Truncated', 'code': '0000000000000', 'function_type': 'B'},
|
|
|
|
{'bc': 'GS1 DataBar Limited', 'code': '0000000000000', 'function_type': 'B'},
|
|
|
|
{'bc': 'GS1 DataBar Expanded', 'code': '00AAAAAAA', 'function_type': 'B'},
|
2016-03-28 18:16:37 +00:00
|
|
|
],
|
2016-03-15 23:01:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 18:16:37 +00:00
|
|
|
# Used to build the CLI
|
|
|
|
# A list of dictionaries. Each dict is a CLI argument.
|
|
|
|
# Keys:
|
|
|
|
# parser: A dict of args for command_parsers.add_parser
|
|
|
|
# defaults: A dict of args for subparser.set_defaults
|
|
|
|
# arguments: A list of dicts of args for subparser.add_argument
|
|
|
|
ESCPOS_COMMANDS = [
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'qr',
|
|
|
|
'help': 'Print a QR code',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'qr',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--text',),
|
|
|
|
'help': 'Text to print as a qr code',
|
|
|
|
'required': True,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'barcode',
|
|
|
|
'help': 'Print a barcode',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'barcode',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--code',),
|
|
|
|
'help': 'Barcode data to print',
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--bc',),
|
|
|
|
'help': 'Barcode format',
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--height',),
|
|
|
|
'help': 'Barcode height in px',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--width',),
|
|
|
|
'help': 'Barcode width',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--pos',),
|
|
|
|
'help': 'Label position',
|
|
|
|
'choices': ['BELOW', 'ABOVE', 'BOTH', 'OFF'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--font',),
|
|
|
|
'help': 'Label font',
|
|
|
|
'choices': ['A', 'B'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--align_ct',),
|
|
|
|
'help': 'Align barcode center',
|
|
|
|
'type': bool,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--function_type',),
|
|
|
|
'help': 'ESCPOS function type',
|
|
|
|
'choices': ['A', 'B'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'text',
|
|
|
|
'help': 'Print plain text',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'text',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--text',),
|
|
|
|
'help': 'Text to print as a qr code',
|
|
|
|
'required': True,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'block_text',
|
|
|
|
'help': 'Print wrapped text',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'block_text',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--txt',),
|
|
|
|
'help': 'block_text to print',
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--columns',),
|
|
|
|
'help': 'Number of columns',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'cut',
|
|
|
|
'help': 'Cut the paper',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'cut',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--mode',),
|
|
|
|
'help': 'Type of cut',
|
|
|
|
'choices': ['FULL', 'PART'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'cashdraw',
|
|
|
|
'help': 'Kick the cash drawer',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'cashdraw',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--pin',),
|
|
|
|
'help': 'Which PIN to kick',
|
|
|
|
'choices': [2, 5],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'image',
|
|
|
|
'help': 'Print an image',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'image',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--path_img',),
|
|
|
|
'help': 'Path to image',
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'fullimage',
|
|
|
|
'help': 'Print a fullimage',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'fullimage',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--img',),
|
|
|
|
'help': 'Path to img',
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--max_height',),
|
|
|
|
'help': 'Max height of image in px',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--width',),
|
|
|
|
'help': 'Max width of image in px',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--histeq',),
|
|
|
|
'help': 'Equalize the histrogram',
|
|
|
|
'type': bool,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--bandsize',),
|
|
|
|
'help': 'Size of bands to divide into when printing',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'charcode',
|
|
|
|
'help': 'Set character code table',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'charcode',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--code',),
|
|
|
|
'help': 'Character code',
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'set',
|
|
|
|
'help': 'Set text properties',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'set',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--align',),
|
|
|
|
'help': 'Horizontal alignment',
|
|
|
|
'choices': ['left', 'center', 'right'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--font',),
|
|
|
|
'help': 'Font choice',
|
|
|
|
'choices': ['left', 'center', 'right'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--text_type',),
|
|
|
|
'help': 'Text properties',
|
|
|
|
'choices': ['B', 'U', 'U2', 'BU', 'BU2', 'NORMAL'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--width',),
|
|
|
|
'help': 'Width multiplier',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--height',),
|
|
|
|
'help': 'Height multiplier',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--density',),
|
|
|
|
'help': 'Print density',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--invert',),
|
|
|
|
'help': 'White on black printing',
|
|
|
|
'type': bool,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--smooth',),
|
|
|
|
'help': 'Text smoothing. Effective on >: 4x4 text',
|
|
|
|
'type': bool,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--flip',),
|
|
|
|
'help': 'Text smoothing. Effective on >: 4x4 text',
|
|
|
|
'type': bool,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'hw',
|
|
|
|
'help': 'Hardware operations',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'hw',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--hw',),
|
|
|
|
'help': 'Operation',
|
|
|
|
'choices': ['INIT', 'SELECT', 'RESET'],
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'control',
|
|
|
|
'help': 'Control sequences',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'control',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--ctl',),
|
|
|
|
'help': 'Control sequence',
|
|
|
|
'choices': ['LF', 'FF', 'CR', 'HT', 'VT'],
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'option_strings': ('--pos',),
|
|
|
|
'help': 'Horizontal tab position (1-4)',
|
|
|
|
'type': int,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'panel_buttons',
|
|
|
|
'help': 'Controls panel buttons',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': 'panel_buttons',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--enable',),
|
|
|
|
'help': 'Feed button enabled',
|
|
|
|
'type': bool,
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'parser': {
|
|
|
|
'name': 'raw',
|
|
|
|
'help': 'Raw data',
|
|
|
|
},
|
|
|
|
'defaults': {
|
|
|
|
'func': '_raw',
|
|
|
|
},
|
|
|
|
'arguments': [
|
|
|
|
{
|
|
|
|
'option_strings': ('--msg',),
|
|
|
|
'help': 'Raw data to send',
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2016-03-15 23:01:07 +00:00
|
|
|
def main():
|
2016-03-16 16:26:57 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Handles loading of configuration and creating and processing of command
|
2016-03-16 23:24:23 +00:00
|
|
|
line arguments. Called when run from a CLI.
|
|
|
|
|
2016-03-16 16:26:57 +00:00
|
|
|
"""
|
2016-03-28 18:16:37 +00:00
|
|
|
|
|
|
|
# Load the configuration and defined printer
|
2016-03-16 23:24:23 +00:00
|
|
|
saved_config = config.Config()
|
|
|
|
printer = saved_config.printer()
|
2016-03-15 23:01:07 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='CLI for python-escpos',
|
2016-03-16 23:24:23 +00:00
|
|
|
epilog='Printer configuration is defined in the python-escpos config'
|
|
|
|
'file. See documentation for details.',
|
2016-03-15 23:01:07 +00:00
|
|
|
)
|
|
|
|
|
2016-03-28 18:16:37 +00:00
|
|
|
# Everything runs off of a subparser so we can use the format
|
|
|
|
#cli [subparser] -args
|
2016-03-15 23:01:07 +00:00
|
|
|
command_subparsers = parser.add_subparsers(
|
|
|
|
title='ESCPOS Command',
|
|
|
|
)
|
|
|
|
|
2016-03-28 18:16:37 +00:00
|
|
|
# Build the ESCPOS command arguments
|
|
|
|
for command in ESCPOS_COMMANDS:
|
|
|
|
parser_command = command_subparsers.add_parser(**command['parser'])
|
|
|
|
parser_command.set_defaults(**command['defaults'])
|
|
|
|
for argument in command['arguments']:
|
|
|
|
option_strings = argument.pop('option_strings')
|
|
|
|
parser_command.add_argument(*option_strings, **argument)
|
2016-03-20 03:03:16 +00:00
|
|
|
|
2016-03-28 18:16:37 +00:00
|
|
|
# Build any custom arguments
|
2016-03-16 23:24:23 +00:00
|
|
|
parser_command_demo = command_subparsers.add_parser('demo',
|
|
|
|
help='Demonstrates various functions')
|
2016-03-15 23:01:07 +00:00
|
|
|
parser_command_demo.set_defaults(func='demo')
|
|
|
|
demo_group = parser_command_demo.add_mutually_exclusive_group()
|
|
|
|
demo_group.add_argument(
|
|
|
|
'--barcodes-a',
|
|
|
|
help='Print demo barcodes for function type A',
|
|
|
|
action='store_true',
|
|
|
|
)
|
|
|
|
demo_group.add_argument(
|
|
|
|
'--barcodes-b',
|
|
|
|
help='Print demo barcodes for function type B',
|
|
|
|
action='store_true',
|
|
|
|
)
|
|
|
|
demo_group.add_argument(
|
|
|
|
'--qr',
|
|
|
|
help='Print some demo QR codes',
|
|
|
|
action='store_true',
|
|
|
|
)
|
|
|
|
demo_group.add_argument(
|
|
|
|
'--text',
|
|
|
|
help='Print some demo text',
|
|
|
|
action='store_true',
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get only arguments actually passed
|
|
|
|
args_dict = vars(parser.parse_args())
|
2016-03-16 23:21:05 +00:00
|
|
|
if not args_dict:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit()
|
2016-03-15 23:01:07 +00:00
|
|
|
command_arguments = dict([k, v] for k, v in six.iteritems(args_dict) if v)
|
|
|
|
|
2016-03-16 23:21:05 +00:00
|
|
|
if not printer:
|
|
|
|
raise Exception('No printers loaded from config')
|
|
|
|
|
|
|
|
|
2016-03-15 23:01:07 +00:00
|
|
|
target_command = command_arguments.pop('func')
|
|
|
|
|
|
|
|
if hasattr(printer, target_command):
|
|
|
|
# print command with args
|
|
|
|
getattr(printer, target_command)(**command_arguments)
|
|
|
|
else:
|
|
|
|
command_arguments['printer'] = printer
|
|
|
|
globals()[target_command](**command_arguments)
|
|
|
|
|
|
|
|
def demo(printer, **kwargs):
|
2016-03-16 16:26:57 +00:00
|
|
|
"""
|
2016-03-16 23:24:23 +00:00
|
|
|
Prints specificed demos. Called when CLI is passed `demo`. This function
|
|
|
|
uses the DEMO_FUNCTIONS dictionary.
|
2016-03-16 16:26:57 +00:00
|
|
|
|
|
|
|
:param printer: A printer from escpos.printer
|
|
|
|
:param kwargs: A dict with a key for each function you want to test. It's
|
|
|
|
in this format since it usually comes from argparse.
|
|
|
|
"""
|
2016-03-15 23:01:07 +00:00
|
|
|
for demo_choice in kwargs.keys():
|
|
|
|
command = getattr(
|
|
|
|
printer,
|
|
|
|
demo_choice
|
|
|
|
.replace('barcodes_a', 'barcode')
|
|
|
|
.replace('barcodes_b', 'barcode')
|
|
|
|
)
|
2016-03-16 23:24:23 +00:00
|
|
|
for params in DEMO_FUNCTIONS[demo_choice]:
|
2016-03-15 23:01:07 +00:00
|
|
|
command(**params)
|
2016-03-20 03:03:34 +00:00
|
|
|
printer.cut()
|
2016-03-15 23:01:07 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|