Commits

Matt Joiner committed 3e419fb

Rename bencoding->lib.bencode. Rename functions too.

Comments (0)

Files changed (2)

bencoding.py

-#!/usr/bin/env python3
-
-import collections
-import io
-import itertools
-import os
-import pdb
-import pprint
-import sys
-
-def _bencode(f, g):
-    for h in g:
-        if isinstance(h, dict):
-            f.write(b'd')
-            _bencode(f, itertools.chain.from_iterable(h.items()))
-            f.write(b'e')
-        elif type(h) is str:
-            s = h.encode()
-            s = str(len(s)).encode() + b':' + s
-            f.write(s)
-        elif type(h) is int:
-            f.write(b'i' + str(h).encode() + b'e')
-        elif isinstance(h, list):
-            f.write(b'l')
-            _bencode(f, h)
-            f.write(b'e')
-        elif isinstance(h, bytes):
-            f.write(str(len(h)).encode() + b':' + h)
-        else:
-            assert False, type(h)
-
-def bencode(obj):
-    f = io.BytesIO()
-    _bencode(f, [obj])
-    return f.getvalue()
-
-def buncode(f):
-    while True:
-        c = f.read(1).decode()
-        if not c:
-            return
-        if c == 'd':
-            a = iter(buncode(f))
-            yield collections.OrderedDict(zip(map(bytes.decode, a), a))
-        elif c == 'i':
-            i = ''
-            while True:
-                c = f.read(1).decode()
-                if not c:
-                    return
-                if c == 'e':
-                    yield int(i)
-                    break
-                else:
-                    i += c
-        elif c == 'l':
-            yield list(buncode(f))
-        elif c == 'e':
-            return
-        else:
-            i = c
-            while True:
-                c = f.read(1).decode()
-                assert c, ('Unterminated key:', i)
-                if c == ':':
-                    yield f.read(int(i))
-                    break
-                else:
-                    i += c
-
-def main():
-    import argparse
-
-    parser = argparse.ArgumentParser()
-    parser.add_argument('file', nargs='?')
-    parser.add_argument('-d', default=False, action='store_true')
-    args = parser.parse_args()
-    from functools import partial
-    if args.file:
-        f = partial(open, args.file)
-    else:
-        f = partial(os.fdopen, sys.stdin.fileno())
-    if args.d:
-        pprint.pprint(buncode(f('rb')).__next__())
-    else:
-        sys.stdout.buffer.write(bencode(eval(f('r').read())))
-
-if __name__ == '__main__':
-    main()
+#!/usr/bin/env python3
+
+import collections
+import io
+import itertools
+import os
+import pdb
+import pprint
+import sys
+
+def _bencode(f, g):
+    for h in g:
+        if isinstance(h, dict):
+            f.write(b'd')
+            _bencode(f, itertools.chain.from_iterable(h.items()))
+            f.write(b'e')
+        elif type(h) is str:
+            s = h.encode()
+            s = str(len(s)).encode() + b':' + s
+            f.write(s)
+        elif type(h) is int:
+            f.write(b'i' + str(h).encode() + b'e')
+        elif isinstance(h, list):
+            f.write(b'l')
+            _bencode(f, h)
+            f.write(b'e')
+        elif isinstance(h, bytes):
+            f.write(str(len(h)).encode() + b':' + h)
+        else:
+            assert False, type(h)
+
+def encode(obj):
+    f = io.BytesIO()
+    _bencode(f, [obj])
+    return f.getvalue()
+
+def decode(f):
+    while True:
+        c = f.read(1).decode()
+        if not c:
+            return
+        if c == 'd':
+            a = iter(decode(f))
+            yield collections.OrderedDict(zip(map(bytes.decode, a), a))
+        elif c == 'i':
+            i = ''
+            while True:
+                c = f.read(1).decode()
+                if not c:
+                    return
+                if c == 'e':
+                    yield int(i)
+                    break
+                else:
+                    i += c
+        elif c == 'l':
+            yield list(decode(f))
+        elif c == 'e':
+            return
+        else:
+            i = c
+            while True:
+                c = f.read(1).decode()
+                assert c, ('Unterminated key:', i)
+                if c == ':':
+                    yield f.read(int(i))
+                    break
+                else:
+                    i += c
+
+def main():
+    import argparse
+
+    parser = argparse.ArgumentParser()
+    parser.add_argument('file', nargs='?')
+    parser.add_argument('-d', default=False, action='store_true')
+    args = parser.parse_args()
+    from functools import partial
+    if args.file:
+        f = partial(open, args.file)
+    else:
+        f = partial(os.fdopen, sys.stdin.fileno())
+    if args.d:
+        pprint.pprint(buncode(f('rb')).__next__())
+    else:
+        sys.stdout.buffer.write(bencode(eval(f('r').read())))
+
+if __name__ == '__main__':
+    main()