553 lines
19 KiB
Python
553 lines
19 KiB
Python
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
from old_to_new import type_change
|
|
import os
|
|
import re
|
|
from type2 import format_key_type_list, construct_lua_data, construct_erl_data, construct_data, construct_go_data, need_export_table
|
|
|
|
COLOR_INDEX = (
|
|
'00000000', '00FFFFFF', '00FF0000', '0000FF00', '000000FF', #0-4
|
|
'00FFFF00', '00FF00FF', '0000FFFF', '00000000', '00FFFFFF', #5-9
|
|
'00FF0000', '0000FF00', '000000FF', '00FFFF00', '00FF00FF', #10-14
|
|
'0000FFFF', '00800000', '00008000', '00000080', '00808000', #15-19
|
|
'00800080', '00008080', '00C0C0C0', '00808080', '009999FF', #20-24
|
|
'00993366', '00FFFFCC', '00CCFFFF', '00660066', '00FF8080', #25-29
|
|
'000066CC', '00CCCCFF', '00000080', '00FF00FF', '00FFFF00', #30-34
|
|
'0000FFFF', '00800080', '00800000', '00008080', '000000FF', #35-39
|
|
'0000CCFF', '00CCFFFF', '00CCFFCC', '00FFFF99', '0099CCFF', #40-44
|
|
'00FF99CC', '00CC99FF', '00FFCC99', '003366FF', '0033CCCC', #45-49
|
|
'0099CC00', '00FFCC00', '00FF9900', '00FF6600', '00666699', #50-54
|
|
'00969696', '00003366', '00339966', '00003300', '00333300', #55-59
|
|
'00993300', '00993366', '00333399', '00333333', '00000000', '00FFFFFF' #60-64
|
|
)
|
|
|
|
def create_dirs(path_info):
|
|
src_folder = path_info['folder_path']
|
|
dest_folder = path_info['dump_path']
|
|
file_path = path_info['file_path']
|
|
|
|
dest_file_path = file_path.replace(src_folder, dest_folder)
|
|
deep_dir = os.path.dirname(dest_file_path)
|
|
if not os.path.isdir(deep_dir):
|
|
try:
|
|
os.makedirs(deep_dir)
|
|
except Exception as e:
|
|
print str(e)
|
|
if not os.path.isdir(deep_dir):
|
|
raise Exception("could not create dir(%s)" % deep_dir)
|
|
return deep_dir
|
|
|
|
|
|
def index_to_rgb(i):
|
|
color = COLOR_INDEX[i]
|
|
color_int = int(color, 16)
|
|
r, g, b = color_int >> 16, (color_int >> 8) & 0xff , color_int & 0xff
|
|
return r, g, b
|
|
|
|
def xlsx_get_function(sheet):
|
|
def gmr():
|
|
return sheet.max_row
|
|
|
|
def gmc():
|
|
return sheet.max_column
|
|
|
|
def access(i, j):
|
|
return sheet.cell(i+1, j+1).value
|
|
|
|
def access_color(i, j):
|
|
fill = sheet.cell(i+1, j+1).fill
|
|
if not fill:
|
|
return 255, 255, 255
|
|
x = fill.start_color.index
|
|
if isinstance(x, (int, long)):
|
|
return index_to_rgb(x)
|
|
else:
|
|
color_int = int(x, 16) & 0x00FFFFFF
|
|
r, g, b = color_int >> 16, (color_int >> 8) & 0xff, color_int & 0xff
|
|
return r, g, b
|
|
|
|
def get_rows():
|
|
return sheet.rows
|
|
return gmr, gmc, access, access_color, get_rows
|
|
|
|
|
|
def xls_get_function(sheet, wb):
|
|
def gmr():
|
|
return sheet.nrows
|
|
def gmc():
|
|
return sheet.ncols
|
|
def access(i, j, xf=False):
|
|
if xf:
|
|
return sheet.cell(i, j).xf_index
|
|
return sheet.cell_value(i, j)
|
|
def access_color(i, j):
|
|
xf = wb.xf_list[sheet.cell_xf_index(i, j)]
|
|
index = xf.background.pattern_colour_index
|
|
return index_to_rgb(index)
|
|
|
|
return gmr, gmc, access, access_color
|
|
|
|
def xlsx_read_new(path):
|
|
from openpyxl import load_workbook
|
|
wb = load_workbook(path, data_only=True)
|
|
data = []
|
|
rule = []
|
|
data_read = False
|
|
for sheet in wb:
|
|
sheet_name = sheet.title
|
|
gmr, gmc, access, access_color, get_rows = xlsx_get_function(sheet)
|
|
if sheet_name == 'rule':
|
|
rule = read_rule(gmr, gmc, access)
|
|
elif not data_read:
|
|
data = read_data_new(gmr, gmc, access, access_color, path, get_rows)
|
|
data_read = True
|
|
wb.close()
|
|
return {
|
|
'data': data,
|
|
'rule': rule,
|
|
}
|
|
|
|
def read_rule(get_max_row, get_max_col, access):
|
|
max_row = get_max_row()
|
|
max_col = get_max_col()
|
|
rules = []
|
|
for i in range(max_row):
|
|
rule = []
|
|
if not access(i, 0):
|
|
break
|
|
for j in range(max_col):
|
|
value = access(i, j)
|
|
if not value:
|
|
break
|
|
rule.append(value)
|
|
rules.append(rule)
|
|
return rules
|
|
|
|
|
|
# return {head_list, name_list, type_list, export_color, lang_color, data}
|
|
def read_data_old(get_max_row, get_max_col, access, access_color, get_rows=None):
|
|
max_col = get_max_col()
|
|
status = 'name_row'
|
|
name_list = []
|
|
type_list = []
|
|
head_list = []
|
|
export_color = []
|
|
lang_color = []
|
|
data_row = 0
|
|
head_row = 0
|
|
name_row = 0
|
|
type_row = 0
|
|
|
|
for i in range(10):
|
|
col_1 = access(i, 0)
|
|
if isinstance(col_1, (str, unicode)) and col_1.startswith("//"):
|
|
head_row = i
|
|
for j in range(max_col):
|
|
head_list.append(access(i, j))
|
|
elif not col_1 or not isinstance(col_1, (str, unicode)) or col_1.isdigit():
|
|
continue
|
|
elif status in ('name_row',):
|
|
name_row = i
|
|
for j in range(max_col):
|
|
value = access(i, j)
|
|
color = access_color(i, j)
|
|
if not value:
|
|
name_list.append(None)
|
|
else:
|
|
name_list.append(value)
|
|
lang_color.append(color)
|
|
status = 'type_row'
|
|
elif status in ('type_row', ):
|
|
type_row = i
|
|
for j in range(max_col):
|
|
value = access(i, j)
|
|
color = access_color(i, j)
|
|
if not value:
|
|
type_list.append(None)
|
|
else:
|
|
type_list.append(value)
|
|
export_color.append(color)
|
|
data_row = i+1
|
|
break
|
|
|
|
max_name = 0
|
|
for i in range(len(name_list)):
|
|
if name_list[i]:
|
|
max_name = i + 1
|
|
|
|
name_list = name_list[:max_name]
|
|
type_list = type_list[:max_name]
|
|
export_color = export_color[:max_name]
|
|
lang_color = lang_color[:max_name]
|
|
|
|
excel_rows = get_max_row()
|
|
max_row = data_row
|
|
for i in range(data_row, excel_rows):
|
|
try:
|
|
value = access(i, 0)
|
|
if value:
|
|
max_row = i + 1
|
|
except KeyError:
|
|
break
|
|
if data_row == 0:
|
|
raise Exception("excel file data is wrong")
|
|
|
|
data_list = []
|
|
if get_rows:
|
|
rows = get_rows()
|
|
cur_row = 0
|
|
for row in rows:
|
|
if cur_row < data_row:
|
|
cur_row += 1
|
|
continue
|
|
one_row = []
|
|
col_1 = row[0].value
|
|
if col_1 is None or col_1 == "":
|
|
continue
|
|
for j in range(max_col):
|
|
v = row[j].value
|
|
if v is None:
|
|
one_row.append("")
|
|
else:
|
|
one_row.append(v)
|
|
data_list.append(one_row)
|
|
else:
|
|
for i in range(data_row, max_row):
|
|
one_row = []
|
|
col_1 = access(i, 0)
|
|
if col_1 is None or col_1 == "":
|
|
continue
|
|
for j in range(max_col):
|
|
v = access(i, j)
|
|
if v is None:
|
|
one_row.append("")
|
|
else:
|
|
one_row.append(v)
|
|
data_list.append(one_row)
|
|
|
|
return {
|
|
'head_list': head_list,
|
|
'name_list': name_list,
|
|
'type_list': type_list,
|
|
'data_list': data_list,
|
|
'export_color': export_color,
|
|
'lang_color': lang_color,
|
|
'head_row': head_row,
|
|
'name_row': name_row,
|
|
'type_row': type_row,
|
|
'data_row': data_row,
|
|
'max_row': max_row,
|
|
}
|
|
|
|
|
|
# return {head_list, name_list, type_list, export_color, lang_color, data}
|
|
def read_data_new(get_max_row, get_max_col, access, _access_color, path, get_rows=None):
|
|
max_row = get_max_row()
|
|
max_col = get_max_col()
|
|
status = 'export_row'
|
|
head_row, export_row, name_row, type_row, data_row = 0, 0, 0, 0, 0
|
|
head_list, export_list, name_list, type_list, data_list = [], [], [], [] ,[]
|
|
|
|
start_i = 0
|
|
for i in range(10):
|
|
col_1 = access(i, 0)
|
|
if not col_1:
|
|
continue
|
|
if col_1 and isinstance(col_1, (str, unicode)) and col_1.startswith("//"):
|
|
continue
|
|
start_i = i
|
|
break
|
|
|
|
for i in range(start_i, 10):
|
|
if status in ('export_row', ):
|
|
export_row = i
|
|
for j in range(max_col):
|
|
export_list.append(access(i, j))
|
|
status = 'name_row'
|
|
elif status in ('name_row', ):
|
|
name_row = i
|
|
for j in range(max_col):
|
|
name_list.append(access(i, j))
|
|
status = 'type_row'
|
|
elif status in ('type_row', ):
|
|
type_row = i
|
|
data_row = i + 1
|
|
for j in range(max_col):
|
|
type_list.append((access(i, j)))
|
|
break
|
|
else:
|
|
raise Exception('unknown error')
|
|
|
|
max_col = len(type_list)
|
|
while not type_list[max_col-1]:
|
|
max_col -= 1
|
|
type_list = type_list[:max_col]
|
|
|
|
if data_row == 0:
|
|
raise Exception("excel file data is wrong")
|
|
|
|
data_list = []
|
|
spaceRowCountDown = 0
|
|
space_row_list = []
|
|
|
|
if get_rows:
|
|
rows = get_rows()
|
|
cur_row = 0
|
|
for row in rows:
|
|
if cur_row < data_row:
|
|
cur_row += 1
|
|
continue
|
|
one_row = []
|
|
col_1 = row[0].value
|
|
if col_1 is None or col_1 == "":
|
|
cur_row += 1
|
|
space_row_list.append(cur_row)
|
|
spaceRowCountDown += 1
|
|
if spaceRowCountDown >= 10:
|
|
raise Exception("file name %s more than ten space row" % path)
|
|
break
|
|
else:
|
|
continue
|
|
else:
|
|
cur_row += 1
|
|
spaceRowCountDown = 0
|
|
for j in range(max_col):
|
|
v = row[j].value
|
|
if v is None:
|
|
one_row.append("")
|
|
else:
|
|
one_row.append(v)
|
|
data_list.append(one_row)
|
|
else:
|
|
for i in range(data_row, max_row):
|
|
one_row = []
|
|
col_1 = access(i, 0)
|
|
# if col_1 is None or col_1 == "":
|
|
# continue
|
|
if col_1 is None or col_1 == "":
|
|
spaceRowCountDown += 1
|
|
if spaceRowCountDown >= 10:
|
|
raise Exception("file name %s more than ten space row" % path)
|
|
break
|
|
else:
|
|
continue
|
|
else:
|
|
spaceRowCountDown = 0
|
|
for j in range(max_col):
|
|
v = access(i, j)
|
|
if v is None:
|
|
one_row.append("")
|
|
else:
|
|
one_row.append(v)
|
|
data_list.append(one_row)
|
|
|
|
return {
|
|
'head_row': head_row,
|
|
'export_row': export_row,
|
|
'name_row': name_row,
|
|
'type_row': type_row,
|
|
'data_row': data_row,
|
|
'head_list': head_list,
|
|
'export_list': export_list,
|
|
'name_list': name_list,
|
|
'type_list': type_list,
|
|
'data_list': data_list,
|
|
'space_row_list': space_row_list,
|
|
}
|
|
|
|
def convert(path, struct_config, config=None):
|
|
os.path.relpath(path['file_path'], path['folder_path'])
|
|
excel_data = xlsx_read_new(path['file_path'])
|
|
excel_data["config"] = struct_config
|
|
|
|
excel_objs = []
|
|
match_result = re.match('.+strings', path['file_path'])
|
|
|
|
if match_result:
|
|
#对多语言相关表进行特殊处理
|
|
name_type_dict = dict()
|
|
main_keys = []
|
|
none_count = 0
|
|
for i in range(len(excel_data['data']['name_list'])):
|
|
if i == 0 or excel_data['data']['export_list'][i] == 'none':
|
|
continue
|
|
|
|
name = excel_data['data']['name_list'][i]
|
|
if name == None:
|
|
none_count = none_count + 1
|
|
if none_count > 10:
|
|
raise Exception('more than ten none column xx-xx path %s' % path['file_path'])
|
|
return
|
|
continue
|
|
|
|
split_data = name.split('-')
|
|
if len(split_data) != 2 and (not name.startswith('*')):
|
|
raise Exception('local config name is not xx-xx(lanuage) path %s ' % path['file_path'])
|
|
return
|
|
|
|
#特殊处理名字
|
|
if name.startswith('*'):
|
|
main_keys.append(i)
|
|
continue
|
|
|
|
excel_data['data']['name_list'][i] = split_data[0]
|
|
if name_type_dict.has_key(split_data[1]):
|
|
name_type_dict[split_data[1]].append(i)
|
|
else:
|
|
name_type_dict[split_data[1]] = []
|
|
name_type_dict[split_data[1]].append(i)
|
|
|
|
|
|
#除id外主键加入
|
|
for language, indexs in name_type_dict.items():
|
|
for main_key_index in main_keys:
|
|
indexs.append(main_key_index)
|
|
|
|
for language, indexs in name_type_dict.items():
|
|
excel_tmp_data = dict()
|
|
excel_tmp_data['data'] = dict()
|
|
excel_tmp_data['data']['head_row'] = excel_data['data']['head_row']
|
|
excel_tmp_data['data']['export_row'] = excel_data['data']['export_row']
|
|
excel_tmp_data['data']['name_row'] = excel_data['data']['name_row']
|
|
excel_tmp_data['data']['type_row'] = excel_data['data']['type_row']
|
|
excel_tmp_data['data']['data_row'] = excel_data['data']['data_row']
|
|
excel_tmp_data['data']['space_row_list'] = excel_data['data']['space_row_list']
|
|
excel_tmp_data['rule'] = excel_data['rule']
|
|
excel_tmp_data['config'] = excel_data['config']
|
|
|
|
excel_tmp_data['data']['head_list'] = []
|
|
excel_tmp_data['data']['name_list'] = []
|
|
excel_tmp_data['data']['type_list'] = []
|
|
excel_tmp_data['data']['export_list'] = []
|
|
excel_tmp_data['data']['data_list'] = []
|
|
|
|
#添加主键
|
|
excel_tmp_data['data']['name_list'].append(excel_data['data']['name_list'][0])
|
|
excel_tmp_data['data']['type_list'].append(excel_data['data']['type_list'][0])
|
|
excel_tmp_data['data']['export_list'].append(excel_data['data']['export_list'][0])
|
|
data_row = len(excel_data['data']['data_list'])
|
|
for w in range(data_row):
|
|
excel_tmp_data['data']['data_list'].append([])
|
|
excel_tmp_data['data']['data_list'][w].append(excel_data['data']['data_list'][w][0])
|
|
|
|
for i in range(len(indexs)):
|
|
index = indexs[i]
|
|
excel_tmp_data['data']['name_list'].append(excel_data['data']['name_list'][index])
|
|
excel_tmp_data['data']['type_list'].append(excel_data['data']['type_list'][index])
|
|
excel_tmp_data['data']['export_list'].append(excel_data['data']['export_list'][index])
|
|
|
|
for j in range(data_row):
|
|
excel_tmp_data['data']['data_list'][j].append(excel_data['data']['data_list'][j][index])
|
|
|
|
deep_dir = os.path.join(path['dump_path'], 'strings')
|
|
deep_dir = os.path.join(deep_dir, language)
|
|
|
|
if not os.path.isdir(deep_dir):
|
|
try:
|
|
os.makedirs(deep_dir)
|
|
except Exception as e:
|
|
print str(e)
|
|
|
|
if not os.path.isdir(deep_dir):
|
|
raise Exception("could not create dir(%s)" % deep_dir)
|
|
|
|
excel_obj = conver_pri(excel_tmp_data, path, deep_dir, config)
|
|
if excel_obj:
|
|
excel_objs.append(excel_obj)
|
|
else:
|
|
deep_dir = create_dirs(path)
|
|
excel_obj = conver_pri(excel_data, path, deep_dir, config)
|
|
if excel_obj:
|
|
excel_objs.append(excel_obj)
|
|
return excel_objs
|
|
|
|
|
|
def conver_pri(excel_data, path, deep_dir, config=None):
|
|
file_name = os.path.basename(path['file_path']).split('.')[0]
|
|
reldir = os.path.relpath(os.path.dirname(path['file_path']), path['folder_path'])
|
|
package_name = 'config'
|
|
if reldir == ".":
|
|
if config.get('go_package'):
|
|
package_name = config.get("go_package", "config")
|
|
else:
|
|
package_name = os.path.basename(reldir)
|
|
|
|
kts, type_mapping = format_key_type_list(excel_data['data']['name_list'], excel_data['data']['type_list'],
|
|
excel_data['data']['export_list'], excel_data['config'])
|
|
|
|
#如果所有字段都为服务器需要或None则客户端不需要导出
|
|
if need_export_table(kts):
|
|
return None
|
|
|
|
key_set, full_data = construct_data(path, kts, type_mapping, excel_data['data']['data_list'], excel_data['data']['space_row_list'])
|
|
|
|
erl_suffix = 'erl'
|
|
lua_suffix = 'lua'
|
|
go_suffix = 'go'
|
|
lua_compact = False
|
|
if config and config.get('erl_suffix'):
|
|
erl_suffix = config['erl_suffix']
|
|
if config and config.get('lua_suffix'):
|
|
lua_suffix = config['lua_suffix']
|
|
if config and config.get('lua_compact'):
|
|
lua_compact = True
|
|
|
|
if config and config.get('erlang'):
|
|
erl_data = construct_erl_data(kts, type_mapping, key_set, full_data, file_name, '\t')
|
|
write_file(os.path.join(deep_dir, 'cfg_%s.%s' % (file_name, erl_suffix)), erl_data)
|
|
del erl_data
|
|
|
|
if config and config.get('lua'):
|
|
lua_data = construct_lua_data(path, kts, type_mapping, key_set, full_data, file_name, lua_compact, '\t')
|
|
name = '%s.%s' % (file_name, lua_suffix)
|
|
if config and config.get('lua_force_lowercase', False):
|
|
name = name.lower()
|
|
write_file(os.path.join(deep_dir, name), lua_data)
|
|
del lua_data
|
|
|
|
if config and config.get('go'):
|
|
go_data = construct_go_data(kts, type_mapping, key_set, full_data, file_name, package_name, '\t')
|
|
name = 'cfg_%s.%s' % (file_name, go_suffix)
|
|
name = name.lower()
|
|
write_file(os.path.join(deep_dir, name), go_data)
|
|
del go_data
|
|
|
|
excel_obj = dict()
|
|
excel_obj["file_name"] = os.path.basename(path["file_path"])
|
|
excel_obj["file_path"] = path["file_path"]
|
|
excel_obj["data"] = full_data
|
|
excel_obj["key_set"] = key_set
|
|
excel_obj["rules"] = excel_data['rule']
|
|
excel_obj["kts"] = kts
|
|
excel_obj["type_mapping"] = type_mapping
|
|
excel_obj["space_row_list"] = excel_data['data']['space_row_list']
|
|
return excel_obj
|
|
|
|
def write_file(file_path, data):
|
|
f = open(file_path, "w")
|
|
f.write(data.encode('utf-8'))
|
|
f.close()
|
|
"""
|
|
write_new_excel = _write_excel(excel_data, path)
|
|
|
|
excel_data = xlrd.open_workbook(path["file_path"], formatting_info=True)
|
|
file_name = util.get_file_name(path["file_path"])
|
|
|
|
config_sheet = read_config_sheet(excel_data)
|
|
data_sheet = read_data_sheet(excel_data)
|
|
rules = init_rule(excel_data)
|
|
rows = write_to_lua(data_sheet, config_sheet, file_name, path)
|
|
excel_data.release_resources()
|
|
del excel_data
|
|
|
|
excel_obj = {}
|
|
excel_obj["file_name"] = os.path.basename(path["file_path"])
|
|
excel_obj["file_path"] = path["file_path"]
|
|
excel_obj["data_sheet"] = data_sheet
|
|
excel_obj["rows"] = rows
|
|
excel_obj["rules"] = rules
|
|
|
|
print "convert " + file_name + " finish"
|
|
return excel_obj
|
|
""" |