mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-06-23 01:08:53 +00:00
move netstring() and split_netstring() into a separate util.netstring module
This commit is contained in:
@ -14,34 +14,10 @@ from allmydata.checker_results import DeepCheckResults, \
|
||||
from allmydata.util import hashutil, mathutil, base32, log
|
||||
from allmydata.util.hashutil import netstring
|
||||
from allmydata.util.limiter import ConcurrencyLimiter
|
||||
from allmydata.util.netstring import split_netstring
|
||||
from allmydata.uri import NewDirectoryURI
|
||||
from pycryptopp.cipher.aes import AES
|
||||
|
||||
def split_netstring(data, numstrings, allow_leftover=False):
|
||||
"""like string.split(), but extracts netstrings. If allow_leftover=False,
|
||||
returns numstrings elements, and throws ValueError if there was leftover
|
||||
data. If allow_leftover=True, returns numstrings+1 elements, in which the
|
||||
last element is the leftover data (possibly an empty string)"""
|
||||
elements = []
|
||||
assert numstrings >= 0
|
||||
while data:
|
||||
colon = data.index(":")
|
||||
length = int(data[:colon])
|
||||
string = data[colon+1:colon+1+length]
|
||||
assert len(string) == length
|
||||
elements.append(string)
|
||||
assert data[colon+1+length] == ","
|
||||
data = data[colon+1+length+1:]
|
||||
if len(elements) == numstrings:
|
||||
break
|
||||
if len(elements) < numstrings:
|
||||
raise ValueError("ran out of netstrings")
|
||||
if allow_leftover:
|
||||
return tuple(elements + [data])
|
||||
if data:
|
||||
raise ValueError("leftover data in netstrings")
|
||||
return tuple(elements)
|
||||
|
||||
class Deleter:
|
||||
def __init__(self, node, name, must_exist=True):
|
||||
self.node = node
|
||||
|
Reference in New Issue
Block a user