mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-01-14 08:49:44 +00:00
port uri.py and test_uri.py to Python3. Interim checkin
This commit is contained in:
parent
82409138b7
commit
b0b2d16383
@ -48,24 +48,24 @@ class Literal(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
self.failUnlessReallyEqual(u.get_verify_cap(), None)
|
||||
|
||||
def test_empty(self):
|
||||
data = "" # This data is some *very* small data!
|
||||
data = b"" # This data is some *very* small data!
|
||||
return self._help_test(data)
|
||||
|
||||
def test_pack(self):
|
||||
data = "This is some small data"
|
||||
data = b"This is some small data"
|
||||
return self._help_test(data)
|
||||
|
||||
def test_nonascii(self):
|
||||
data = "This contains \x00 and URI:LIT: and \n, oh my."
|
||||
data = b"This contains \x00 and URI:LIT: and \n, oh my."
|
||||
return self._help_test(data)
|
||||
|
||||
class Compare(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
def test_compare(self):
|
||||
lit1 = uri.LiteralFileURI("some data")
|
||||
lit1 = uri.LiteralFileURI(b"some data")
|
||||
fileURI = 'URI:CHK:f5ahxa25t4qkktywz6teyfvcx4:opuioq7tj2y6idzfp6cazehtmgs5fdcebcz3cygrxyydvcozrmeq:3:10:345834'
|
||||
chk1 = uri.CHKFileURI.init_from_string(fileURI)
|
||||
chk2 = uri.CHKFileURI.init_from_string(fileURI)
|
||||
unk = uri.UnknownURI("lafs://from_the_future")
|
||||
unk = uri.UnknownURI(b"lafs://from_the_future")
|
||||
self.failIfEqual(lit1, chk1)
|
||||
self.failUnlessReallyEqual(chk1, chk2)
|
||||
self.failIfEqual(chk1, "not actually a URI")
|
||||
@ -74,12 +74,12 @@ class Compare(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
self.failUnlessReallyEqual(len(s), 3) # since chk1==chk2
|
||||
|
||||
def test_is_uri(self):
|
||||
lit1 = uri.LiteralFileURI("some data").to_string()
|
||||
lit1 = uri.LiteralFileURI(b"some data").to_string()
|
||||
self.failUnless(uri.is_uri(lit1))
|
||||
self.failIf(uri.is_uri(None))
|
||||
|
||||
def test_is_literal_file_uri(self):
|
||||
lit1 = uri.LiteralFileURI("some data").to_string()
|
||||
lit1 = uri.LiteralFileURI(b"some data").to_string()
|
||||
self.failUnless(uri.is_literal_file_uri(lit1))
|
||||
self.failIf(uri.is_literal_file_uri(None))
|
||||
self.failIf(uri.is_literal_file_uri("foo"))
|
||||
@ -99,7 +99,7 @@ class CHKFile(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
def test_pack(self):
|
||||
key = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
|
||||
storage_index = hashutil.storage_index_hash(key)
|
||||
uri_extension_hash = hashutil.uri_extension_hash("stuff")
|
||||
uri_extension_hash = hashutil.uri_extension_hash(b"stuff")
|
||||
needed_shares = 25
|
||||
total_shares = 100
|
||||
size = 1234
|
||||
@ -153,8 +153,8 @@ class CHKFile(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
v2 = uri.from_string(v.to_string())
|
||||
self.failUnlessReallyEqual(v, v2)
|
||||
|
||||
v3 = uri.CHKFileVerifierURI(storage_index="\x00"*16,
|
||||
uri_extension_hash="\x00"*32,
|
||||
v3 = uri.CHKFileVerifierURI(storage_index=b"\x00"*16,
|
||||
uri_extension_hash=b"\x00"*32,
|
||||
needed_shares=3,
|
||||
total_shares=10,
|
||||
size=1234)
|
||||
@ -165,7 +165,7 @@ class CHKFile(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
def test_pack_badly(self):
|
||||
key = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
|
||||
storage_index = hashutil.storage_index_hash(key)
|
||||
uri_extension_hash = hashutil.uri_extension_hash("stuff")
|
||||
uri_extension_hash = hashutil.uri_extension_hash(b"stuff")
|
||||
needed_shares = 25
|
||||
total_shares = 100
|
||||
size = 1234
|
||||
@ -197,20 +197,20 @@ class Extension(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
data = {"stuff": "value",
|
||||
"size": 12,
|
||||
"needed_shares": 3,
|
||||
"big_hash": hashutil.tagged_hash("foo", "bar"),
|
||||
"big_hash": hashutil.tagged_hash(b"foo", b"bar"),
|
||||
}
|
||||
ext = uri.pack_extension(data)
|
||||
d = uri.unpack_extension(ext)
|
||||
self.failUnlessReallyEqual(d["stuff"], "value")
|
||||
self.failUnlessReallyEqual(d["size"], 12)
|
||||
self.failUnlessReallyEqual(d["big_hash"], hashutil.tagged_hash("foo", "bar"))
|
||||
self.failUnlessReallyEqual(d["big_hash"], hashutil.tagged_hash(b"foo", b"bar"))
|
||||
|
||||
readable = uri.unpack_extension_readable(ext)
|
||||
self.failUnlessReallyEqual(readable["needed_shares"], 3)
|
||||
self.failUnlessReallyEqual(readable["stuff"], "value")
|
||||
self.failUnlessReallyEqual(readable["size"], 12)
|
||||
self.failUnlessReallyEqual(readable["big_hash"],
|
||||
base32.b2a(hashutil.tagged_hash("foo", "bar")))
|
||||
base32.b2a(hashutil.tagged_hash(b"foo", b"bar")))
|
||||
self.failUnlessReallyEqual(readable["UEB_hash"],
|
||||
base32.b2a(hashutil.uri_extension_hash(ext)))
|
||||
|
||||
@ -544,8 +544,8 @@ class Dirnode(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
u1.get_verify_cap()._filenode_uri)
|
||||
|
||||
def test_immutable(self):
|
||||
readkey = "\x01" * 16
|
||||
uri_extension_hash = hashutil.uri_extension_hash("stuff")
|
||||
readkey = b"\x01" * 16
|
||||
uri_extension_hash = hashutil.uri_extension_hash(b"stuff")
|
||||
needed_shares = 3
|
||||
total_shares = 10
|
||||
size = 1234
|
||||
@ -605,7 +605,7 @@ class Dirnode(testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
self.failUnless(str(u2_verifier))
|
||||
|
||||
def test_literal(self):
|
||||
u0 = uri.LiteralFileURI("data")
|
||||
u0 = uri.LiteralFileURI(b"data")
|
||||
u1 = uri.LiteralDirectoryURI(u0)
|
||||
self.failUnless(str(u1))
|
||||
self.failUnlessReallyEqual(u1.to_string(), "URI:DIR2-LIT:mrqxiyi")
|
||||
|
@ -5,7 +5,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from future.utils import PY2
|
||||
if PY2:
|
||||
from builtins import filter, map, zip, ascii, chr, dict, hex, input, next, oct, open, pow, round, super, bytes, int, list, object, range, str, max, min # noqa: F401
|
||||
from future.builtins import filter, map, zip, ascii, chr, dict, hex, input, next, oct, open, pow, round, super, bytes, list, object, range, str, max, min # noqa: F401
|
||||
|
||||
import re
|
||||
|
||||
@ -32,10 +32,10 @@ class BadURIError(CapConstraintError):
|
||||
# - make variable and method names consistently use _uri for an URI string,
|
||||
# and _cap for a Cap object (decoded URI)
|
||||
|
||||
BASE32STR_128bits = '(%s{25}%s)' % (base32.BASE32CHAR.decode('utf8'), base32.BASE32CHAR_3bits.decode('utf8'))
|
||||
BASE32STR_256bits = '(%s{51}%s)' % (base32.BASE32CHAR.decode('utf8'), base32.BASE32CHAR_1bits.decode('utf8'))
|
||||
BASE32STR_128bits = b'(%s{25}%s)' % (base32.BASE32CHAR, base32.BASE32CHAR_3bits)
|
||||
BASE32STR_256bits = b'(%s{51}%s)' % (base32.BASE32CHAR, base32.BASE32CHAR_1bits)
|
||||
|
||||
NUMBER='([0-9]+)'
|
||||
NUMBER = b'([0-9]+)'
|
||||
|
||||
|
||||
class _BaseURI(object):
|
||||
@ -61,10 +61,11 @@ class _BaseURI(object):
|
||||
@implementer(IURI, IImmutableFileURI)
|
||||
class CHKFileURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:CHK:'
|
||||
STRING_RE=re.compile('^URI:CHK:'+BASE32STR_128bits+':'+
|
||||
BASE32STR_256bits+':'+NUMBER+':'+NUMBER+':'+NUMBER+
|
||||
'$')
|
||||
BASE_STRING=b'URI:CHK:'
|
||||
BASE_STRING_U='URI:CHK:'
|
||||
STRING_RE=re.compile(BASE_STRING+BASE32STR_128bits+b':'+
|
||||
BASE32STR_256bits+b':'+NUMBER+b':'+NUMBER+b':'+NUMBER+
|
||||
b'$')
|
||||
|
||||
def __init__(self, key, uri_extension_hash, needed_shares, total_shares,
|
||||
size):
|
||||
@ -79,11 +80,10 @@ class CHKFileURI(_BaseURI):
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
mo = cls.STRING_RE.search(uri.encode('utf8'))
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
return cls(base32.a2b(bytes(mo.group(1), 'utf8')),
|
||||
base32.a2b(bytes(mo.group(2), 'utf8')),
|
||||
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)),
|
||||
int(mo.group(3)), int(mo.group(4)), int(mo.group(5)))
|
||||
|
||||
def to_string(self):
|
||||
@ -92,8 +92,8 @@ class CHKFileURI(_BaseURI):
|
||||
assert isinstance(self.size, int)
|
||||
|
||||
return ('URI:CHK:%s:%s:%d:%d:%d' %
|
||||
(base32.b2a(self.key),
|
||||
base32.b2a(self.uri_extension_hash),
|
||||
(base32.b2a(self.key).decode('utf8'),
|
||||
base32.b2a(self.uri_extension_hash).decode('utf8'),
|
||||
self.needed_shares,
|
||||
self.total_shares,
|
||||
self.size))
|
||||
@ -121,9 +121,10 @@ class CHKFileURI(_BaseURI):
|
||||
@implementer(IVerifierURI)
|
||||
class CHKFileVerifierURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:CHK-Verifier:'
|
||||
STRING_RE=re.compile('^URI:CHK-Verifier:'+BASE32STR_128bits+':'+
|
||||
BASE32STR_256bits+':'+NUMBER+':'+NUMBER+':'+NUMBER)
|
||||
BASE_STRING=b'URI:CHK-Verifier:'
|
||||
BASE_STRING_U='URI:CHK-Verifier:'
|
||||
STRING_RE=re.compile(BASE_STRING+BASE32STR_128bits+b':'+
|
||||
BASE32STR_256bits+b':'+NUMBER+b':'+NUMBER+b':'+NUMBER)
|
||||
|
||||
def __init__(self, storage_index, uri_extension_hash,
|
||||
needed_shares, total_shares, size):
|
||||
@ -136,11 +137,10 @@ class CHKFileVerifierURI(_BaseURI):
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
mo = cls.STRING_RE.search(uri.encode('utf8'))
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
return cls(si_a2b(bytes(mo.group(1), 'utf8')),
|
||||
base32.a2b(bytes(mo.group(2), 'utf8')),
|
||||
return cls(si_a2b(mo.group(1)), base32.a2b(mo.group(2)),
|
||||
int(mo.group(3)), int(mo.group(4)), int(mo.group(5)))
|
||||
|
||||
def to_string(self):
|
||||
@ -149,8 +149,8 @@ class CHKFileVerifierURI(_BaseURI):
|
||||
assert isinstance(self.size, int)
|
||||
|
||||
return ('URI:CHK-Verifier:%s:%s:%d:%d:%d' %
|
||||
(si_b2a(self.storage_index),
|
||||
base32.b2a(self.uri_extension_hash),
|
||||
(si_b2a(self.storage_index).decode('utf8'),
|
||||
base32.b2a(self.uri_extension_hash).decode('utf8'),
|
||||
self.needed_shares,
|
||||
self.total_shares,
|
||||
self.size))
|
||||
@ -171,23 +171,24 @@ class CHKFileVerifierURI(_BaseURI):
|
||||
@implementer(IURI, IImmutableFileURI)
|
||||
class LiteralFileURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:LIT:'
|
||||
STRING_RE=re.compile('^URI:LIT:'+str(base32.BASE32STR_anybytes)+'$')
|
||||
BASE_STRING=b'URI:LIT:'
|
||||
BASE_STRING_U='URI:LIT:'
|
||||
STRING_RE=re.compile(b'^URI:LIT:'+base32.BASE32STR_anybytes+b'$')
|
||||
|
||||
def __init__(self, data=None):
|
||||
if data is not None:
|
||||
assert isinstance(data, str)
|
||||
assert isinstance(data, bytes)
|
||||
self.data = data
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
mo = cls.STRING_RE.search(uri.encode('utf8'))
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
return cls(base32.a2b(bytes(mo.group(1), 'utf8')))
|
||||
return cls(base32.a2b(mo.group(1)))
|
||||
|
||||
def to_string(self):
|
||||
return 'URI:LIT:%s' % base32.b2a(self.data)
|
||||
return 'URI:LIT:%s' % base32.b2a(self.data).decode('utf8')
|
||||
|
||||
def is_readonly(self):
|
||||
return True
|
||||
@ -212,9 +213,10 @@ class LiteralFileURI(_BaseURI):
|
||||
@implementer(IURI, IMutableFileURI)
|
||||
class WriteableSSKFileURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:SSK:'
|
||||
STRING_RE=re.compile('^'+BASE_STRING+BASE32STR_128bits+':'+
|
||||
BASE32STR_256bits+'$')
|
||||
BASE_STRING=b'URI:SSK:'
|
||||
BASE_STRING_U='URI:SSK:'
|
||||
STRING_RE=re.compile(b'^'+BASE_STRING+BASE32STR_128bits+b':'+
|
||||
BASE32STR_256bits+b'$')
|
||||
|
||||
def __init__(self, writekey, fingerprint):
|
||||
assert isinstance(writekey, bytes)
|
||||
@ -226,11 +228,10 @@ class WriteableSSKFileURI(_BaseURI):
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
mo = cls.STRING_RE.search(uri.encode('utf8'))
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
return cls(base32.a2b(mo.group(1).encode('utf8')),
|
||||
base32.a2b(mo.group(2).encode('utf8')))
|
||||
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
||||
|
||||
def to_string(self):
|
||||
assert isinstance(self.writekey, bytes)
|
||||
@ -242,10 +243,10 @@ class WriteableSSKFileURI(_BaseURI):
|
||||
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
||||
|
||||
def abbrev(self):
|
||||
return base32.b2a(self.writekey[:5])
|
||||
return base32.b2a(self.writekey[:5]).decode('utf8')
|
||||
|
||||
def abbrev_si(self):
|
||||
return base32.b2a(self.storage_index)[:5]
|
||||
return base32.b2a(self.storage_index)[:5].decode('utf8')
|
||||
|
||||
def is_readonly(self):
|
||||
return False
|
||||
@ -263,8 +264,9 @@ class WriteableSSKFileURI(_BaseURI):
|
||||
@implementer(IURI, IMutableFileURI)
|
||||
class ReadonlySSKFileURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:SSK-RO:'
|
||||
STRING_RE=re.compile('^URI:SSK-RO:'+BASE32STR_128bits+':'+BASE32STR_256bits+'$')
|
||||
BASE_STRING=b'URI:SSK-RO:'
|
||||
BASE_STRING_U='URI:SSK-RO:'
|
||||
STRING_RE=re.compile(b'^URI:SSK-RO:'+BASE32STR_128bits+b':'+BASE32STR_256bits+b'$')
|
||||
|
||||
def __init__(self, readkey, fingerprint):
|
||||
self.readkey = readkey
|
||||
@ -274,11 +276,10 @@ class ReadonlySSKFileURI(_BaseURI):
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
mo = cls.STRING_RE.search(uri.encode('utf8'))
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
return cls(base32.a2b(mo.group(1).encode('utf8')),
|
||||
base32.a2b(mo.group(2).encode('utf8')))
|
||||
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
||||
|
||||
def to_string(self):
|
||||
assert isinstance(self.readkey, bytes)
|
||||
@ -290,10 +291,10 @@ class ReadonlySSKFileURI(_BaseURI):
|
||||
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
||||
|
||||
def abbrev(self):
|
||||
return base32.b2a(self.readkey[:5])
|
||||
return base32.b2a(self.readkey[:5]).decode('utf8')
|
||||
|
||||
def abbrev_si(self):
|
||||
return base32.b2a(self.storage_index)[:5]
|
||||
return base32.b2a(self.storage_index)[:5].decode('utf8')
|
||||
|
||||
def is_readonly(self):
|
||||
return True
|
||||
@ -311,8 +312,9 @@ class ReadonlySSKFileURI(_BaseURI):
|
||||
@implementer(IVerifierURI)
|
||||
class SSKVerifierURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:SSK-Verifier:'
|
||||
STRING_RE=re.compile('^'+BASE_STRING+BASE32STR_128bits+':'+BASE32STR_256bits+'$')
|
||||
BASE_STRING=b'URI:SSK-Verifier:'
|
||||
BASE_STRING_U='URI:SSK-Verifier:'
|
||||
STRING_RE=re.compile(b'^'+BASE_STRING+BASE32STR_128bits+b':'+BASE32STR_256bits+b'$')
|
||||
|
||||
def __init__(self, storage_index, fingerprint):
|
||||
assert len(storage_index) == 16
|
||||
@ -324,15 +326,13 @@ class SSKVerifierURI(_BaseURI):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
return cls(si_a2b(bytes(mo.group(1), 'utf8')),
|
||||
base32.a2b(bytes(mo.group(2), 'utf8')))
|
||||
return cls(si_a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
||||
|
||||
def to_string(self):
|
||||
assert isinstance(self.storage_index, bytes)
|
||||
assert isinstance(self.fingerprint, bytes)
|
||||
XXX
|
||||
return 'URI:SSK-Verifier:%s:%s' % (si_b2a(self.storage_index),
|
||||
base32.b2a(self.fingerprint))
|
||||
return 'URI:SSK-Verifier:%s:%s' % (si_b2a(self.storage_index).decode('utf8'),
|
||||
base32.b2a(self.fingerprint).decode('utf8'))
|
||||
|
||||
def is_readonly(self):
|
||||
return True
|
||||
@ -350,8 +350,9 @@ class SSKVerifierURI(_BaseURI):
|
||||
@implementer(IURI, IMutableFileURI)
|
||||
class WriteableMDMFFileURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:MDMF:'
|
||||
STRING_RE=re.compile('^'+BASE_STRING+BASE32STR_128bits+':'+BASE32STR_256bits+'(:|$)')
|
||||
BASE_STRING=b'URI:MDMF:'
|
||||
BASE_STRING_U='URI:MDMF:'
|
||||
STRING_RE=re.compile(b'^'+BASE_STRING+BASE32STR_128bits+b':'+BASE32STR_256bits+b'(:|$)')
|
||||
|
||||
def __init__(self, writekey, fingerprint):
|
||||
self.writekey = writekey
|
||||
@ -362,27 +363,26 @@ class WriteableMDMFFileURI(_BaseURI):
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
mo = cls.STRING_RE.search(uri.encode('utf8'))
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
return cls(base32.a2b(bytes(mo.group(1), 'utf8')),
|
||||
base32.a2b(bytes(mo.group(2), 'utf8')))
|
||||
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
||||
|
||||
def to_string(self):
|
||||
assert isinstance(self.writekey, bytes)
|
||||
assert isinstance(self.fingerprint, bytes)
|
||||
ret = 'URI:MDMF:%s:%s' % (base32.b2a(self.writekey),
|
||||
base32.b2a(self.fingerprint))
|
||||
ret = 'URI:MDMF:%s:%s' % (base32.b2a(self.writekey).decode('utf8'),
|
||||
base32.b2a(self.fingerprint).decode('utf8'))
|
||||
return ret
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
||||
|
||||
def abbrev(self):
|
||||
return base32.b2a(self.writekey[:5])
|
||||
return base32.b2a(self.writekey[:5]).decode('utf8')
|
||||
|
||||
def abbrev_si(self):
|
||||
return base32.b2a(self.storage_index)[:5]
|
||||
return base32.b2a(self.storage_index)[:5].decode('utf8')
|
||||
|
||||
def is_readonly(self):
|
||||
return False
|
||||
@ -400,8 +400,9 @@ class WriteableMDMFFileURI(_BaseURI):
|
||||
@implementer(IURI, IMutableFileURI)
|
||||
class ReadonlyMDMFFileURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:MDMF-RO:'
|
||||
STRING_RE=re.compile('^' +BASE_STRING+BASE32STR_128bits+':'+BASE32STR_256bits+'(:|$)')
|
||||
BASE_STRING=b'URI:MDMF-RO:'
|
||||
BASE_STRING_U='URI:MDMF-RO:'
|
||||
STRING_RE=re.compile(b'^' +BASE_STRING+BASE32STR_128bits+b':'+BASE32STR_256bits+b'(:|$)')
|
||||
|
||||
def __init__(self, readkey, fingerprint):
|
||||
self.readkey = readkey
|
||||
@ -411,28 +412,27 @@ class ReadonlyMDMFFileURI(_BaseURI):
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
mo = cls.STRING_RE.search(uri.encode('utf8'))
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
|
||||
return cls(base32.a2b(bytes(mo.group(1), 'utf8')),
|
||||
base32.a2b(bytes(mo.group(2), 'utf8')))
|
||||
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
||||
|
||||
def to_string(self):
|
||||
assert isinstance(self.readkey, str)
|
||||
assert isinstance(self.fingerprint, str)
|
||||
ret = 'URI:MDMF-RO:%s:%s' % (base32.b2a(self.readkey),
|
||||
base32.b2a(self.fingerprint))
|
||||
assert isinstance(self.readkey, bytes)
|
||||
assert isinstance(self.fingerprint, bytes)
|
||||
ret = 'URI:MDMF-RO:%s:%s' % (base32.b2a(self.readkey).decode('utf8'),
|
||||
base32.b2a(self.fingerprint).decode('utf8'))
|
||||
return ret
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
||||
|
||||
def abbrev(self):
|
||||
return base32.b2a(self.readkey[:5])
|
||||
return base32.b2a(self.readkey[:5]).decode('utf8')
|
||||
|
||||
def abbrev_si(self):
|
||||
return base32.b2a(self.storage_index)[:5]
|
||||
return base32.b2a(self.storage_index)[:5].decode('utf8')
|
||||
|
||||
def is_readonly(self):
|
||||
return True
|
||||
@ -450,8 +450,9 @@ class ReadonlyMDMFFileURI(_BaseURI):
|
||||
@implementer(IVerifierURI)
|
||||
class MDMFVerifierURI(_BaseURI):
|
||||
|
||||
BASE_STRING='URI:MDMF-Verifier:'
|
||||
STRING_RE=re.compile('^'+BASE_STRING+BASE32STR_128bits+':'+BASE32STR_256bits+'(:|$)')
|
||||
BASE_STRING=b'URI:MDMF-Verifier:'
|
||||
BASE_STRING_U=b'URI:MDMF-Verifier:'
|
||||
STRING_RE=re.compile(b'^'+BASE_STRING+BASE32STR_128bits+b':'+BASE32STR_256bits+b'(:|$)')
|
||||
|
||||
def __init__(self, storage_index, fingerprint):
|
||||
assert len(storage_index) == 16
|
||||
@ -460,17 +461,16 @@ class MDMFVerifierURI(_BaseURI):
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.STRING_RE.search(uri)
|
||||
mo = cls.STRING_RE.search(uri.encode('utf8'))
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
return cls(si_a2b(bytes(mo.group(1), 'utf8')),
|
||||
base32.a2b(bytes(mo.group(2), 'utf8')))
|
||||
return cls(si_a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
||||
|
||||
def to_string(self):
|
||||
assert isinstance(self.storage_index, str)
|
||||
assert isinstance(self.fingerprint, str)
|
||||
ret = 'URI:MDMF-Verifier:%s:%s' % (si_b2a(self.storage_index),
|
||||
base32.b2a(self.fingerprint))
|
||||
assert isinstance(self.storage_index, bytes)
|
||||
assert isinstance(self.fingerprint, bytes)
|
||||
ret = 'URI:MDMF-Verifier:%s:%s' % (si_b2a(self.storage_index).decode('utf8'),
|
||||
base32.b2a(self.fingerprint).decode('utf8'))
|
||||
return ret
|
||||
|
||||
def is_readonly(self):
|
||||
@ -496,20 +496,21 @@ class _DirectoryBaseURI(_BaseURI):
|
||||
|
||||
@classmethod
|
||||
def init_from_string(cls, uri):
|
||||
mo = cls.BASE_STRING_RE.search(uri)
|
||||
bytes_uri = uri.encode('utf8')
|
||||
mo = cls.BASE_STRING_RE.search(bytes_uri)
|
||||
if not mo:
|
||||
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
||||
bits = uri[mo.end():]
|
||||
fn = cls.INNER_URI_CLASS.init_from_string(
|
||||
cls.INNER_URI_CLASS.BASE_STRING+bits)
|
||||
cls.INNER_URI_CLASS.BASE_STRING_U+bits)
|
||||
return cls(fn)
|
||||
|
||||
def to_string(self):
|
||||
fnuri = self._filenode_uri.to_string()
|
||||
mo = re.match(self.INNER_URI_CLASS.BASE_STRING, fnuri)
|
||||
mo = re.match(self.INNER_URI_CLASS.BASE_STRING, fnuri.encode('utf8'))
|
||||
assert mo, fnuri
|
||||
bits = fnuri[mo.end():]
|
||||
return self.BASE_STRING+bits
|
||||
return self.BASE_STRING_U+bits
|
||||
|
||||
def abbrev(self):
|
||||
return self._filenode_uri.to_string().split(':')[2][:5]
|
||||
@ -518,7 +519,7 @@ class _DirectoryBaseURI(_BaseURI):
|
||||
si = self._filenode_uri.get_storage_index()
|
||||
if si is None:
|
||||
return "<LIT>"
|
||||
return base32.b2a(si)[:5]
|
||||
return base32.b2a(si)[:5].decode('utf8')
|
||||
|
||||
def is_mutable(self):
|
||||
return True
|
||||
@ -536,8 +537,9 @@ class _DirectoryBaseURI(_BaseURI):
|
||||
@implementer(IDirectoryURI)
|
||||
class DirectoryURI(_DirectoryBaseURI):
|
||||
|
||||
BASE_STRING='URI:DIR2:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2:'
|
||||
BASE_STRING_U='URI:DIR2:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=WriteableSSKFileURI
|
||||
|
||||
def __init__(self, filenode_uri=None):
|
||||
@ -555,8 +557,9 @@ class DirectoryURI(_DirectoryBaseURI):
|
||||
@implementer(IReadonlyDirectoryURI)
|
||||
class ReadonlyDirectoryURI(_DirectoryBaseURI):
|
||||
|
||||
BASE_STRING='URI:DIR2-RO:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2-RO:'
|
||||
BASE_STRING_U='URI:DIR2-RO:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=ReadonlySSKFileURI
|
||||
|
||||
def __init__(self, filenode_uri=None):
|
||||
@ -589,8 +592,9 @@ class _ImmutableDirectoryBaseURI(_DirectoryBaseURI):
|
||||
|
||||
|
||||
class ImmutableDirectoryURI(_ImmutableDirectoryBaseURI):
|
||||
BASE_STRING='URI:DIR2-CHK:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2-CHK:'
|
||||
BASE_STRING_U='URI:DIR2-CHK:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=CHKFileURI
|
||||
|
||||
def get_verify_cap(self):
|
||||
@ -599,8 +603,9 @@ class ImmutableDirectoryURI(_ImmutableDirectoryBaseURI):
|
||||
|
||||
|
||||
class LiteralDirectoryURI(_ImmutableDirectoryBaseURI):
|
||||
BASE_STRING='URI:DIR2-LIT:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2-LIT:'
|
||||
BASE_STRING_U='URI:DIR2-LIT:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=LiteralFileURI
|
||||
|
||||
def get_verify_cap(self):
|
||||
@ -611,8 +616,9 @@ class LiteralDirectoryURI(_ImmutableDirectoryBaseURI):
|
||||
@implementer(IDirectoryURI)
|
||||
class MDMFDirectoryURI(_DirectoryBaseURI):
|
||||
|
||||
BASE_STRING='URI:DIR2-MDMF:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2-MDMF:'
|
||||
BASE_STRING_U='URI:DIR2-MDMF:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=WriteableMDMFFileURI
|
||||
|
||||
def __init__(self, filenode_uri=None):
|
||||
@ -633,8 +639,9 @@ class MDMFDirectoryURI(_DirectoryBaseURI):
|
||||
@implementer(IReadonlyDirectoryURI)
|
||||
class ReadonlyMDMFDirectoryURI(_DirectoryBaseURI):
|
||||
|
||||
BASE_STRING='URI:DIR2-MDMF-RO:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2-MDMF-RO:'
|
||||
BASE_STRING_U='URI:DIR2-MDMF-RO:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=ReadonlyMDMFFileURI
|
||||
|
||||
def __init__(self, filenode_uri=None):
|
||||
@ -671,8 +678,9 @@ def wrap_dirnode_cap(filecap):
|
||||
@implementer(IVerifierURI)
|
||||
class MDMFDirectoryURIVerifier(_DirectoryBaseURI):
|
||||
|
||||
BASE_STRING='URI:DIR2-MDMF-Verifier:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2-MDMF-Verifier:'
|
||||
BASE_STRING_U='URI:DIR2-MDMF-Verifier:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=MDMFVerifierURI
|
||||
|
||||
def __init__(self, filenode_uri=None):
|
||||
@ -696,8 +704,9 @@ class MDMFDirectoryURIVerifier(_DirectoryBaseURI):
|
||||
@implementer(IVerifierURI)
|
||||
class DirectoryURIVerifier(_DirectoryBaseURI):
|
||||
|
||||
BASE_STRING='URI:DIR2-Verifier:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2-Verifier:'
|
||||
BASE_STRING_U='URI:DIR2-Verifier:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=SSKVerifierURI
|
||||
|
||||
def __init__(self, filenode_uri=None):
|
||||
@ -720,8 +729,9 @@ class DirectoryURIVerifier(_DirectoryBaseURI):
|
||||
|
||||
@implementer(IVerifierURI)
|
||||
class ImmutableDirectoryURIVerifier(DirectoryURIVerifier):
|
||||
BASE_STRING='URI:DIR2-CHK-Verifier:'
|
||||
BASE_STRING_RE=re.compile('^'+BASE_STRING)
|
||||
BASE_STRING=b'URI:DIR2-CHK-Verifier:'
|
||||
BASE_STRING_U='URI:DIR2-CHK-Verifier:'
|
||||
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
||||
INNER_URI_CLASS=CHKFileVerifierURI
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user