【特性】新增cfg python27 json支持

main
walon 2021-12-22 18:10:35 +08:00
parent 8e4ef0952d
commit 51dbe96917
14 changed files with 223 additions and 14 deletions

View File

@ -8,7 +8,7 @@ namespace Luban.Job.Cfg
[Option('s', "service", Required = true, HelpText = "service")]
public string Service { get; set; }
[Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_python3_json,code_typescript_bin,code_typescript_json,code_rust_json,code_protobuf,code_template,code_flatbuffers,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources,data_template,data_protobuf_bin,data_protobuf_json,data_flatbuffers_json,convert_json,convert_lua,convert_xlsx . can be multi")]
[Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_python2_json,code_python3_json,code_typescript_bin,code_typescript_json,code_rust_json,code_protobuf,code_template,code_flatbuffers,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources,data_template,data_protobuf_bin,data_protobuf_json,data_flatbuffers_json,convert_json,convert_lua,convert_xlsx . can be multi")]
public string GenType { get; set; }
[Option("input_data_dir", Required = true, HelpText = "input data dir")]

View File

@ -91,7 +91,7 @@ namespace Luban.Job.Cfg.Generate
var file = outputFile;
var md5 = CacheFileUtil.GenMd5AndAddCache(file, string.Join("\n", fileContent));
ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
ctx.GenScatteredFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 });
}));
}

View File

@ -0,0 +1,37 @@
using Luban.Common.Protos;
using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Tpl;
using Luban.Job.Common.Utils;
using System.Collections.Generic;
namespace Luban.Job.Cfg.Generate
{
[Render("code_python2_json")]
class Python2CodeJsonRender : TemplateCodeRenderBase
{
protected override string RenderTemplateDir => "python2_json";
public override void Render(GenContext ctx)
{
ctx.Render = this;
ctx.Lan = Common.ELanguage.PYTHON;
DefAssembly.LocalAssebmly.CurrentLanguage = ctx.Lan;
var lines = new List<string>(10000);
static void PreContent(List<string> fileContent)
{
//fileContent.Add(PythonStringTemplates.ImportTython3Enum);
//fileContent.Add(PythonStringTemplates.PythonVectorTypes);
fileContent.Add(StringTemplateManager.Ins.GetTemplateString("config/python2_json/include"));
}
GenerateCodeMonolithic(ctx, RenderFileUtil.GetFileOrDefault(ctx.GenArgs.OutputCodeMonolithicFile, "Types.py"), lines, PreContent, null);
//string indexFileName = "__init__.py";
//string indexFileContent = "";
//var md5 = CacheFileUtil.GenMd5AndAddCache(indexFileName, indexFileContent);
//ctx.GenScatteredFiles.Add(new FileInfo() { FilePath = indexFileName, MD5 = md5 });
}
}
}

View File

