Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 30 lines (24 sloc) 727 Bytes
#!/usr/bin/python -B
"""
Converts the first bencode item in stdin to JSON
"""
import os
import sys
sys.path.append(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "lib", "quickmunge", "bencode"))
import bencode
import json
# Needed on Windows, else input newlines will be corrupted
try:
import msvcrt
except ImportError:
pass
else:
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
if sys.argv[1:]:
f = open(sys.argv[1], "rb")
else:
f = sys.stdin
# Must use latin-1 because there are non-utf-8 bytestrings in there
# We still output legal JSON, the Unicode escapes just represent bytes
# instead of Unicode codepoints.
print json.dumps(bencode.bdecode(f.read()), encoding="latin-1", indent=2)