Factor out a function to convert a line to a pkt-line.
authorDave Borowitz <dborowitz@google.com>
Fri, 14 May 2010 01:22:07 +0000 (18:22 -0700)
committerDave Borowitz <dborowitz@google.com>
Mon, 2 Aug 2010 16:10:51 +0000 (09:10 -0700)
Change-Id: I11bd6c47a2434d371d7e15560fae94268cba260a

NEWS
dulwich/protocol.py

diff --git a/NEWS b/NEWS
index 350255bdbf30914c13afe6de53d7d6c969799f10..1f815ff04b4fd8f5f496216b1a7ee4576cab3c61 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -89,6 +89,8 @@
 
   * Factor out _report_status in ReceivePackHandler. (Dave Borowitz)
 
+  * Factor out a function to convert a line to a pkt-line. (Dave Borowitz)
+
 
 0.6.0  2010-05-22
 
index 8bd7842d854e97597761c04222998d879a84d0ee..5a5b6b82b7d16460f5bef0fb34dfe48cf85630c1 100644 (file)
@@ -57,6 +57,18 @@ class ProtocolFile(object):
         pass
 
 
+def pkt_line(data):
+    """Wrap data in a pkt-line.
+
+    :param data: The data to wrap, as a str or None.
+    :return: The data prefixed with its length in pkt-line format; if data was
+        None, returns the flush-pkt ('0000')
+    """
+    if data is None:
+        return '0000'
+    return '%04x%s' % (len(data) + 4, data)
+
+
 class Protocol(object):
 
     def __init__(self, read, write, report_activity=None):
@@ -98,14 +110,10 @@ class Protocol(object):
         :param line: A string containing the data to send
         """
         try:
-            if line is None:
-                self.write("0000")
-                if self.report_activity:
-                    self.report_activity(4, 'write')
-            else:
-                self.write("%04x%s" % (len(line)+4, line))
-                if self.report_activity:
-                    self.report_activity(4+len(line), 'write')
+            line = pkt_line(line)
+            self.write(line)
+            if self.report_activity:
+                self.report_activity(len(line), 'write')
         except socket.error, e:
             raise GitProtocolError(e)