@ -6,11 +6,10 @@ using System.Collections.Generic;
namespace Luban.Job.Cfg.Generate
{
[Render("code_python_json")]
[Render("code_python3_json")]
class Python3CodeJsonRender : TemplateCodeRenderBase
{
protected override string RenderTemplateDir => "python_json";
protected override string RenderTemplateDir => "python3_json";
public override void Render(GenContext ctx)
{
@ -23,7 +22,7 @@ namespace Luban.Job.Cfg.Generate
{
//fileContent.Add(PythonStringTemplates.ImportTython3Enum);
//fileContent.Add(PythonStringTemplates.PythonVectorTypes);
fileContent.Add(StringTemplateManager.Ins.GetTemplateString("config/python_json/include"));
fileContent.Add(StringTemplateManager.Ins.GetTemplateString("config/python3_json/include"));
}
GenerateCodeMonolithic(ctx, RenderFileUtil.GetFileOrDefault(ctx.GenArgs.OutputCodeMonolithicFile, "Types.py"), lines, PreContent, null);

View File

@ -2,8 +2,7 @@ namespace Luban.Job.Common.Utils
{
public static class FileHeaderUtil
{
const string AUTO_GENERATE_C_LIKE = @"
//------------------------------------------------------------------------------
const string AUTO_GENERATE_C_LIKE = @"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
@ -13,8 +12,7 @@ namespace Luban.Job.Common.Utils
";
const string AUTO_GENERATE_LUA = @"
--[[------------------------------------------------------------------------------
const string AUTO_GENERATE_LUA = @"--[[------------------------------------------------------------------------------
-- <auto-generated>
-- This code was generated by a tool.
-- Changes to this file may cause incorrect behavior and will be lost if
@ -23,7 +21,7 @@ namespace Luban.Job.Common.Utils
--]]------------------------------------------------------------------------------
";
const string AUTO_GENERATE_PYTHON = @"
const string AUTO_GENERATE_PYTHON = @"#-*- coding: utf-8 -*-
'''
<auto-generated>
This code was generated by a tool.

View File

@ -214,16 +214,28 @@
<None Update="Templates\config\protobuf2\tables.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\python_json\bean.tpl">
<None Update="Templates\config\python2_json\bean.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\python_json\include.tpl">
<None Update="Templates\config\python2_json\include.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\python_json\table.tpl">
<None Update="Templates\config\python2_json\table.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\python_json\tables.tpl">
<None Update="Templates\config\python2_json\tables.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\python3_json\bean.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\python3_json\include.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\python3_json\table.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\python3_json\tables.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\rust_json\bean.tpl">

View File

@ -0,0 +1,40 @@
{{
name = x.py_full_name
is_abstract_type = x.is_abstract_type
parent_def_type = x.parent_def_type
export_fields = x.export_fields
hierarchy_export_fields = x.hierarchy_export_fields
}}
class {{name}} {{if parent_def_type}}({{parent_def_type.py_full_name}}){{end}}:
{{~if x.is_abstract_type~}}
_childrenTypes = None
@staticmethod
def fromJson(_json_):
childrenTypes = {{name}}._childrenTypes
if not childrenTypes:
childrenTypes = {{name}}._childrenTypes = {
{{~ for child in x.hierarchy_not_abstract_children~}}
'{{child.name}}': {{child.py_full_name}},
{{~end~}}
}
type = _json_['__type__']
child = {{name}}._childrenTypes.get(type)
if child != None:
return child(_json_)
else:
raise Exception()
{{~end~}}
def __init__(self, _json_):
{{~if parent_def_type~}}
{{parent_def_type.py_full_name}}.__init__(self, _json_)
{{~end~}}
{{~ for field in export_fields ~}}
{{py3_deserialize_field ('self.' + field.convention_name) '_json_' field.name field.ctype}}
{{~end~}}
{{~if export_fields.empty?}}
pass
{{~end~}}

View File

@ -0,0 +1,54 @@
from enum import Enum
import abc
class Vector2:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return '{%g,%g}' % (self.x, self.y)
@staticmethod
def fromJson(_json_):
x = _json_['x']
y = _json_['y']
if (x == None or y == None):
raise Exception()
return Vector2(x, y)
class Vector3:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __str__(self):
return '{%f,%f,%f}' % (self.x, self.y, self.z)
@staticmethod
def fromJson(_json_):
x = _json_['x']
y = _json_['y']
z = _json_['z']
if (x == None or y == None or z == None):
raise Exception()
return Vector3(x, y, z)
class Vector4:
def __init__(self, x, y, z, w):
self.x = x
self.y = y
self.z = z
self.w = w
def __str__(self):
return '{%g,%g,%g,%g}' % (self.x, self.y, self.z, self.w)
@staticmethod
def fromJson(_json_):
x = _json_['x']
y = _json_['y']
z = _json_['z']
w = _json_['w']
if (x == None or y == None or z == None or w == None):
raise Exception()
return Vector4(x, y, z, w)

View File

@ -0,0 +1,54 @@
{{
name = x.py_full_name
key_type = x.key_ttype
key_type1 = x.key_ttype1
key_type2 = x.key_ttype2
value_type = x.value_ttype
}}
class {{name}}:
{{~if x.is_map_table ~}}
def __init__(self, _json_ ):
self._dataMap = {}
self._dataList = []
for _json2_ in _json_:
{{py3_deserialize_value '_v' '_json2_' value_type}}
self._dataList.append(_v)
self._dataMap[_v.{{x.index_field.convention_name}}] = _v
def getDataMap(self) : return self._dataMap
def getDataList(self) : return self._dataList
def get(self, key) : return self._dataMap.get(key)
{{~else if x.is_list_table ~}}
def __init__(self, _json_ ):
self._dataList = []
for _json2_ in _json_:
{{py3_deserialize_value '_v' '_json2_' value_type}}
self._dataList.append(_v)
def getDataList(self) : return self._dataList
def get(self, index) : return self._dataList[index]
{{~else~}}
def __init__(self, _json_):
if (len(_json_) != 1): raise Exception('table mode=one, but size != 1')
{{py3_deserialize_value 'self._data' '_json_[0]' value_type}}
def getData(self) : return self._data
{{~ for field in value_type.bean.hierarchy_export_fields ~}}
{{~if field.comment != '' ~}}
'''
{{field.escape_comment}}
'''
{{~end~}}
def {{field.convention_name}}(self) : return self._data.{{field.convention_name}}
{{~end~}}
{{~end~}}

View File

@ -0,0 +1,15 @@
{{
name = x.name
namespace = x.namespace
tables = x.tables
}}
class {{name}}:
{{~ for table in tables ~}}
#def {{table.name}} : return self._{{table.name}}
{{~end~}}
def __init__(self, loader):
{{~for table in tables ~}}
self.{{table.name}} = {{table.py_full_name}}(loader('{{table.output_data_file}}'));
{{~end~}}