~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tools/file2json.py

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
Converts a binary file into JSON.
 
3
 
 
4
This lets you transform a binary file into something you can
 
5
easily bundle inside a web page.
 
6
 
 
7
Usage: file2json.py FILENAME VARNAME
 
8
 
 
9
FILENAME - the binary file
 
10
VARNAME - the variable to store it in (the output will be VARNAME = [...])
 
11
'''
 
12
 
 
13
import os, sys
 
14
 
 
15
data = open(sys.argv[1], 'r').read()
 
16
sdata = map(lambda x: str(ord(x)) + ',', data)
 
17
sdata[-1] = sdata[-1].replace(',', '')
 
18
lined = []
 
19
while len(sdata) > 0:
 
20
  lined += sdata[:30]
 
21
  sdata = sdata[30:]
 
22
  if len(sdata) > 0:
 
23
    lined += ['\n']
 
24
json = '[' + ''.join(lined) + ']'
 
25
 
 
26
if len(sys.argv) < 3:
 
27
  print json
 
28
else:
 
29
  print 'var ' + sys.argv[2] + '=' + json + ';'
 
30
 
 
31
'''
 
32
or (but this fails, we get a string at runtime?)
 
33
 
 
34
data = open(sys.argv[1], 'r').read()
 
35
counter = 0
 
36
print '[',
 
37
for i in range(len(data)):
 
38
  last = i == len(data)-1
 
39
  print ord(data[i]),
 
40
  counter += 1
 
41
  if counter % 20 == 0:
 
42
    print
 
43
  if counter % 1005 == 0 and not last:
 
44
    print '] + [',
 
45
  elif not last: print ',',
 
46
 
 
47
print ']'
 
48
'''
 
49