Changeset - 4d9fa6c34559
[Not reviewed]
0 1 0
Lance Edgar (lance) - 12 years ago 2012-11-09 11:42:35
lance@edbob.org
add timeout support to CommandNetworkPrinter
1 file changed with 18 insertions and 6 deletions:
0 comments (0 inline, 0 general)
rattail/labels.py
Show inline comments
 
@@ -137,35 +137,37 @@ class CommandFilePrinter(CommandPrinter):
 
        labels_file.close()
 
        fn = '%s_%s.labels' % (socket.gethostname(),
 
                               edbob.local_time().strftime('%Y-%m-%d_%H-%M-%S'))
 
        final_path = os.path.join(output_dir, fn)
 
        shutil.move(labels_path, final_path)
 
        return final_path
 

	
 

	
 
# Force ordering for network printer required settings.
 
settings = OrderedDict()
 
settings['address'] = "IP Address"
 
settings['port'] = "Port"
 
settings['timeout'] = "Timeout"
 

	
 
class CommandNetworkPrinter(CommandPrinter):
 
    """
 
    Generic :class:`LabelPrinter` implementation which "prints" labels to a
 
    network socket in the form of native printer (textual) commands.  The
 
    socket is assumed to exist on a networked label printer.
 
    """
 

	
 
    required_settings = settings
 
    address = None
 
    port = None
 
    timeout = None
 

	
 
    def print_labels(self, labels, progress=None):
 
        """
 
        Prints ``labels`` by generating commands and sending directly to a
 
        socket which exists on a networked printer.
 
        """
 

	
 
        if not self.address:
 
            raise LabelPrintingError("Printer does not have an IP address defined")
 
        if not self.port:
 
            raise LabelPrintingError("Printer does not have a port defined.")
 

	
 
@@ -176,30 +178,40 @@ class CommandNetworkPrinter(CommandPrinter):
 
            data.write('%s\n' % '\n'.join(header))
 

	
 
        commands = self.formatter.format_labels(labels, progress=progress)
 
        if commands is None: # process canceled?
 
            data.close()
 
            return None
 
        data.write(commands)
 

	
 
        footer = self.batch_footer_commands()
 
        if footer:
 
            data.write('%s\n' % '\n'.join(footer))
 

	
 
        # Must pass byte-strings (not unicode) to this function.
 
        sock = socket.create_connection((str(self.address), str(self.port)))
 
        bytes = sock.send(data.getvalue())
 
        sock.close()
 
        data.close()
 
        return bytes
 
        try:
 
            timeout = int(self.timeout)
 
        except ValueError:
 
            timeout = socket.getdefaulttimeout()
 

	
 
        try:
 
            # Must pass byte-strings (not unicode) to this function.
 
            sock = socket.create_connection((str(self.address), str(self.port)), timeout)
 
        except socket.timeout:
 
            raise LabelPrintingError("Can't communicate with printer.")
 
        else:
 
            bytes = sock.send(data.getvalue())
 
            sock.close()
 
            return bytes
 
        finally:
 
            data.close()
 

	
 

	
 
class LabelFormatter(edbob.Object):
 
    """
 
    Base class for all label formatters.
 
    """
 

	
 
    format = None
 

	
 
    @requires_impl()
 
    def format_labels(self, labels, progress=None, *args, **kwargs):
 
        """
0 comments (0 inline, 0 general)