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
 
@@ -146,6 +146,7 @@ class CommandFilePrinter(CommandPrinter):
 
settings = OrderedDict()
 
settings['address'] = "IP Address"
 
settings['port'] = "Port"
 
settings['timeout'] = "Timeout"
 

	
 
class CommandNetworkPrinter(CommandPrinter):
 
    """
 
@@ -157,6 +158,7 @@ class CommandNetworkPrinter(CommandPrinter):
 
    required_settings = settings
 
    address = None
 
    port = None
 
    timeout = None
 

	
 
    def print_labels(self, labels, progress=None):
 
        """
 
@@ -185,12 +187,22 @@ class CommandNetworkPrinter(CommandPrinter):
 
        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):
0 comments (0 inline, 0 general)