public void LogAttributes(AttributeWalker projectAttributes) { Logger.LogHeading("Attributes - Class", LogLevel.Info); foreach (var entry in projectAttributes.ClassAttributes) { string target = entry.Key.ToString(); PythonClassAttribute attribute = entry.Value; Logger.Log($"{target} -> {attribute.ModuleName}:{attribute.ClassName} ({attribute.File}) {(attribute.GenerateMethods ? "*" : "")}", LogLevel.Info); } Logger.LogHeading("Attributes - Fields", LogLevel.Info); foreach (var entry in projectAttributes.FieldAttributes) { string target = entry.Key.ToString(); PythonFieldAttribute attribute = entry.Value; Logger.Log($"{target} -> {attribute.Name} ({attribute.File})", LogLevel.Info); } Logger.LogHeading("Attributes - Methods", LogLevel.Info); foreach (var entry in projectAttributes.MethodAttributes) { string target = entry.Key.ToString(); PythonMethodAttribute attribute = entry.Value; Logger.Log($"{target} -> {attribute.Function} ({attribute.File}) {(attribute.Generate ? "*" : "")}", LogLevel.Info); } Logger.LogHeading("Attributes - Properties", LogLevel.Info); foreach (var entry in projectAttributes.PropertyAttributes) { string target = entry.Key.ToString(); PythonPropertyAttribute attribute = entry.Value; Logger.Log($"{target} -> {attribute.Name} ({attribute.File})", LogLevel.Info); } Logger.LogHeading("Attributes - Operators", LogLevel.Info); foreach (var entry in projectAttributes.OperatorAttributes) { string target = entry.Key.ToString(); PythonOperatorAttribute attribute = entry.Value; Logger.Log($"{target} -> {attribute.Operator}", LogLevel.Info); } }
public async Task <Project> Generate(Project project) { AttributeWalker projectAttributes = new AttributeWalker(); await projectAttributes.WalkProject(project); LogAttributes(projectAttributes); PythonGraph pythonGraph = new PythonGraph(); CsPyMapper.MapAttributesToGraph(pythonGraph, projectAttributes); // foreach (var entryPoint in pythonMappings.PythonEntryPoints) // { // pythonGraph.AddPythonFile(entryPoint); // } // foreach (var mapping in pythonMappings.FieldMappings) // { // var fieldNameParts = mapping.Value.FieldName.Split("."); // var className = string.Join(".", fieldNameParts.SkipLast(1)); // var fieldName = fieldNameParts.Last(); // var pythonClass = pythonGraph.GetClass(mapping.Value.File, className); // var pythonFieldType = GetPythonType(pythonGraph, pythonMappings, mapping.Key.Type); // var pythonField = PythonField.Create(fieldName, pythonFieldType); // pythonClass.Children[fieldName] = pythonField; // } // foreach (var mapping in pythonMappings.MethodMappings) // { // var pythonFunction = pythonGraph.GetFunction(mapping.Value.File, mapping.Value.FunctionName); // MapFunctionTypes(pythonGraph, pythonMappings, pythonFunction, mapping.Key); // } LogGraph(pythonGraph); // // project = await ApplyRewriter(project, model => new ClassGeneratorRewriter(this, model, _pythonCache)); // project = await ApplyRewriter(project, model => new MethodGeneratorRewriter(this, model, pythonGraph, pythonMappings)); return(project); }
public static void MapAttributesToGraph(PythonGraph pythonGraph, AttributeWalker projectAttributes) { var typeDictionary = new CsPyMapperDictionary(); // Add core Python types AddCorePythonTypes(pythonGraph, typeDictionary); // Add custom Python types foreach (var entry in projectAttributes.ClassAttributes) { var target = entry.Key; var attribute = entry.Value; var pythonClass = pythonGraph.GetOrAddModule(attribute.ModuleName).GetOrAddClass(attribute.ClassName); typeDictionary[target] = pythonClass; } // Add fields foreach (var entry in projectAttributes.FieldAttributes) { var target = entry.Key; var attribute = entry.Value; var pythonClass = typeDictionary[target.ContainingType]; var fieldName = attribute.Name ?? target.Name; var fieldType = typeDictionary.GetPythonType(target.Type); var pythonField = PythonField.Create(fieldName, fieldType); pythonClass.Children.Add(fieldName, pythonField); } // Add methods foreach (var entry in projectAttributes.MethodAttributes) { var target = entry.Key; var attribute = entry.Value; MapPythonFunction(target, attribute.Function, typeDictionary); } // Add operators foreach (var entry in projectAttributes.OperatorAttributes) { var target = entry.Key; var attribute = entry.Value; MapPythonFunction(target, $"@operator.{attribute.Operator}", typeDictionary); } // Add properties foreach (var entry in projectAttributes.PropertyAttributes) { var target = entry.Key; var attribute = entry.Value; var pythonClass = typeDictionary[target.ContainingType]; var propertyName = attribute.Name ?? target.Name; var propertyType = typeDictionary.GetPythonType(target.Type); var pythonProperty = PythonProperty.Create(propertyName, propertyType); if (target.GetMethod != null) { var getterName = attribute.GetterFunction ?? propertyName; var parameters = new[] { new PythonParameter("self", new PythonType(pythonClass)) }; pythonProperty.GetterFunction = PythonFunction.Create(getterName, propertyType, parameters); } if (target.SetMethod != null) { var setterName = attribute.SetterFunction ?? propertyName; var parameters = new[] { new PythonParameter("self", new PythonType(pythonClass)), new PythonParameter("value", propertyType) }; pythonProperty.SetterFunction = PythonFunction.Create(setterName, PythonTypes.None, parameters); } pythonClass.Children.Add(propertyName, pythonProperty); } }