-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImportConfig.py
89 lines (78 loc) · 2.4 KB
/
ImportConfig.py
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
86
87
88
89
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Author: NormanYang
import os.path
import time
import os
import sys
import codecs
OUTPUT_FILE = "Config/ConfigAll.lua"
SCRIPT_HEAD = "-- This file is generated by program!\n\
-- Don't change it manaully.\n\
-- Author: zerospace007@163.com NormanYang\n\
-- Source file: %s\n\
-- Created at: %s\n\
\n\
\n\
"
TABLE_HEAD = "\n--配置文件管理--\nConfigAll = {};\n\
local this = ConfigAll;\n\
local configList = {};\n\
"
CONFIG_NAME = "\n--配置管理名称--\n\
ConfigName =\n{\n%s}\n\
"
LOCAL_REQUIRE_NAME = "\n--load require管理名称--\n\
local LoadRq =\n{\n%s}\n\
"
FUNCTION_LOAD_ALL = "\n--添加所有配置到管理列表--\nfunction ConfigAll.LoadAll()\n\
%send\
"
FUNCTION_MANAGER = "\n\n--添加配置表--\n\
function ConfigAll.AddConfig(configName, configObj)\n\
configList[configName] = configObj;\n\
end\n\
\n--获取配置表--\n\
function ConfigAll.GetConfig(configName)\n\
return configList[configName];\n\
end\n\
\n--移除配置表--\n\
function ConfigAll.RemoveConfig(configName)\n\
configList[configName] = nil;\n\
end\n\
\nreturn ConfigAll;\n\
"
def write_to_lua_script(data):
#写入文件头
outfp = codecs.open(OUTPUT_FILE, 'w', 'UTF-8');
# create_time = time.strftime("%a %b %d %H:%M:%S %Y", time.gmtime(time.time()))
create_time = time.strftime("%b %Y", time.gmtime(time.time()))
outfp.write(SCRIPT_HEAD % (OUTPUT_FILE, create_time));
#写入文件内容
config_name = "";
load_item = "";
require_name = "";
for lua_file in data:
config_name += " " + lua_file + " = " + "\"" + lua_file + "\",\n";
require_name += " " + lua_file + " = require " + "\"Config/" + lua_file + "\",\n";
load_item += " this.AddConfig(ConfigName." + lua_file + ", LoadRq." + lua_file + ");\n";
outfp.write(LOCAL_REQUIRE_NAME % require_name);
outfp.write(CONFIG_NAME % config_name);
outfp.write(TABLE_HEAD);
outfp.write(FUNCTION_LOAD_ALL % load_item);
outfp.write(FUNCTION_MANAGER);
outfp.close();
def handler_path(lua_path):
from platform import python_version
print('Python', python_version())
if (os.path.exists(OUTPUT_FILE)==True):
os.remove(OUTPUT_FILE);
data = [];
for parent,dirnames,filenames in os.walk(lua_path):
for filename in filenames:
if (parent == lua_path):
data.append(os.path.splitext(filename)[0]);
print(data);
write_to_lua_script(data)
if __name__=="__main__":
handler_path("Config/")