diff --git a/README.md b/README.md index d7f3532..3b958c7 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,11 @@ -配置文件 -====== +转换 Excel 文档成为 CSV 和 LUA 格式 +=================================== -* 文档作者: zm -* 最后更新: 2014-12-09 +- 作者: zm [zrong](http://zengrong.net) +- 创建: 2014-12-09 +- 修改: 2015-03-04 将模块名称从 hhlc 改为 xlc。 -该Python库用于保存和转换黄鹤楼项目中的配置文件。 - -# 安装 +# 1. 安装 第一次使用时,必须先进行安装以保证依赖库存在。可以使用下面的方式调用 pip 来安装依赖库: @@ -19,34 +18,32 @@ Windows 系统,若 pip 命令不可用,可使用下面的命令: 依赖库文件可以是本地文件或者 url,下面两个地址都可用: ../client/requirements.txt - http://192.168.18.18/project/hhl/tool/requirements.txt - -# 使用 + http://192.168.18.18/project/1201/tool/requirements.txt -## 作为库使用 +# 2. 使用 -### 引用hhlc库 +## 2.1 转换配置文件 - import hhlc +调用方式如下: -### 参数说明 + import xlc + xlc.call(xls, export, templ, command, ptype) -* xls +- xls Excel 文件和配置文件目录 -* export +- export 最终文件的输出目录 -* tmpl +- tmpl 模版文件的目录 -* command +- command 执行的命令, list类型, 传空list表示转换所有配置文件 -* ptype -指定转换配置文件的类型, 如all/csv/lua +- ptype +指定转换配置文件的类型, 如 all/csv/lua - hhlc.main(xls, export, command, ptype) ### 转换所有配置文件到export路径 - hhlc.main( + xlc.call( "D:\\works\\hhl\\projects\\config\\xls", "D:\\works\\hhl\\projects\\config\\export", "D:\\works\\hhl\\projects\\config\\templates", @@ -56,7 +53,7 @@ Excel 文件和配置文件目录 ### 转换指定的一个或多个配置文件到export路径 - hhlc.main( + xlc.call( "D:\\works\\hhl\\projects\\config\\xls", "D:\\works\\hhl\\projects\\config\\export", "D:\\works\\hhl\\projects\\config\\templates", @@ -66,10 +63,29 @@ Excel 文件和配置文件目录 ### 转换指定类型的配置文件, 并指定导出路径 - hhlc.main( + xlc.call( "D:\\works\\hhl\\projects\\config\\xls", "D:\\works\\hhl\\projects\\config\\export", "D:\\works\\hhl\\projects\\config\\templates", ["hero", "skill"], "lua" ) + +## 2.3 转换技能配置文件(与 ET 工具配合使用) + +调用方式如下: + + import xlc.etc + xlc.etc.call(heroPath, sszPath, exportPath) + +- heroPath ET 工具使用的英雄 JSON 配置文件路径 +- sszPath ET 工具使用的其他 JSON 配置 +- exportPath 导出的配置文件路径 + +范例代码: + + xlc.etc.call( + "D:\\works\\hhl\\projects\\resource\\art", + "D:\\works\\hhl\\projects\\resource\\art\\ssz", + "D:\\works\\hhl\\projects\\resource\\skill" + ) diff --git a/setup.py b/setup.py index 6d6f4b7..fdab063 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ entry_points = { 'console_scripts': [ - 'hhlc = hhlc:main', + 'xlc = xlc:call', ] } @@ -32,13 +32,13 @@ # for root, dirs, files in os.walk('bin')] setup( - name="hhlc", - version="0.2.4", + name="xlc", + version="0.3.1", url='http://zengrong.net/', author='zrong', author_email='zrongzrong@gmail.com', description="Convert xls to lua and csv file.", - packages=['hhlc'], + packages=['xlc'], classifiers=classifiers, #include_package_data=True, #package_data=package_data, diff --git a/hhlc/__init__.py b/xlc/__init__.py similarity index 80% rename from hhlc/__init__.py rename to xlc/__init__.py index ab66a94..bc2c201 100644 --- a/hhlc/__init__.py +++ b/xlc/__init__.py @@ -3,9 +3,9 @@ # Creation 2014-09-26 import os -from zrong.base import getFiles -from hhlc.conf import Parser, ParseError -import hhlc.dirconf as dirconf +from zrong.base import get_files +from xlc.conf import Parser, ParseError +import xlc.dirconf as dirconf # 排除非模块文件 @@ -46,7 +46,7 @@ def call(xls, export, tmpl, command=[], ptype="all"): elif ptype == "csv": dirconf.updateCsvPath(export) - files = filter(excludeFiles, getFiles(dirconf.xls_path, ["py"])) + files = filter(excludeFiles, get_files(dirconf.xls_path, ["py"])) if isinstance(command, list) and len(command) != 0: files = get_theModule(files, command) # + ".py" @@ -63,13 +63,13 @@ def callEtc(heroPath, sszPath, exportPath): # import etc etc.call(heroPath, sszPath, exportPath) -if __name__ == '__main__': - callDt( - "D:\\works\\hhl\\projects\\config\\xls", - "D:\\works\\hhl\\projects\\client\\src\\conf", - "D:\\works\\hhl\\projects\\config\\templates", - ["skill"], - "lua") +# if __name__ == '__main__': + # callDt( + # "D:\\works\\hhl\\projects\\config\\xls", + # "D:\\works\\hhl\\projects\\client\\src\\conf", + # "D:\\works\\hhl\\projects\\config\\templates", + # ["skill"], + # "lua") # callEtc( # "D:\\works\\hhl\\projects\\resource\\art", diff --git a/hhlc/base.py b/xlc/base.py similarity index 98% rename from hhlc/base.py rename to xlc/base.py index 52dffbb..c9db693 100644 --- a/hhlc/base.py +++ b/xlc/base.py @@ -59,7 +59,7 @@ def _initFuns(self): pass def defaultStr(self, *args): - return '"%s"' % str(args[0]) + return '"%s"' % str(args[0]).replace("\n", "\\n") def defaultInt(self, *args): if args[0] != 0 and not args[0]: diff --git a/hhlc/conf.py b/xlc/conf.py similarity index 92% rename from hhlc/conf.py rename to xlc/conf.py index d70d95e..40ca91f 100644 --- a/hhlc/conf.py +++ b/xlc/conf.py @@ -4,10 +4,10 @@ import os import xlrd -from zrong.base import getFiles, readFile -from hhlc.tocsv import Tocsv -from hhlc.tolua import Tolua -import hhlc.dirconf as dirconf +from zrong.base import (read_file) +from xlc.tocsv import Tocsv +from xlc.tolua import Tolua +import xlc.dirconf as dirconf class ParseError(Exception): @@ -34,7 +34,7 @@ def parseModules(self, files): for path in files: print("parse file: " + os.path.basename(path)) - self.module = self.sheetCheck(eval(readFile(path))) + self.module = self.sheetCheck(eval(read_file(path))) for sheet in self.module.get("sheets"): self.parseSheet(sheet) @@ -100,4 +100,4 @@ def readXls(self, sheet): __data = [__table.row_values(i) for i in range(nrows)] - return __data \ No newline at end of file + return __data diff --git a/hhlc/dirconf.py b/xlc/dirconf.py similarity index 100% rename from hhlc/dirconf.py rename to xlc/dirconf.py diff --git a/hhlc/etc.py b/xlc/etc.py similarity index 59% rename from hhlc/etc.py rename to xlc/etc.py index 7352a90..78c8570 100644 --- a/hhlc/etc.py +++ b/xlc/etc.py @@ -3,11 +3,12 @@ # etc = effect tool convertor # Author zm # Creation 2014-09-26 -# Modification zrong 2015-01-09 +# Modification zrong 2015-03-04 import re import os import json +from zrong import slog from zrong.base import (write_file, read_file, get_files, slog) @@ -17,6 +18,11 @@ def __init__(self, export): self.export = export self.file_dir = os.getcwd() + def parseTmpl(self, path): + jsontxt = read_file(os.path.join(path, "tmpl.json")) + self.tmpl = json.loads(jsontxt) + pass + def parseConf(self, obj, fileName): start = '["%s"]=\n' end = '\n\n' @@ -35,7 +41,7 @@ def parseHero(self, files): for path in files: heroId = os.path.basename(path).split('.')[0] - print("parse ect:", heroId) + # print("parse ect:", heroId) jsontxt = read_file(path) # replace // jsontxt = re.sub(r'\/\/.*$', '', jsontxt, flags=re.M) @@ -85,51 +91,34 @@ def _parseSkillKey(self, key, heroId): def getSkill(self, tab, id, obj): _tab = self._tab(tab) - # print(len(args)) - return _tab + 'id=' + id + ',\n' + \ - _tab + 'effects_b=' + self.getValue(obj["effects_b"]) + ',\n' + \ - _tab + 'effects_b_no=' + self.getTable(obj["effects_b_no"]) + ',\n' + \ - _tab + 'effects_f=' + self.getValue(obj["effects_f"]) + ',\n' + \ - _tab + 'effects_f_no=' + self.getTable(obj["effects_f_no"]) + ',\n' + \ - _tab + 't_sex=' + self.getValue(obj["t_sex"]) + ',\n' + \ - _tab + 't_syb=' + self.getValue(obj["t_syb"]) + ',\n' + \ - _tab + 't_sb=' + self.getValue(obj["t_sb"]) + ',\n' + \ - _tab + 'ce_b=' + self.getValue(obj["ce_b"]) + ',\n' + \ - _tab + 'ce_b_no=' + self.getValue(obj["ce_b_no"]) + ',\n' + \ - _tab + 'ce_f=' + self.getValue(obj["ce_f"]) + ',\n' + \ - _tab + 'ce_f_no=' + self.getValue(obj["ce_f_no"]) + ',\n' + \ - _tab + 't_syx=' + self.getValue(obj["t_syx"]) + ',\n' + \ - _tab + 't_sd=' + self.getValue(obj["t_sd"]) + ',\n' + \ - _tab + 'zoom=' + self.getValue(obj["zoom"]) + ',\n' + \ - _tab + 'zoom_no=' + self.getValue(obj["zoom_no"]) + ',\n' + \ - _tab + 'shake=' + self.getValue(obj["shake"]) + ',\n' + \ - _tab + 'shake_no=' + self.getValue(obj["shake_no"]) + ',\n' + \ - _tab + 'shine1=' + self.getValue(obj["shine1"]) + ',\n' + \ - _tab + 'shine1_no=' + self.getValue(obj["shine1_no"]) + ',\n' + \ - _tab + 'shine2=' + self.getValue(obj["shine2"]) + ',\n' + \ - _tab + 'shine2_no=' + self.getValue(obj["shine2_no"]) + tmpStr = _tab + 'id=' + id + ',\n' + + for val in self.tmpl["hero_action"]: + if val[0] in obj: + tmpStr += _tab + \ + val[0] + '=' + \ + self.getValue(obj[val[0]], val[1]) + \ + ',\n' + + return tmpStr[0:-2] def _parseEnemy_action(self, tab, obj): _tab = self._tab(tab) - return ',\n' + \ - _tab + 'e_effects_no=' + self.getValue(obj["e_effects_no"]) + ',\n' + \ - _tab + 'e_shine1=' + self.getValue(obj["e_shine1"]) + ',\n' + \ - _tab + 'e_shine1_no=' + self.getValue(obj["e_shine1_no"]) + ',\n' + \ - _tab + 'e_shine2=' + self.getValue(obj["e_shine2"]) + ',\n' + \ - _tab + 'e_shine2_no=' + self.getValue(obj["e_shine2_no"]) + ',\n' + \ - _tab + 'e_desc=' + self.getValue(obj["e_desc"]) + ',\n' + \ - _tab + 'e_desc_no=' + self.getValue(obj["e_desc_no"]) + ',\n' + \ - _tab + 'e_icon=' + self.getValue(obj["e_icon"]) + ',\n' + \ - _tab + 'e_icon_no=' + self.getValue(obj["e_icon_no"]) + ',\n' + \ - _tab + 'e_effects_b=' + self.getValue(obj["e_effects_b"]) + ',\n' + \ - _tab + 'e_effects_b_no=' + self.getValue(obj["e_effects_b_no"]) + ',\n' + \ - _tab + 'e_effects_f=' + self.getValue(obj["e_effects_f"]) + ',\n' + \ - _tab + 'e_effects_f_no=' + self.getValue(obj["e_effects_f_no"]) + ',\n' + \ - _tab + 'e_arm=' + self.getValue(obj["e_arm"]) + ',\n' + \ - _tab + 'e_arm_no=' + self.getValue(obj["e_arm_no"]) + ',\n' + \ - _tab + 'e_label=' + self.getValue(obj["e_label"]) + '\n' - - def getValue(self, val): + tmpStr = ',\n' + + for val in self.tmpl["enemy_action"]: + if val[0] in obj: + tmpStr += _tab + \ + val[0] + '=' + \ + self.getValue(obj[val[0]], val[1]) + \ + ',\n' + + return tmpStr[0:-2] + '\n' + + def getValue(self, val, fmt): + if fmt == "table": + return self.getTable(val) + val = str(val) # '10.0' try: tmp = float(val) # 10.0 @@ -189,16 +178,18 @@ def confFiles(path): # 获取play.json -def playFiles(path): - __fname = os.path.basename(path) - for exclude in ["play.json"]: - if exclude == __fname: - return True - return False +# def playFiles(path): +# __fname = os.path.basename(path) +# for exclude in ["play.json"]: +# if exclude == __fname: +# return True +# return False def call(heroPath, sszPath, exportPath): parser = Parser(exportPath) + #parse tmpl + parser.parseTmpl(heroPath) # parse heros files = filter(herosFiles, get_files(heroPath, ["json"])) diff --git a/hhlc/tocsv.py b/xlc/tocsv.py similarity index 87% rename from hhlc/tocsv.py rename to xlc/tocsv.py index 9b986ce..8c9f870 100644 --- a/hhlc/tocsv.py +++ b/xlc/tocsv.py @@ -4,8 +4,8 @@ import os import csv -from hhlc.base import Tobase, Totype -import hhlc.dirconf as dirconf +from xlc.base import Tobase, Totype +import xlc.dirconf as dirconf class Tocsv(Tobase): @@ -27,7 +27,7 @@ def parse(self, plan, head, data): self.chkDirPath(filePath) - csvfile = open(filePath, 'w', newline='') + csvfile = open(filePath, 'w', encoding='utf8', newline='') writer = csv.writer(csvfile) # head @@ -49,11 +49,15 @@ def parse(self, plan, head, data): __head = head[conf['index']] elif "head" in conf: __head = conf['head'] + elif "key" in conf: + __head = conf['key'] + # 如果数据为空, 并且指定的head在忽略列表就不添加此行 if __head and __head in plan.get("ignore_head"): addRow = False break - if addRow: writer.writerow(__row) + if addRow: + writer.writerow(__row) csvfile.close() diff --git a/hhlc/tolua.py b/xlc/tolua.py similarity index 92% rename from hhlc/tolua.py rename to xlc/tolua.py index 69940ee..024417d 100644 --- a/hhlc/tolua.py +++ b/xlc/tolua.py @@ -1,11 +1,12 @@ # parseLua.py # Author zm # Creation 2014-09-29 +# Modification 2015-03-04 zrong import os -from zrong.base import writeByTempl -from hhlc.base import Tobase, Totype -import hhlc.dirconf as dirconf +from zrong.base import write_by_templ +from xlc.base import Tobase, Totype +import xlc.dirconf as dirconf class Tolua(Tobase): @@ -43,12 +44,16 @@ def parse(self, plan, head, data): __head = head[conf['index']] elif "head" in conf: __head = conf['head'] + elif "key" in conf: + __head = conf['key'] + # 如果数据为空, 并且指定的head在忽略列表就不添加此行 if __head and __head in plan.get("ignore_head"): addRow = False break __rowStr += end - if addRow: tmpStr += __rowStr + if addRow: + tmpStr += __rowStr self._createFile(tmpStr, plan.get("true_name")) @@ -151,7 +156,7 @@ def _createFile(self, str, true_name): self.chkDirPath(filePath) - writeByTempl( + write_by_templ( os.path.join(dirconf.temp_path, "%s.lua" % self.getPlanKey("template", "default")), filePath,