diff --git a/doc/commands.json b/doc/commands.json index 521f86e..8af2c90 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -17,7 +17,7 @@ "BEXPIREAT": { "arguments": "key timestamp", "group": "Bitmap", - "readonly": false, + "readonly": false }, "BGET": { "arguments": "key", diff --git a/generate.py b/generate.py index beba9e5..1295e22 100644 --- a/generate.py +++ b/generate.py @@ -2,32 +2,18 @@ import json import time +import sys +import os 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() +content = u"""\n +type cmdConf struct { + name string + argDesc string + group string + readonly bool +} +""" def json_to_js(json_path, js_path): @@ -39,7 +25,7 @@ def json_to_js(json_path, js_path): keys.append(k.encode('utf-8')) with open(js_path, "w") as fp: generate_time(fp) - fp.write("module.exports = [\n" ) + fp.write("module.exports = [\n") for k in sorted(keys): fp.write('\t"%s",\n' % k.lower()) fp.write("]") @@ -53,22 +39,64 @@ def json_to_go_array(json_path, go_path): 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 json_to_command_cnf(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 server") + print >> g_fp, content + g_fp.write("var cnfCmds = []cmdConf{\n") + for k, v in _json.iteritems(): + g_fp.write('\t{\n\t\t"%s",\n\t\t"%s",\n\t\t"%s", \n\t\t%s,\n\t},\n' % + (k, v["arguments"], v["group"], "true" if v["readonly"] else "false" )) + 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')) + 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" + usage = """ + Usage: python %s src_path dst_path" - json_to_go_array(json_path, path) + 1. for Node.js client: + + python generate.py /path/to/commands.json /path/to/commands.js + + 2. for cmd/ledis_cli/const.go + + python generate.py /path/to/commands.json /path/to/const.go + + 3. for server/command_cnf.go + + python generate.py /path/to/commands.json /path/to/command_cnf.go + + """ + + if len(sys.argv) != 3: + sys.exit(usage % os.path.basename(sys.argv[0])) + + src_path, dst_path = sys.argv[1:] + dst_path_base = os.path.basename(dst_path) + + if dst_path_base.endswith(".js"): + json_to_js(src_path, dst_path) + + elif dst_path_base.startswith("const.go"): + json_to_go_array(src_path, dst_path) + + elif dst_path_base.startswith("command"): + json_to_command_cnf(src_path, dst_path) + + else: + print "Not support arguments" diff --git a/server/command_cnf.go b/server/command_cnf.go new file mode 100644 index 0000000..e830592 --- /dev/null +++ b/server/command_cnf.go @@ -0,0 +1,510 @@ +//This file was generated by ./generate.py on Mon Aug 11 2014 12:35:56 +0800 +package server + +type cmdConf struct { + name string + argDesc string + group string + readonly bool +} + +var cnfCmds = []cmdConf{ + { + "ZRANGEBYSCORE", + "key min max [WITHSCORES] [LIMIT offset count]", + "ZSet", + true, + }, + { + "ZPERSIST", + "key", + "ZSet", + false, + }, + { + "LTTL", + "key", + "List", + true, + }, + { + "LINDEX", + "key index", + "List", + true, + }, + { + "FULLSYNC", + "-", + "Replication", + false, + }, + { + "ZREVRANK", + "key member", + "ZSet", + true, + }, + { + "ZEXPIRE", + "key seconds", + "ZSet", + false, + }, + { + "SYNC", + "index offset", + "Replication", + false, + }, + { + "BMSETBIT", + "key offset value [offset value ...]", + "Bitmap", + false, + }, + { + "LPOP", + "key", + "List", + false, + }, + { + "HPERSIST", + "key", + "Hash", + false, + }, + { + "EXPIRE", + "key seconds", + "KV", + false, + }, + { + "DEL", + "key [key ...]", + "KV", + false, + }, + { + "LPUSH", + "key value [value ...]", + "List", + false, + }, + { + "PERSIST", + "key", + "KV", + false, + }, + { + "HTTL", + "key", + "Hash", + true, + }, + { + "LEXPIREAT", + "key timestamp", + "List", + false, + }, + { + "ZEXPIREAT", + "key timestamp", + "ZSet", + false, + }, + { + "DECR", + "key", + "KV", + false, + }, + { + "SLAVEOF", + "host port", + "Replication", + false, + }, + { + "INCR", + "key", + "KV", + false, + }, + { + "MSET", + "key value [key value ...]", + "KV", + false, + }, + { + "LEXPIRE", + "key seconds", + "List", + false, + }, + { + "HINCRBY", + "key field increment", + "Hash", + false, + }, + { + "GET", + "key", + "KV", + true, + }, + { + "ZREVRANGE", + "key start stop [WITHSCORES]", + "ZSet", + true, + }, + { + "ZINCRBY", + "key increment member", + "ZSet", + false, + }, + { + "LPERSIST", + "key", + "List", + false, + }, + { + "HEXISTS", + "key field", + "Hash", + true, + }, + { + "ZREM", + "key member [member ...]", + "ZSet", + false, + }, + { + "BOPT", + "operation destkey key [key ...]", + "Bitmap", + false, + }, + { + "ZCLEAR", + "key", + "ZSet", + false, + }, + { + "LCLEAR", + "key", + "List", + false, + }, + { + "ZRANK", + "key member", + "ZSet", + true, + }, + { + "TTL", + "key", + "KV", + true, + }, + { + "ZADD", + "key score member [score member ...]", + "ZSet", + false, + }, + { + "HEXPIRE", + "key seconds", + "Hash", + false, + }, + { + "HDEL", + "key field [field ...]", + "Hash", + false, + }, + { + "HSET", + "key field value", + "Hash", + false, + }, + { + "LLEN", + "key", + "List", + true, + }, + { + "HVALS", + "key", + "Hash", + true, + }, + { + "BCOUNT", + "key [start end]", + "Bitmap", + true, + }, + { + "BGET", + "key", + "Bitmap", + true, + }, + { + "MGET", + "key [key ...]", + "KV", + true, + }, + { + "EXISTS", + "key", + "KV", + true, + }, + { + "HMCLEAR", + "key [key ...]", + "Hash", + false, + }, + { + "ZCOUNT", + "key min max", + "ZSet", + true, + }, + { + "SELECT", + "index", + "Server", + false, + }, + { + "ECHO", + "message", + "Server", + true, + }, + { + "ZTTL", + "key", + "ZSet", + true, + }, + { + "HKEYS", + "key", + "Hash", + true, + }, + { + "HGETALL", + "key", + "Hash", + true, + }, + { + "RPOP", + "key", + "List", + false, + }, + { + "HMGET", + "key field [field ...]", + "Hash", + true, + }, + { + "SETNX", + "key value", + "KV", + false, + }, + { + "HGET", + "key field", + "Hash", + true, + }, + { + "BPERSIST", + "key", + "Bitmap", + false, + }, + { + "INCRBY", + "key increment", + "KV", + false, + }, + { + "BDELETE", + "key", + "ZSet", + false, + }, + { + "ZMCLEAR", + "key [key ...]", + "ZSet", + false, + }, + { + "RPUSH", + "key value [value ...]", + "List", + false, + }, + { + "LRANGE", + "key start stop", + "List", + true, + }, + { + "HLEN", + "key", + "Hash", + true, + }, + { + "ZSCORE", + "key member", + "ZSet", + true, + }, + { + "LMCLEAR", + "key [key ...]", + "List", + false, + }, + { + "EXPIREAT", + "key timestamp", + "KV", + false, + }, + { + "ZREMRANGEBYSCORE", + "key min max", + "ZSet", + false, + }, + { + "ZCARD", + "key", + "ZSet", + true, + }, + { + "ZREMRANGEBYRANK", + "key start stop", + "ZSet", + false, + }, + { + "PING", + "-", + "Server", + true, + }, + { + "HMSET", + "key field value [field value ...]", + "Hash", + false, + }, + { + "BTTL", + "key", + "Bitmap", + true, + }, + { + "HCLEAR", + "key", + "Hash", + false, + }, + { + "ZRANGE", + "key start stop [WITHSCORES]", + "ZSet", + false, + }, + { + "ZREVRANGEBYSCORE", + "key max min [WITHSCORES][LIMIT offset count]", + "ZSet", + true, + }, + { + "BSETBIT", + "key offset value", + "Bitmap", + false, + }, + { + "BEXPIREAT", + "key timestamp", + "Bitmap", + false, + }, + { + "SET", + "key value", + "KV", + false, + }, + { + "BGETBIT", + "key offset", + "Bitmap", + true, + }, + { + "BEXPIRE", + "key seconds", + "Bitmap", + false, + }, + { + "GETSET", + " key value", + "KV", + false, + }, + { + "DECRBY", + "key decrement", + "KV", + false, + }, + { + "HEXPIREAT", + "key timestamp", + "Hash", + false, + }, +}