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
 
@@ -143,23 +143,25 @@ class CommandFilePrinter(CommandPrinter):
 

	
 

	
 
# 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.
 
        """
 
@@ -182,18 +184,28 @@ class CommandNetworkPrinter(CommandPrinter):
 
        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.
 
    """
0 comments (0 inline, 0 general)