4 Convert a json file to msgpack.
6 If fed only an input file the converted will write out a .pack file
7 of the same base name in the same directory
8 $ json2msgpack.py -i foo.json
11 Specify an output file path
12 $ json2msgpack.py -i foo.json -o /bar/tmp/bar.pack
13 foo.json > /bar/tmp/bar.pack
16 https://github.com/msgpack/msgpack-python
24 sys.path.append(os.path.dirname(os.path.realpath(__file__)))
31 parser = argparse.ArgumentParser()
32 parser.add_argument('-i', '--infile', required=True,
33 help='Input json file to convert to msgpack')
34 parser.add_argument('-o', '--outfile',
35 help=('Optional output. If not specified the .pack file '\
36 'will write to the same director as the input file.'))
37 args = parser.parse_args()
38 convert(args.infile, args.outfile)
40 def convert(infile, outfile):
42 ext = infile.split('.')[-1]
43 outfile = '%s%s' % (infile[:-len(ext)-1], EXT)
45 print('%s > %s' % (infile, outfile))
47 print('reading in JSON')
48 with open(infile) as op:
51 print('writing to msgpack')
52 with open(outfile, 'wb') as op:
53 msgpack.dump(data, op)
55 if __name__ == '__main__':