Tweak some label printing logic to support python 3

This commit is contained in:
Lance Edgar 2019-01-21 16:23:24 -06:00
parent c6329cded6
commit 81a2dfa1f4

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar # Copyright © 2010-2019 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -30,7 +30,8 @@ import os
import os.path import os.path
import socket import socket
import shutil import shutil
from six import StringIO
import six
from rattail.core import Object from rattail.core import Object
from rattail.files import temp_path from rattail.files import temp_path
@ -174,7 +175,7 @@ class CommandNetworkPrinter(CommandPrinter):
if not self.port: if not self.port:
raise LabelPrintingError("Printer does not have a port defined.") raise LabelPrintingError("Printer does not have a port defined.")
data = StringIO() data = six.StringIO()
header = self.batch_header_commands() header = self.batch_header_commands()
if header: if header:
@ -240,7 +241,7 @@ class CommandFormatter(LabelFormatter):
""" """
def format_labels(self, labels, progress=None): def format_labels(self, labels, progress=None):
fmt = StringIO() fmt = six.StringIO()
def format_label(record, i): def format_label(record, i):
product, quantity, data = record product, quantity, data = record
@ -250,7 +251,10 @@ class CommandFormatter(LabelFormatter):
header = "{0}\n".format('\n'.join(header)) header = "{0}\n".format('\n'.join(header))
fmt.write(header.encode('utf_8')) fmt.write(header.encode('utf_8'))
body = "{}\n".format('\n'.join(self.label_body_commands(product, **data))) body = "{}\n".format('\n'.join(self.label_body_commands(product, **data)))
fmt.write(body.encode('utf_8')) if six.PY3:
fmt.write(body)
else: # PY2
fmt.write(body.encode('utf_8'))
footer = self.label_footer_commands() footer = self.label_footer_commands()
if footer: if footer:
footer = "{0}\n".format('\n'.join(footer)) footer = "{0}\n".format('\n'.join(footer))
@ -259,7 +263,10 @@ class CommandFormatter(LabelFormatter):
progress_loop(format_label, labels, progress, progress_loop(format_label, labels, progress,
message="Formatting labels") message="Formatting labels")
val = fmt.getvalue().decode('utf_8') if six.PY3:
val = fmt.getvalue()
else: # PY2
val = fmt.getvalue().decode('utf_8')
fmt.close() fmt.close()
return val return val
@ -304,7 +311,7 @@ class TwoUpCommandFormatter(CommandFormatter):
raise NotImplementedError raise NotImplementedError
def format_labels(self, labels, progress=None): def format_labels(self, labels, progress=None):
fmt = StringIO() fmt = six.StringIO()
self.half_started = False self.half_started = False
def format_label(record, i): def format_label(record, i):