forked from mirror/ledisdb
75 lines
2.1 KiB
Python
75 lines
2.1 KiB
Python
#!/usr/bin/env python
|
|
|
|
import json
|
|
import time
|
|
from collections import OrderedDict as dict
|
|
|
|
|
|
def go_array_to_json(path):
|
|
"""Convert `./cmd/ledis-cli/const.go` to commands.json"""
|
|
fp = open(path).read()
|
|
commands_str = fp.split("string")[1]
|
|
_commands_str = commands_str.splitlines()[1:len(commands_str.splitlines())-1]
|
|
commands_d = dict()
|
|
values_d = dict()
|
|
for i in _commands_str:
|
|
t = i.split('"')
|
|
values_d.update(
|
|
{
|
|
"arguments": "%s" % t[3],
|
|
"group": "%s" % t[5]
|
|
})
|
|
values_d = dict(sorted(values_d.items()))
|
|
d = {
|
|
"%s" % t[1]: values_d
|
|
}
|
|
commands_d.update(d)
|
|
|
|
fp = open("commands.json", "w")
|
|
json.dump(commands_d, fp, indent=4)
|
|
fp.close()
|
|
|
|
|
|
def json_to_js(json_path, js_path):
|
|
"""Convert `commands.json` to `commands.js`"""
|
|
keys = []
|
|
with open(json_path) as fp:
|
|
_json = json.load(fp)
|
|
for k in _json.keys():
|
|
keys.append(k.encode('utf-8'))
|
|
with open(js_path, "w") as fp:
|
|
generate_time(fp)
|
|
fp.write("module.exports = [\n" )
|
|
for k in sorted(keys):
|
|
fp.write('\t"%s",\n' % k.lower())
|
|
fp.write("]")
|
|
|
|
|
|
def json_to_go_array(json_path, go_path):
|
|
g_fp = open(go_path, "w")
|
|
with open(json_path) as fp:
|
|
_json = json.load(fp)
|
|
generate_time(g_fp)
|
|
g_fp.write("package main\n\nvar helpCommands = [][]string{\n")
|
|
_json_sorted = dict(sorted(_json.items(), key=lambda x: x[0]))
|
|
for k, v in _json_sorted.iteritems():
|
|
print k, v
|
|
g_fp.write('\t{"%s", "%s", "%s"},\n' % (k, v["arguments"], v["group"]))
|
|
g_fp.write("}\n")
|
|
g_fp.close()
|
|
|
|
|
|
def generate_time(fp):
|
|
fp.write("//This file was generated by ./generate.py on %s \n" % \
|
|
time.strftime('%a %b %d %Y %H:%M:%S %z'))
|
|
|
|
if __name__ == "__main__":
|
|
path = "./cmd/ledis-cli/const.go"
|
|
# go_array_to_json(path)
|
|
json_path = "./commands.json"
|
|
js_path = "./commands.js"
|
|
json_to_js(json_path, js_path)
|
|
go_path = "const.go"
|
|
|
|
json_to_go_array(json_path, path)
|