示例#1
0
        private static void AddCorePythonTypes(PythonGraph pythonGraph, CsPyMapperDictionary typeDictionary)
        {
            var coreModule = pythonGraph.GetOrAddModule("@python.core");

            typeDictionary[SpecialType.System_String]  = coreModule.GetOrAddClass("str");
            typeDictionary[SpecialType.System_Int32]   = coreModule.GetOrAddClass("int");
            typeDictionary[SpecialType.System_Double]  = coreModule.GetOrAddClass("float");
            typeDictionary[SpecialType.System_Boolean] = coreModule.GetOrAddClass("bool");
        }
示例#2
0
        public void LogGraph(PythonGraph graph)
        {
            Logger.LogHeading("Python Graph", LogLevel.Info);

            foreach (var entry in graph.Modules)
            {
                PythonModule module = entry.Value;
                LogNode(module, "");
            }
        }
示例#3
0
        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);
        }
示例#4
0
        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);
            }
        }