【废弃】废弃对python27的支持

main
walon 2021-08-14 13:02:26 +08:00
parent 3e8bfb1d77
commit 31df88bf48
5 changed files with 24 additions and 212 deletions

View File

@ -160,14 +160,27 @@ namespace Luban.Job.Cfg.Defs
{ {
if (type.IsNullable) if (type.IsNullable)
{ {
return $"if {jsonVarName} != None: {type.Apply(PyUnderingDeserializeVisitor.Py3Ins, jsonVarName, fieldName)}"; return $"if {jsonVarName} != None: {type.Apply(PyUnderingDeserializeVisitor.Ins, jsonVarName, fieldName)}";
} }
else else
{ {
return type.Apply(PyUnderingDeserializeVisitor.Py3Ins, jsonVarName, fieldName); return type.Apply(PyUnderingDeserializeVisitor.Ins, jsonVarName, fieldName);
} }
} }
public static string Py3DeserializeField(string fieldName, string jsonVarName, string jsonFieldName, TType type)
{
if (type.IsNullable)
{
return $"if {jsonVarName}.get('{jsonFieldName}') != None: {type.Apply(PyUnderingDeserializeVisitor.Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName)}";
}
else
{
return type.Apply(PyUnderingDeserializeVisitor.Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName);
}
}
/*
public static string Py27DeserializeValue(string fieldName, string jsonVarName, TType type) public static string Py27DeserializeValue(string fieldName, string jsonVarName, TType type)
{ {
if (type.IsNullable) if (type.IsNullable)
@ -180,18 +193,6 @@ namespace Luban.Job.Cfg.Defs
} }
} }
public static string Py3DeserializeField(string fieldName, string jsonVarName, string jsonFieldName, TType type)
{
if (type.IsNullable)
{
return $"if {jsonVarName}.get('{jsonFieldName}') != None: {type.Apply(PyUnderingDeserializeVisitor.Py3Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName)}";
}
else
{
return type.Apply(PyUnderingDeserializeVisitor.Py3Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName);
}
}
public static string Py27DeserializeField(string fieldName, string jsonVarName, string jsonFieldName, TType type) public static string Py27DeserializeField(string fieldName, string jsonVarName, string jsonFieldName, TType type)
{ {
if (type.IsNullable) if (type.IsNullable)
@ -203,5 +204,6 @@ namespace Luban.Job.Cfg.Defs
return type.Apply(PyUnderingDeserializeVisitor.Py27Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName); return type.Apply(PyUnderingDeserializeVisitor.Py27Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName);
} }
} }
*/
} }
} }

View File

