diff --git a/rattail/labels.py b/rattail/labels.py index 4fc312a98d0a869a99b773cb65ce722389369b37..cf083b676c72eeff2ca1c8dbfb8ccaa8f05cb693 100644 --- a/rattail/labels.py +++ b/rattail/labels.py @@ -142,7 +142,7 @@ class LabelFormatter(edbob.Object): format = None @requires_impl() - def format_labels(self, labels, *args, **kwargs): + def format_labels(self, labels, progress=None, *args, **kwargs): """ Formats ``labels`` and returns the result. """ @@ -231,12 +231,17 @@ class TwoUpCommandFormatter(CommandFormatter): pass - def format_labels(self, labels): + def format_labels(self, labels, progress=None): + prog = None + if progress: + prog = progress("Formatting labels", len(labels)) + fmt = StringIO() + cancel = False half_started = False - for product, quantity in labels: - for i in range(quantity): + for i, (product, quantity) in enumerate(labels, 1): + for j in range(quantity): if half_started: fmt.write('%s\n' % '\n'.join( self.label_body_commands(product, x=self.half_offset))) @@ -251,6 +256,16 @@ class TwoUpCommandFormatter(CommandFormatter): fmt.write('%s\n' % '\n'.join( self.label_body_commands(product, x=0))) half_started = True + if prog and not prog.update(i): + cancel = True + break + + if prog: + prog.destroy() + + if cancel: + fmt.close() + return None if half_started: footer = self.label_footer_commands()