Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 39 lines (31 sloc) 853 Bytes
#!/usr/bin/python -B
"""
Converts the first JSON item in stdin to bencode
"""
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 collections
import json
# Needed on Windows, else output newlines will be corrupted
try:
import msvcrt
except ImportError:
pass
else:
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
def encode_latin_1_unicode(x, r):
bencode.encode_string(x.encode("latin-1"), r)
# This is the most convenient place to add support for our latin-1
# "Unicode" strings, unfortunately.
bencode.encode_func[unicode] = encode_latin_1_unicode
if sys.argv[1:]:
f = open(sys.argv[1], "rb")
else:
f = sys.stdin
sys.stdout.write(
bencode.bencode(
json.loads(
f.read(),
object_pairs_hook=collections.OrderedDict)))