Skip to content

Commit edad799

Browse files
authored
Merge pull request #40 from alexander255/master
Deduplicate source code
2 parents 1759c1d + ba3913d commit edad799

18 files changed

+245
-305
lines changed

multiaddr/codec.py

Lines changed: 0 additions & 88 deletions
This file was deleted.

multiaddr/codecs/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- encoding: utf-8 -*-
2+
from __future__ import absolute_import
3+
import importlib
4+
5+
6+
# These are special sizes
7+
LENGTH_PREFIXED_VAR_SIZE = -1
8+
9+
10+
class NoneCodec:
11+
SIZE = 0
12+
IS_PATH = False
13+
14+
15+
CODEC_CACHE = {}
16+
def codec_by_name(name):
17+
if name is None: # Special “do nothing – expect nothing” pseudo-codec
18+
return NoneCodec
19+
codec = CODEC_CACHE.get(name)
20+
if not codec:
21+
codec = CODEC_CACHE[name] = importlib.import_module(".{0}".format(name), __name__)
22+
return codec

multiaddr/codecs/_util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if hasattr(int, 'from_bytes'):
2+
def packed_net_bytes_to_int(b):
3+
"""Convert the given big-endian byte-string to an int."""
4+
return int.from_bytes(b, byteorder='big')
5+
else: # PY2
6+
def packed_net_bytes_to_int(b):
7+
"""Convert the given big-endian byte-string to an int."""
8+
return int(b.encode('hex'), 16)

multiaddr/codecs/fspath.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
import os
33

44
import six
5-
import varint
65

7-
from ..protocols import read_varint_code
6+
from . import LENGTH_PREFIXED_VAR_SIZE
7+
8+
9+
SIZE = LENGTH_PREFIXED_VAR_SIZE
10+
IS_PATH = True
811

912

1013
if hasattr(os, "fsencode") and hasattr(os, "fsdecode"):
@@ -25,11 +28,8 @@ def fsdecode(path):
2528

2629

2730
def to_bytes(proto, string):
28-
bytes = fsencode(string)
29-
size = varint.encode(len(bytes))
30-
return b''.join([size, bytes])
31+
return fsencode(string)
3132

3233

3334
def to_string(proto, buf):
34-
size, num_bytes_read = read_varint_code(buf)
35-
return fsdecode(buf[num_bytes_read:])
35+
return fsdecode(buf)

multiaddr/codecs/idna.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
from __future__ import absolute_import
22

33
import idna
4-
import varint
54

6-
from ..protocols import read_varint_code
5+
from . import LENGTH_PREFIXED_VAR_SIZE
6+
7+
8+
SIZE = LENGTH_PREFIXED_VAR_SIZE
9+
IS_PATH = False
710

811

912
def to_bytes(proto, string):
10-
bytes = idna.encode(string, uts46=True)
11-
size = varint.encode(len(bytes))
12-
return b''.join([size, bytes])
13+
return idna.encode(string, uts46=True)
1314

1415

1516
def to_string(proto, buf):
16-
size, num_bytes_read = read_varint_code(buf)
17-
return idna.decode(buf[num_bytes_read:])
17+
return idna.decode(buf)

multiaddr/codecs/ip4.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import netaddr
44
import six
55

6-
from ..util import packed_net_bytes_to_int
6+
from ._util import packed_net_bytes_to_int
7+
8+
9+
SIZE = 32
10+
IS_PATH = False
711

812

913
def to_bytes(proto, string):

multiaddr/codecs/ip6.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import netaddr
44
import six
55

6-
from ..util import packed_net_bytes_to_int
6+
from ._util import packed_net_bytes_to_int
7+
8+
9+
SIZE = 128
10+
IS_PATH = False
711

812

913
def to_bytes(proto, string):

multiaddr/codecs/onion.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import six
66

77

8+
SIZE = 96
9+
IS_PATH = False
10+
11+
812
def to_bytes(proto, string):
913
addr = string.split(":")
1014
if len(addr) != 2:

multiaddr/codecs/p2p.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import base58
44
import six
5-
import varint
65

7-
from ..protocols import read_varint_code
6+
from . import LENGTH_PREFIXED_VAR_SIZE
7+
8+
9+
SIZE = LENGTH_PREFIXED_VAR_SIZE
10+
IS_PATH = False
811

912

1013
def to_bytes(proto, string):
@@ -15,15 +18,10 @@ def to_bytes(proto, string):
1518
mm = base58.b58decode(string)
1619
except Exception as ex:
1720
raise ValueError("failed to parse p2p addr: %s %s" % (string, str(ex)))
18-
size = varint.encode(len(mm))
1921
if len(mm) < 5:
2022
raise ValueError("invalid P2P multihash: %s" % mm)
21-
return b''.join([size, mm])
23+
return mm
2224

2325

2426
def to_string(proto, buf):
25-
size, num_bytes_read = read_varint_code(buf)
26-
buf = buf[num_bytes_read:]
27-
if len(buf) != size:
28-
raise ValueError("inconsistent lengths")
2927
return base58.b58encode(buf).decode('ascii')

multiaddr/codecs/uint16be.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import six
55

66

7+
SIZE = 16
8+
IS_PATH = False
9+
10+
711
def to_bytes(proto, string):
812
try:
913
return struct.pack('>H', int(string, 10))

0 commit comments

Comments
 (0)