mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-02-20 17:52:50 +00:00
Fix lints, remove some Python 2 junk.
This commit is contained in:
parent
f5d9947368
commit
63549c71ef
@ -3,30 +3,11 @@ Base32 encoding.
|
||||
|
||||
Ported to Python 3.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from future.utils import PY2
|
||||
if PY2:
|
||||
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
|
||||
|
||||
if PY2:
|
||||
def backwardscompat_bytes(b):
|
||||
"""
|
||||
Replace Future bytes with native Python 2 bytes, so % works
|
||||
consistently until other modules are ported.
|
||||
"""
|
||||
return getattr(b, "__native__", lambda: b)()
|
||||
import string
|
||||
maketrans = string.maketrans
|
||||
else:
|
||||
def backwardscompat_bytes(b):
|
||||
return b
|
||||
maketrans = bytes.maketrans
|
||||
from typing import Optional
|
||||
def backwardscompat_bytes(b):
|
||||
return b
|
||||
maketrans = bytes.maketrans
|
||||
|
||||
from typing import Optional
|
||||
import base64
|
||||
|
||||
from allmydata.util.assertutil import precondition
|
||||
@ -34,7 +15,7 @@ from allmydata.util.assertutil import precondition
|
||||
rfc3548_alphabet = b"abcdefghijklmnopqrstuvwxyz234567" # RFC3548 standard used by Gnutella, Content-Addressable Web, THEX, Bitzi, Web-Calculus...
|
||||
chars = rfc3548_alphabet
|
||||
|
||||
vals = backwardscompat_bytes(bytes(range(32)))
|
||||
vals = bytes(range(32))
|
||||
c2vtranstable = maketrans(chars, vals)
|
||||
v2ctranstable = maketrans(vals, chars)
|
||||
identitytranstable = maketrans(b'', b'')
|
||||
@ -61,16 +42,16 @@ def get_trailing_chars_without_lsbs(N):
|
||||
d = {}
|
||||
return b''.join(_get_trailing_chars_without_lsbs(N, d=d))
|
||||
|
||||
BASE32CHAR = backwardscompat_bytes(b'['+get_trailing_chars_without_lsbs(0)+b']')
|
||||
BASE32CHAR_4bits = backwardscompat_bytes(b'['+get_trailing_chars_without_lsbs(1)+b']')
|
||||
BASE32CHAR_3bits = backwardscompat_bytes(b'['+get_trailing_chars_without_lsbs(2)+b']')
|
||||
BASE32CHAR_2bits = backwardscompat_bytes(b'['+get_trailing_chars_without_lsbs(3)+b']')
|
||||
BASE32CHAR_1bits = backwardscompat_bytes(b'['+get_trailing_chars_without_lsbs(4)+b']')
|
||||
BASE32STR_1byte = backwardscompat_bytes(BASE32CHAR+BASE32CHAR_3bits)
|
||||
BASE32STR_2bytes = backwardscompat_bytes(BASE32CHAR+b'{3}'+BASE32CHAR_1bits)
|
||||
BASE32STR_3bytes = backwardscompat_bytes(BASE32CHAR+b'{4}'+BASE32CHAR_4bits)
|
||||
BASE32STR_4bytes = backwardscompat_bytes(BASE32CHAR+b'{6}'+BASE32CHAR_2bits)
|
||||
BASE32STR_anybytes = backwardscompat_bytes(bytes(b'((?:%s{8})*') % (BASE32CHAR,) + bytes(b"(?:|%s|%s|%s|%s))") % (BASE32STR_1byte, BASE32STR_2bytes, BASE32STR_3bytes, BASE32STR_4bytes))
|
||||
BASE32CHAR = b'['+get_trailing_chars_without_lsbs(0)+b']'
|
||||
BASE32CHAR_4bits = b'['+get_trailing_chars_without_lsbs(1)+b']'
|
||||
BASE32CHAR_3bits = b'['+get_trailing_chars_without_lsbs(2)+b']'
|
||||
BASE32CHAR_2bits = b'['+get_trailing_chars_without_lsbs(3)+b']'
|
||||
BASE32CHAR_1bits = b'['+get_trailing_chars_without_lsbs(4)+b']'
|
||||
BASE32STR_1byte = BASE32CHAR+BASE32CHAR_3bits
|
||||
BASE32STR_2bytes = BASE32CHAR+b'{3}'+BASE32CHAR_1bits
|
||||
BASE32STR_3bytes = BASE32CHAR+b'{4}'+BASE32CHAR_4bits
|
||||
BASE32STR_4bytes = BASE32CHAR+b'{6}'+BASE32CHAR_2bits
|
||||
BASE32STR_anybytes = bytes(b'((?:%s{8})*') % (BASE32CHAR,) + bytes(b"(?:|%s|%s|%s|%s))") % (BASE32STR_1byte, BASE32STR_2bytes, BASE32STR_3bytes, BASE32STR_4bytes)
|
||||
|
||||
def b2a(os): # type: (bytes) -> bytes
|
||||
"""
|
||||
@ -80,7 +61,7 @@ def b2a(os): # type: (bytes) -> bytes
|
||||
"""
|
||||
return base64.b32encode(os).rstrip(b"=").lower()
|
||||
|
||||
def b2a_or_none(os): # type: (Optional[bytes]) -> Optional[bytes]
|
||||
def b2a_or_none(os: Optional[bytes]) -> Optional[bytes]:
|
||||
if os is not None:
|
||||
return b2a(os)
|
||||
return None
|
||||
@ -100,8 +81,6 @@ NUM_OS_TO_NUM_QS=(0, 2, 4, 5, 7,)
|
||||
NUM_QS_TO_NUM_OS=(0, 1, 1, 2, 2, 3, 3, 4)
|
||||
NUM_QS_LEGIT=(1, 0, 1, 0, 1, 1, 0, 1,)
|
||||
NUM_QS_TO_NUM_BITS=tuple([_x*8 for _x in NUM_QS_TO_NUM_OS])
|
||||
if PY2:
|
||||
del _x
|
||||
|
||||
# A fast way to determine whether a given string *could* be base-32 encoded data, assuming that the
|
||||
# original data had 8K bits for a positive integer K.
|
||||
@ -135,8 +114,6 @@ def a2b(cs): # type: (bytes) -> bytes
|
||||
"""
|
||||
@param cs the base-32 encoded data (as bytes)
|
||||
"""
|
||||
# Workaround Future newbytes issues by converting to real bytes on Python 2:
|
||||
cs = backwardscompat_bytes(cs)
|
||||
precondition(could_be_base32_encoded(cs), "cs is required to be possibly base32 encoded data.", cs=cs)
|
||||
precondition(isinstance(cs, bytes), cs)
|
||||
|
||||
@ -144,9 +121,8 @@ def a2b(cs): # type: (bytes) -> bytes
|
||||
# Add padding back, to make Python's base64 module happy:
|
||||
while (len(cs) * 5) % 8 != 0:
|
||||
cs += b"="
|
||||
# Let newbytes come through and still work on Python 2, where the base64
|
||||
# module gets confused by them.
|
||||
return base64.b32decode(backwardscompat_bytes(cs))
|
||||
|
||||
return base64.b32decode(cs)
|
||||
|
||||
|
||||
__all__ = ["b2a", "a2b", "b2a_or_none", "BASE32CHAR_3bits", "BASE32CHAR_1bits", "BASE32CHAR", "BASE32STR_anybytes", "could_be_base32_encoded"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user