aboutsummaryrefslogtreecommitdiffstats
path: root/tools/json2hash
diff options
context:
space:
mode:
authorsmihica <smihica@gmail.com>2013-03-21 11:41:55 +0900
committersmihica <smihica@gmail.com>2013-03-21 11:41:55 +0900
commit918e5420388bded0dd6b3656cc2fe9f4ba9c0649 (patch)
treef71e2c1def3f0d7e6680123eed23c00d9f9c4cb4 /tools/json2hash
parent7ff95cafdcf85b5048e5ec51f5fd51a176e3d97e (diff)
downloademmet-mode-918e5420388bded0dd6b3656cc2fe9f4ba9c0649.tar.lz
emmet-mode-918e5420388bded0dd6b3656cc2fe9f4ba9c0649.tar.xz
emmet-mode-918e5420388bded0dd6b3656cc2fe9f4ba9c0649.zip
[add] Added CSS transform functions.
Diffstat (limited to 'tools/json2hash')
-rwxr-xr-xtools/json2hash90
1 files changed, 90 insertions, 0 deletions
diff --git a/tools/json2hash b/tools/json2hash
new file mode 100755
index 0000000..e8fa4f3
--- /dev/null
+++ b/tools/json2hash
@@ -0,0 +1,90 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import json
+import sys
+import os
+import re
+import argparse
+from cStringIO import StringIO
+
+class ApplicationException(Exception):
+ pass
+
+def pystr2elstrexp(s):
+ s = re.sub(r'^(u\'|\'|u\"|\")(.*)(\'|\")$', r'\2', repr(s))
+ output = re.sub(r'("|\\")', '\\"', s)
+ return u'"' + output + u'"'
+
+class Processor(object):
+
+ def __init__(self):
+ self.out = StringIO()
+
+ def _print(self, data):
+ _type = type(data)
+ if _type is dict:
+ self.out.write('(let ((tbl (make-hash-table :test \'equal)))\n')
+ for k, v in data.items():
+ self.out.write('(puthash ')
+ self.out.write(pystr2elstrexp(k))
+ self.out.write(' ')
+ self._print(v)
+ self.out.write(' tbl)\n')
+ self.out.write('tbl)')
+ elif _type is list:
+ self.out.write('(list \n')
+ for v in data:
+ self._print(v)
+ self.out.write('\n')
+ self.out.wirte(')\n')
+ elif (_type is str or _type is unicode):
+ self.out.write(pystr2elstrexp(data))
+ else: # number ?
+ self.out.write(str(data))
+
+ def read(self, file):
+ data = json.load(file)
+ self._print(data)
+
+ def write(self, out):
+ out.write(self.out.getvalue())
+
+def main():
+ argparser = argparse.ArgumentParser()
+ argparser.add_argument('-o', type=str)
+ argparser.add_argument('--defvar', type=str)
+ argparser.add_argument('file', nargs='+', type=str)
+ options = argparser.parse_args()
+ if options.o:
+ out = open(options.o, 'w')
+ else:
+ out = sys.stdout
+ try:
+ p = Processor()
+ for file in options.file:
+ try:
+ f = open(os.path.abspath(file))
+ p.read(f)
+ finally:
+ f.close()
+ out.write(';; ' + str(options.o) + '\n')
+ out.write(';; This file is generated from ' + ','.join(options.file) + '\n')
+ out.write(';; Don\'t edit.\n')
+ if options.defvar:
+ out.write(u'(zencoding-defparameter ' + options.defvar + u'\n')
+ p.write(out)
+ if options.defvar:
+ out.write(')\n')
+ except:
+ if options.o:
+ out.close()
+ os.unlink(options.o)
+ raise
+ if options.o:
+ out.close()
+
+if __name__ == '__main__':
+ try:
+ main()
+ except ApplicationException as e:
+ print >>sys.stderr, e.message