]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
refactor URI_extension handlers out of encode/download and into uri.py
authorBrian Warner <warner@allmydata.com>
Tue, 12 Jun 2007 01:25:18 +0000 (18:25 -0700)
committerBrian Warner <warner@allmydata.com>
Tue, 12 Jun 2007 01:25:18 +0000 (18:25 -0700)
src/allmydata/download.py
src/allmydata/encode.py
src/allmydata/uri.py

index 4a7cab688c08c944a27d62a2f6b2eb4de74374a4..f29caa3981337ed2db4c4b319426cd5946d297bb 100644 (file)
@@ -9,7 +9,7 @@ from allmydata.util import idlib, mathutil, hashutil
 from allmydata.util.assertutil import _assert
 from allmydata import codec, hashtree
 from allmydata.Crypto.Cipher import AES
-from allmydata.uri import unpack_uri
+from allmydata.uri import unpack_uri, unpack_extension
 from allmydata.interfaces import IDownloadTarget, IDownloader
 from allmydata.encode import NotEnoughPeersError
 
@@ -342,29 +342,7 @@ class FileDownloader:
         #               "vb is %s but should be a ValidatedBucket" % (vb,)
 
     def _unpack_uri_extension_data(self, data):
-        d = {}
-        while data:
-            colon = data.index(":")
-            key = data[:colon]
-            data = data[colon+1:]
-
-            colon = data.index(":")
-            number = data[:colon]
-            length = int(number)
-            data = data[colon+1:]
-
-            value = data[:length]
-            assert data[length] == ","
-            data = data[length+1:]
-
-            d[key] = value
-
-        # convert certain things to numbers
-        for intkey in ("size", "segment_size", "num_segments",
-                       "needed_shares", "total_shares"):
-            if intkey in d:
-                d[intkey] = int(d[intkey])
-        return d
+        return unpack_extension(data)
 
     def _obtain_uri_extension(self, ignored):
         # all shareholders are supposed to have a copy of uri_extension, and
index f062d14b254a51789c03021ea85dcfc9abe297e5..e0838f7323bae65351da86bfae044d28495024dc 100644 (file)
@@ -1,9 +1,9 @@
 # -*- test-case-name: allmydata.test.test_encode -*-
 
-import re
 from zope.interface import implements
 from twisted.internet import defer
 from twisted.python import log
+from allmydata import uri
 from allmydata.hashtree import HashTree
 from allmydata.Crypto.Cipher import AES
 from allmydata.util import mathutil, hashutil
@@ -433,15 +433,7 @@ class Encoder(object):
 
     def send_uri_extension_to_all_shareholders(self):
         log.msg("%s: sending uri_extension" % self)
-        pieces = []
-        for k in sorted(self.uri_extension_data.keys()):
-            value = self.uri_extension_data[k]
-            if isinstance(value, (int, long)):
-                value = "%d" % value
-            assert isinstance(value, str), k
-            assert re.match(r'^[a-zA-Z_\-]+$', k)
-            pieces.append(k + ":" + hashutil.netstring(value))
-        uri_extension = "".join(pieces)
+        uri_extension = uri.pack_extension(self.uri_extension_data)
         self.uri_extension_hash = hashutil.uri_extension_hash(uri_extension)
         dl = []
         for shareid in self.landlords.keys():
index 0739361331e4862e2cfcbb4aecead11b9b8e5059..a5c0f0003277b2bd24e246308872e0d5fc1be2bc 100644 (file)
@@ -1,5 +1,6 @@
 
-from allmydata.util import idlib
+import re
+from allmydata.util import idlib, hashutil
 
 # the URI shall be an ascii representation of the file. It shall contain
 # enough information to retrieve and validate the contents. It shall be
@@ -41,3 +42,40 @@ def unpack_uri(uri):
     return d
 
 
+def pack_extension(data):
+    pieces = []
+    for k in sorted(data.keys()):
+        value = data[k]
+        if isinstance(value, (int, long)):
+            value = "%d" % value
+        assert isinstance(value, str), k
+        assert re.match(r'^[a-zA-Z_\-]+$', k)
+        pieces.append(k + ":" + hashutil.netstring(value))
+    uri_extension = "".join(pieces)
+    return uri_extension
+
+def unpack_extension(data):
+    d = {}
+    while data:
+        colon = data.index(":")
+        key = data[:colon]
+        data = data[colon+1:]
+
+        colon = data.index(":")
+        number = data[:colon]
+        length = int(number)
+        data = data[colon+1:]
+
+        value = data[:length]
+        assert data[length] == ","
+        data = data[length+1:]
+
+        d[key] = value
+
+    # convert certain things to numbers
+    for intkey in ("size", "segment_size", "num_segments",
+                   "needed_shares", "total_shares"):
+        if intkey in d:
+            d[intkey] = int(d[intkey])
+    return d
+