@ -1,128 +0,0 @@
using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Defs;
using Scriban;
using System;
using System.Collections.Generic;
namespace Luban.Job.Cfg.Generate
{
class Python27CodeJsonRender : PythonCodeRenderBase
{
[ThreadStatic]
private static Template t_beanRender;
public override string Render(DefBean b)
{
var template = t_beanRender ??= Template.Parse(@"
{{
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
}}
{{~if x.comment != '' ~}}
'''
{{x.comment}}
'''
{{~end~}}
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 ~}}
{{~if !field.ctype.is_nullable~}}
if _json_['{{field.name}}'] == None: raise Exception()
{{~end~}}
{{py27_deserialize ('self.' + field.py_style_name) ('_json_[""' + field.name + '""]') field.ctype}}
{{~end~}}
{{~if export_fields.empty?}}
pass
{{~end~}}
");
var result = template.RenderCode(b);
return result;
}
[ThreadStatic]
private static Template t_tableRender;
public override string Render(DefTable p)
{
var template = t_tableRender ??= Template.Parse(@"
{{
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
}}
{{~if x.comment != '' ~}}
'''
{{x.comment}}
'''
{{~end~}}
class {{name}}:
{{~if x.is_map_table ~}}
def __init__(self, _json_ ):
self._dataMap = {}
self._dataList = []
for _json2_ in _json_:
{{py27_deserialize '_v' '_json2_' value_type}}
self._dataList.append(_v)
self._dataMap[_v.{{x.index_field.py_style_name}}] = _v
def getDataMap(self) : return self._dataMap
def getDataList(self) : return self._dataList
def get(self, key) : return self._dataMap.get(key)
{{~else~}}
def __init__(self, _json_):
if (len(_json_) != 1): raise Exception('table mode=one, but size != 1')
{{py27_deserialize '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.comment}}
'''
{{~end~}}
def {{field.py_style_name}}(self) : return self._data.{{field.py_style_name}}
{{~end~}}
{{~end~}}
");
var result = template.RenderCode(p);
return result;
}
}
}

View File

@ -46,7 +46,7 @@ namespace Luban.Job.Cfg
[Option("output_data_json_monolithic_file", Required = false, HelpText = "output monolithic json file")] [Option("output_data_json_monolithic_file", Required = false, HelpText = "output monolithic json file")]
public string OutputDataJsonMonolithicFile { get; set; } public string OutputDataJsonMonolithicFile { 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_go_bin,code_go_json,code_cpp_bin,code_python27_json,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json2,data_json_monolithic . 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_go_bin,code_go_json,code_cpp_bin,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json2,data_json_monolithic . can be multi")]
public string GenType { get; set; } public string GenType { get; set; }
[Option('s', "service", Required = true, HelpText = "service")] [Option('s', "service", Required = true, HelpText = "service")]
@ -87,7 +87,6 @@ namespace Luban.Job.Cfg
case "code_cpp_bin": return new CppCodeBinRender(); case "code_cpp_bin": return new CppCodeBinRender();
case "code_lua_bin": return new LuaCodeBinRender(); case "code_lua_bin": return new LuaCodeBinRender();
case "code_lua_lua": return new LuaCodeLuaRender(); case "code_lua_lua": return new LuaCodeLuaRender();
case "code_python27_json": return new Python27CodeJsonRender();
case "code_python3_json": return new Python3CodeJsonRender(); case "code_python3_json": return new Python3CodeJsonRender();
case "code_typescript_bin": return new TypescriptCodeBinRender(); case "code_typescript_bin": return new TypescriptCodeBinRender();
case "code_typescript_json": return new TypescriptCodeJsonRender(); case "code_typescript_json": return new TypescriptCodeJsonRender();
@ -112,7 +111,6 @@ namespace Luban.Job.Cfg
case "code_cpp_bin": return ELanguage.CPP; case "code_cpp_bin": return ELanguage.CPP;
case "code_lua_bin": case "code_lua_bin":
case "code_lua_lua": return ELanguage.LUA; case "code_lua_lua": return ELanguage.LUA;
case "code_python27_json":
case "code_python3_json": return ELanguage.PYTHON; case "code_python3_json": return ELanguage.PYTHON;
case "code_typescript_bin": case "code_typescript_bin":
case "code_typescript_json": return ELanguage.TYPESCRIPT; case "code_typescript_json": return ELanguage.TYPESCRIPT;
@ -359,7 +357,7 @@ namespace Luban.Job.Cfg
GenTypescriptCode(ctx); GenTypescriptCode(ctx);
break; break;
} }
case "code_python27_json": case "code_python3_json":
{ {
GenPythonCodes(ctx); GenPythonCodes(ctx);
break; break;
@ -590,29 +588,14 @@ namespace Luban.Job.Cfg
ctx.Render = CreateCodeRender(genType); ctx.Render = CreateCodeRender(genType);
ctx.Lan = GetLanguage(genType); ctx.Lan = GetLanguage(genType);
var isPython3 = genType.Contains("python3");
var lines = new List<string>(10000); var lines = new List<string>(10000);
Action<List<string>> preContent = (fileContent) => static void PreContent(List<string> fileContent)
{
if (isPython3)
{ {
fileContent.Add(PythonStringTemplates.ImportTython3Enum); fileContent.Add(PythonStringTemplates.ImportTython3Enum);
}
fileContent.Add(PythonStringTemplates.PythonVectorTypes); fileContent.Add(PythonStringTemplates.PythonVectorTypes);
}; }
GenerateCodeMonolithic(ctx, "Types.py", lines, preContent, null); GenerateCodeMonolithic(ctx, "Types.py", lines, PreContent, null);
ctx.Tasks.Add(Task.Run(() =>
{
var moduleInitContent = "";
var initFile = "__init__.py";
var initMd5 = CacheFileUtil.GenMd5AndAddCache(initFile, moduleInitContent);
ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = initFile, MD5 = initMd5 });
}));
} }
private void GenCppCode(GenContext ctx) private void GenCppCode(GenContext ctx)

View File

@ -1,36 +0,0 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
{
class PyDeserializeVisitor : DecoratorFuncVisitor<string, string, string>
{
public static PyDeserializeVisitor Py3Ins { get; } = new PyDeserializeVisitor(true);
public static PyDeserializeVisitor Py27Ins { get; } = new PyDeserializeVisitor(false);
public PyDeserializeVisitor(bool py3)
{
Python3 = py3;
UnderringVisitor = py3 ? PyUnderingDeserializeVisitor.Py3Ins : PyUnderingDeserializeVisitor.Py27Ins;
}
public bool Python3 { get; }
PyUnderingDeserializeVisitor UnderringVisitor { get; }
public override string DoAccept(TType type, string jsonFieldName, string fieldName)
{
if (type.IsNullable)
{
return $"if {jsonFieldName} != None: {type.Apply(UnderringVisitor, jsonFieldName, fieldName)}";
}
else
{
return type.Apply(UnderringVisitor, jsonFieldName, fieldName);
}
}
}
}

View File

@ -5,16 +5,7 @@ namespace Luban.Job.Cfg.TypeVisitors
{ {
class PyUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string> class PyUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
{ {
public static PyUnderingDeserializeVisitor Py3Ins { get; } = new PyUnderingDeserializeVisitor(true); public static PyUnderingDeserializeVisitor Ins { get; } = new();
public static PyUnderingDeserializeVisitor Py27Ins { get; } = new PyUnderingDeserializeVisitor(false);
public PyUnderingDeserializeVisitor(bool py3)
{
Python3 = py3;
}
public bool Python3 { get; }
public string Accept(TBool type, string jsonVarName, string fieldName) public string Accept(TBool type, string jsonVarName, string fieldName)
{ {
@ -68,7 +59,7 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TEnum type, string jsonVarName, string fieldName) public string Accept(TEnum type, string jsonVarName, string fieldName)
{ {
return Python3 ? $"{fieldName} = {type.DefineEnum.PyFullName}({jsonVarName})" : $"{fieldName} = {jsonVarName}"; return $"{fieldName} = {type.DefineEnum.PyFullName}({jsonVarName})";
} }
public string Accept(TString type, string jsonVarName, string fieldName) public string Accept(TString type, string jsonVarName, string fieldName)