From: Brian Warner Date: Tue, 12 Jun 2007 01:25:18 +0000 (-0700) Subject: refactor URI_extension handlers out of encode/download and into uri.py X-Git-Tag: allmydata-tahoe-0.4.0~53 X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/...?a=commitdiff_plain;h=382888899b947a3a10ae97c7776a897ae3ce6051;p=tahoe-lafs%2Ftahoe-lafs.git refactor URI_extension handlers out of encode/download and into uri.py --- diff --git a/src/allmydata/download.py b/src/allmydata/download.py index 4a7cab68..f29caa39 100644 --- a/src/allmydata/download.py +++ b/src/allmydata/download.py @@ -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 diff --git a/src/allmydata/encode.py b/src/allmydata/encode.py index f062d14b..e0838f73 100644 --- a/src/allmydata/encode.py +++ b/src/allmydata/encode.py @@ -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(): diff --git a/src/allmydata/uri.py b/src/allmydata/uri.py index 07393613..a5c0f000 100644 --- a/src/allmydata/uri.py +++ b/src/allmydata/uri.py @@ -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 +