-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexcel2lua_final.py
More file actions
executable file
·86 lines (72 loc) · 2.87 KB
/
excel2lua_final.py
File metadata and controls
executable file
·86 lines (72 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env python
#coding=utf-8
'''
#========================================================================
# FileName: excel2lua_final.py
# Author: kevinlin
# Desc: 调用excel2lua_mod.py来生成配置
# Email: linjiang1205 AT qq.com
# LastChange: 2014-12-20 16:55:32
#========================================================================
'''
import os
import sys
import commands
from excel2lua_mod import *
import getopt
def usage():
print '''
Usage: %s excel_file sheet_name ["repeated=[0|1]", "subkey=NUM" , "output=[c|s|cs|sc]"]
excel_file: the source file of config
sheet_name: the sheet you want convent to lua
repeated: is key repeated??
0 means no repeated key inside
1 means the key maybe repeated, then parse them as one table
subkey: table nested. point out which column is the sub key
output: [c|s|cs|sc] client or svr or both??
c means client, has some different with s
s means server, has some different with c
''' %(sys.argv[0])
if __name__ == '__main__' :
if len(sys.argv) < 3 :
usage()
sys.exit(-1)
xls_file_name = sys.argv[1]
#读取sheet名,转为大写字母
sheet_name = sys.argv[2].upper();
#是否有重复的key,为0时表示key不重复。为1时有重复key,将对某个key产生多一层的table
have_repeated_key = 0
#当有重复的key时,这里指定index从0开始,还是以第二列的数值为index(默认从0开始。参考Dungen.xls)
sub_key_col = 0
#输出目标、client/svr
output=""
try:
opt, args = getopt.getopt(sys.argv[3:], "hr:s:o:", ["help", "repeated=", "subkey=", "output="])
except getopt.GetoptError, err:
print "err:",(err)
usage()
sys.exit(-1)
for op, value in opt:
if op == "-h" :
usage()
elif op == "-r" or op == "--repeated":
have_repeated_key = int(value)
elif op == "-s" or op == "--subkey":
sub_key_col = int(value)
elif op == "-o" or op == "--output":
output = value
logging.debug("have_repeated_key:%d|sub_key_col:%d|output:%s"%(have_repeated_key, sub_key_col, output))
outputstr = {"c": "client", "s":"svr"}
for i in output:
if not outputstr.has_key(i):
usage()
sys.exit(-2)
parser = CardInterpreter(xls_file_name,sheet_name, have_repeated_key, sub_key_col)
parser.Interpreter(outputstr[i])
file_name = GetFileName(outputstr[i], sheet_name)
sheet_name_lower = sheet_name.lower();
context = OutputFileHeader(xls_file_name, file_name, sheet_name_lower, False)
context += parser.GetResult()
Write2File(file_name,context)
# 试运行产生的lua脚本看是否会报错
TestLuaFile(file_name)