diff options
author | max99x <max99x@gmail.com> | 2011-08-05 16:47:30 +0300 |
---|---|---|
committer | max99x <max99x@gmail.com> | 2011-08-05 16:47:30 +0300 |
commit | cda35f57e3f5cad30d9a0a7927b526f024b13c12 (patch) | |
tree | 06e1a4dca78ea604bc89e702265b15c984c4ae15 | |
parent | 274f2b8985ddd8ec91a1736764ffaf3b6c9d8128 (diff) | |
parent | 15d64a10cc64d2da2ee3d6e2b941c8707765d751 (diff) |
Merge remote-tracking branch 'upstream/master'
-rw-r--r-- | tools/file2json.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tools/file2json.py b/tools/file2json.py new file mode 100644 index 00000000..19f32e17 --- /dev/null +++ b/tools/file2json.py @@ -0,0 +1,27 @@ +''' +Converts a binary file into JSON. + +This lets you transform a binary file into something you can +easily bundle inside a web page. + +Usage: file2json.py FILENAME VARNAME + +FILENAME - the binary file +VARNAME - the variable to store it in (the output will be VARNAME = [...]) +''' + +import os, sys + +data = open(sys.argv[1], 'r').read() +sdata = map(lambda x: str(ord(x)) + ',', data) +sdata[-1] = sdata[-1].replace(',', '') +lined = [] +while len(sdata) > 0: + lined += sdata[:30] + sdata = sdata[30:] + if len(sdata) > 0: + lined += ['\n'] +json = '[' + ''.join(lined) + ']' + +print 'var ' + sys.argv[2] + '=' + json + ';' + |