OSDN Git Service

three.jsをThirdPartyに追加
[webglgame/webgl_framework.git] / webglFramework / Thirdparty / three.js-master / utils / converters / msgpack / json2msgpack.py
1 #!/usr/bin/env python
2
3 __doc__ = '''
4 Convert a json file to msgpack.
5
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
9 foo.json > foo.pack
10
11 Specify an output file path
12 $ json2msgpack.py -i foo.json -o /bar/tmp/bar.pack
13 foo.json > /bar/tmp/bar.pack
14
15 Dependencies:
16 https://github.com/msgpack/msgpack-python
17 '''
18
19 import os
20 import sys
21 import json
22 import argparse
23
24 sys.path.append(os.path.dirname(os.path.realpath(__file__)))
25
26 import msgpack
27
28 EXT = '.pack'
29
30 def main():
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)
39
40 def convert(infile, outfile):
41     if not outfile:
42         ext = infile.split('.')[-1]
43         outfile = '%s%s' % (infile[:-len(ext)-1], EXT)
44
45     print('%s > %s' % (infile, outfile))
46
47     print('reading in JSON')
48     with open(infile) as op:
49         data = json.load(op)
50
51     print('writing to msgpack')
52     with open(outfile, 'wb') as op:
53         msgpack.dump(data, op)
54
55 if __name__ == '__main__':
56     main()