/// <summary>
        /// Generate Python client code for given ServiceClient.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(ServiceClient serviceClient)
        {
            var serviceClientTemplateModel = new ServiceClientTemplateModel(serviceClient);

            if (!string.IsNullOrWhiteSpace(Version))
            {
                serviceClientTemplateModel.Version = Version;
            }

            // Service client
            var setupTemplate = new SetupTemplate
            {
                Model = serviceClientTemplateModel
            };
            await Write(setupTemplate, "setup.py");

            var serviceClientInitTemplate = new ServiceClientInitTemplate
            {
                Model = serviceClientTemplateModel
            };
            await Write(serviceClientInitTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "__init__.py"));

            var serviceClientTemplate = new ServiceClientTemplate
            {
                Model = serviceClientTemplateModel,
            };
            await Write(serviceClientTemplate, Path.Combine(serviceClientTemplateModel.PackageName, serviceClientTemplateModel.Name.ToPythonCase() + ".py"));

            var versionTemplate = new VersionTemplate
            {
                Model = serviceClientTemplateModel,
            };
            await Write(versionTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "version.py"));

            var exceptionTemplate = new ExceptionTemplate
            {
                Model = serviceClientTemplateModel,
            };
            await Write(exceptionTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "exceptions.py"));

            var credentialTemplate = new CredentialTemplate
            {
                Model = serviceClientTemplateModel,
            };
            await Write(credentialTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "credentials.py"));

            //Models
            if (serviceClient.ModelTypes.Any())
            {
                var modelInitTemplate = new ModelInitTemplate
                {
                    Model = new ModelInitTemplateModel(serviceClient)
                };
                await Write(modelInitTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "models", "__init__.py"));

                foreach (var modelType in serviceClientTemplateModel.ModelTemplateModels)
                {
                    var modelTemplate = new ModelTemplate
                    {
                        Model = modelType
                    };
                    await Write(modelTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "models", modelType.Name.ToPythonCase() + ".py"));
                }
            }

            //MethodGroups
            if (serviceClientTemplateModel.MethodGroupModels.Any())
            {
                var methodGroupIndexTemplate = new MethodGroupInitTemplate
                {
                    Model = serviceClientTemplateModel
                };
                await Write(methodGroupIndexTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "operations", "__init__.py"));

                foreach (var methodGroupModel in serviceClientTemplateModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "operations", methodGroupModel.MethodGroupType.ToPythonCase() + ".py"));
                }
            }

            // Enums
            if (serviceClient.EnumTypes.Any())
            {
                var enumTemplate = new EnumTemplate
                {
                    Model = new EnumTemplateModel(serviceClient.EnumTypes),
                };
                await Write(enumTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "models", serviceClientTemplateModel.Name.ToPythonCase() + "_enums.py"));
            }
        }
示例#2
0
        /// <summary>
        /// Generate Python client code for given ServiceClient.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(CodeModel cm)
        {
            var codeModel = cm as CodeModelPy;

            if (codeModel == null)
            {
                throw new Exception("Code model is not a Python Code Model");
            }

            // Service client
            string[] namespaceParts = codeModel.Namespace.Split('.');
            for (int i = 1; i < namespaceParts.Length; ++i)
            {
                string initFolderName = Path.Combine(namespaceParts.Take(i).ToArray());
                await Write("__import__('pkg_resources').declare_namespace(__name__)",
                            Path.Combine(initFolderName, "__init__.py"), true);
            }

            var folderName    = codeModel.Name.ToPythonCase().ToLower();
            var setupTemplate = new SetupTemplate {
                Model = codeModel
            };

            await Write(setupTemplate, "setup.py");

            var serviceClientInitTemplate = new ServiceClientInitTemplate {
                Model = codeModel
            };

            await Write(serviceClientInitTemplate, Path.Combine(folderName, "__init__.py"));

            var serviceClientTemplate = new ServiceClientTemplate {
                Model = codeModel
            };

            await Write(serviceClientTemplate, Path.Combine(folderName, codeModel.Name.ToPythonCase() + ".py"));

            var versionTemplate = new VersionTemplate {
                Model = codeModel
            };

            await Write(versionTemplate, Path.Combine(folderName, "version.py"));

            var exceptionTemplate = new ExceptionTemplate {
                Model = codeModel
            };

            await Write(exceptionTemplate, Path.Combine(folderName, "exceptions.py"));

            var credentialTemplate = new CredentialTemplate {
                Model = codeModel
            };

            await Write(credentialTemplate, Path.Combine(folderName, "credentials.py"));

            //Models
            if (codeModel.ModelTypes.Any())
            {
                var modelInitTemplate = new ModelInitTemplate {
                    Model = codeModel
                };
                await Write(modelInitTemplate, Path.Combine(folderName, "models", "__init__.py"));

                foreach (var modelType in codeModel.ModelTemplateModels)
                {
                    var modelTemplate = new ModelTemplate
                    {
                        Model = modelType
                    };
                    await Write(modelTemplate, Path.Combine(folderName, "models", ((string)modelType.Name).ToPythonCase() + ".py"));
                }
            }

            //MethodGroups
            if (codeModel.MethodGroupModels.Any())
            {
                var methodGroupIndexTemplate = new MethodGroupInitTemplate
                {
                    Model = codeModel
                };
                await Write(methodGroupIndexTemplate, Path.Combine(folderName, "operations", "__init__.py"));

                foreach (var methodGroupModel in codeModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, Path.Combine(folderName, "operations", ((string)methodGroupModel.TypeName).ToPythonCase() + ".py"));
                }
            }

            // Enums
            if (codeModel.EnumTypes.Any())
            {
                var enumTemplate = new EnumTemplate {
                    Model = codeModel.EnumTypes
                };
                await Write(enumTemplate, Path.Combine(folderName, "models", codeModel.Name.ToPythonCase() + "_enums.py"));
            }
        }
示例#3
0
        /// <summary>
        /// Generate Python client code for given ServiceClient.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(ServiceClient serviceClient)
        {
            var serviceClientTemplateModel = new ServiceClientTemplateModel(serviceClient);

            if (!string.IsNullOrWhiteSpace(Version))
            {
                serviceClientTemplateModel.Version = Version;
            }

            // Service client
            var setupTemplate = new SetupTemplate
            {
                Model = serviceClientTemplateModel
            };

            await Write(setupTemplate, "setup.py");

            var serviceClientInitTemplate = new ServiceClientInitTemplate
            {
                Model = serviceClientTemplateModel
            };

            await Write(serviceClientInitTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "__init__.py"));

            var serviceClientTemplate = new ServiceClientTemplate
            {
                Model = serviceClientTemplateModel,
            };

            await Write(serviceClientTemplate, Path.Combine(serviceClientTemplateModel.PackageName, serviceClientTemplateModel.Name.ToPythonCase() + ".py"));

            var versionTemplate = new VersionTemplate
            {
                Model = serviceClientTemplateModel,
            };

            await Write(versionTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "version.py"));

            var exceptionTemplate = new ExceptionTemplate
            {
                Model = serviceClientTemplateModel,
            };

            await Write(exceptionTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "exceptions.py"));

            var credentialTemplate = new CredentialTemplate
            {
                Model = serviceClientTemplateModel,
            };

            await Write(credentialTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "credentials.py"));

            //Models
            if (serviceClient.ModelTypes.Any())
            {
                var modelInitTemplate = new ModelInitTemplate
                {
                    Model = new ModelInitTemplateModel(serviceClient)
                };
                await Write(modelInitTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "models", "__init__.py"));

                foreach (var modelType in serviceClientTemplateModel.ModelTemplateModels)
                {
                    var modelTemplate = new ModelTemplate
                    {
                        Model = modelType
                    };
                    await Write(modelTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "models", modelType.Name.ToPythonCase() + ".py"));
                }
            }

            //MethodGroups
            if (serviceClientTemplateModel.MethodGroupModels.Any())
            {
                var methodGroupIndexTemplate = new MethodGroupInitTemplate
                {
                    Model = serviceClientTemplateModel
                };
                await Write(methodGroupIndexTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "operations", "__init__.py"));

                foreach (var methodGroupModel in serviceClientTemplateModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "operations", methodGroupModel.MethodGroupType.ToPythonCase() + ".py"));
                }
            }

            // Enums
            if (serviceClient.EnumTypes.Any())
            {
                var enumTemplate = new EnumTemplate
                {
                    Model = new EnumTemplateModel(serviceClient.EnumTypes),
                };
                await Write(enumTemplate, Path.Combine(serviceClientTemplateModel.PackageName, "models", serviceClientTemplateModel.Name.ToPythonCase() + "_enums.py"));
            }
        }
示例#4
0
        /// <summary>
        /// Generate Python client code for given ServiceClient.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(CodeModel cm)
        {
            var codeModel = cm as CodeModelPy;

            if (codeModel == null)
            {
                throw new Exception("Code model is not a Python Code Model");
            }

            writeNamespaceFolders(codeModel);

            if (codeModel.BasicSetupPy)
            {
                var setupTemplate = new SetupTemplate {
                    Model = codeModel
                };
                await Write(setupTemplate, "setup.py");
            }

            var noAsync = await Settings.Instance.Host.GetValue <bool?>("no-async");

            var folderName = codeModel.NoNamespaceFolders?"":Path.Combine(codeModel.Namespace.Split('.'));
            var serviceClientInitTemplate = new ServiceClientInitTemplate {
                Model = codeModel
            };

            await Write(serviceClientInitTemplate, Path.Combine(folderName, "__init__.py"));

            var configurationTemplate = new ConfigurationTemplate {
                Model = codeModel
            };

            await Write(configurationTemplate, Path.Combine(folderName, "_configuration.py"));

            // Writing service client
            var serviceClientTemplate = new ServiceClientTemplate {
                Model = codeModel
            };

            await Write(serviceClientTemplate, Path.Combine(folderName, "_" + codeModel.Name.ToPythonCase() + ".py"));

            if (codeModel.MethodTemplateModels.Any(each => each.MethodGroup.IsCodeModelMethodGroup))
            {
                var serviceClientOpTemplate = new ServiceClientOperationsTemplate {
                    Model = codeModel
                };
                await Write(serviceClientOpTemplate, Path.Combine(folderName, "operations", "_" + codeModel.Name.ToPythonCase() + "_operations.py"));
            }

            // do we need to write out the version template file?
            var versionPath = Path.Combine(folderName, "version.py");

            // protect the version file
            await Settings.Instance.Host.ProtectFiles(versionPath);

            if (true != await Settings.Instance.Host.GetValue <bool?>("keep-version-file") || string.IsNullOrEmpty(await Settings.Instance.Host.ReadFile(versionPath)))
            {
                var versionTemplate = new VersionTemplate {
                    Model = codeModel
                };
                // if they didn't say to keep the old file (or it was not there/empty), write it out.
                await Write(versionTemplate, versionPath);
            }

            //Models
            if (codeModel.ModelTypes.Any())
            {
                var modelInitTemplate = new ModelInitTemplate {
                    Model = codeModel
                };
                await Write(modelInitTemplate, Path.Combine(folderName, "models", "__init__.py"));

                var modelTemplate = new ModelTemplate {
                    Model = codeModel.getSortedModels()
                };
                await Write(modelTemplate, Path.Combine(folderName, "models", "_models.py"));

                modelTemplate.Python3Mode = true;
                await Write(modelTemplate, Path.Combine(folderName, "models", "_models_py3.py"));
            }

            //MethodGroups
            if (codeModel.MethodGroupModels.Any() || codeModel.MethodTemplateModels.Any(each => each.MethodGroup.IsCodeModelMethodGroup))
            {
                var methodGroupIndexTemplate = new MethodGroupInitTemplate
                {
                    Model = codeModel
                };
                await Write(methodGroupIndexTemplate, Path.Combine(folderName, "operations", "__init__.py"));

                foreach (var methodGroupModel in codeModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, Path.Combine(folderName, "operations", "_" + ((string)methodGroupModel.TypeName).ToPythonCase() + ".py"));
                }
            }

            // Enums
            if (codeModel.EnumTypes.Any())
            {
                var enumTemplate = new EnumTemplate {
                    Model = codeModel.EnumTypes
                };
                await Write(enumTemplate, Path.Combine(folderName, "models", "_" + codeModel.Name.ToPythonCase() + "_enums.py"));
            }

            // Async
            if (noAsync != true)
            {
                var serviceClientInitTemplateAsync = new ServiceClientInitTemplateAsync {
                    Model = codeModel
                };
                await Write(serviceClientInitTemplateAsync, Path.Combine(folderName, "aio", "__init__.py"));

                // Writing AIO service client
                var serviceClientAsyncTemplate = new ServiceClientTemplateAsync {
                    Model = codeModel
                };
                await Write(serviceClientAsyncTemplate, Path.Combine(folderName, "aio", "_" + codeModel.Name.ToPythonCase() + "_async.py"));

                if (codeModel.MethodTemplateModels.Any(each => each.MethodGroup.IsCodeModelMethodGroup))
                {
                    var serviceClientOpTemplateAsync = new ServiceClientOperationsTemplateAsync {
                        Model = codeModel
                    };
                    await Write(serviceClientOpTemplateAsync, Path.Combine(folderName, "aio", "operations_async", "_" + codeModel.Name.ToPythonCase() + "_operations_async.py"));
                }

                //MethodGroups
                if (codeModel.MethodGroupModels.Any() || codeModel.MethodTemplateModels.Any(each => each.MethodGroup.IsCodeModelMethodGroup))
                {
                    var methodGroupIndexTemplateAsync = new MethodGroupInitTemplate
                    {
                        Model     = codeModel,
                        AsyncMode = true
                    };
                    await Write(methodGroupIndexTemplateAsync, Path.Combine(folderName, "aio", "operations_async", "__init__.py"));

                    foreach (var methodGroupModel in codeModel.MethodGroupModels)
                    {
                        // Build a Py3 version that import the Py2 one
                        var methodGroupTemplatePy3 = new MethodGroupTemplateAsync
                        {
                            Model = methodGroupModel
                        };
                        await Write(methodGroupTemplatePy3, Path.Combine(folderName, "aio", "operations_async", "_" + ((string)methodGroupModel.TypeName).ToPythonCase() + "_async.py"));
                    }
                }
            }
        }
示例#5
0
        /// <summary>
        /// Generate Python client code for given ServiceClient.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(CodeModel cm)
        {
            var codeModel = cm as CodeModelPy;

            if (codeModel == null)
            {
                throw new Exception("Code model is not a Python Code Model");
            }

            // Service client
            var setupTemplate = new SetupTemplate {
                Model = codeModel
            };

            await Write(setupTemplate, "setup.py");

            var serviceClientInitTemplate = new ServiceClientInitTemplate {
                Model = codeModel
            };

            await Write(serviceClientInitTemplate, Path.Combine(codeModel.PackageName, "__init__.py"));

            var serviceClientTemplate = new ServiceClientTemplate {
                Model = codeModel
            };

            await Write(serviceClientTemplate, Path.Combine(codeModel.PackageName, codeModel.Name.ToPythonCase() + ".py"));

            var versionTemplate = new VersionTemplate {
                Model = codeModel
            };

            await Write(versionTemplate, Path.Combine(codeModel.PackageName, "version.py"));

            var exceptionTemplate = new ExceptionTemplate {
                Model = codeModel
            };

            await Write(exceptionTemplate, Path.Combine(codeModel.PackageName, "exceptions.py"));

            var credentialTemplate = new CredentialTemplate {
                Model = codeModel
            };

            await Write(credentialTemplate, Path.Combine(codeModel.PackageName, "credentials.py"));

            //Models
            if (codeModel.ModelTypes.Any())
            {
                var modelInitTemplate = new ModelInitTemplate {
                    Model = codeModel
                };
                await Write(modelInitTemplate, Path.Combine(codeModel.PackageName, "models", "__init__.py"));

                foreach (var modelType in codeModel.ModelTemplateModels)
                {
                    var modelTemplate = new ModelTemplate
                    {
                        Model = modelType
                    };
                    await Write(modelTemplate, Path.Combine(codeModel.PackageName, "models", ((string)modelType.Name).ToPythonCase() + ".py"));
                }
            }

            //MethodGroups
            if (codeModel.MethodGroupModels.Any())
            {
                var methodGroupIndexTemplate = new MethodGroupInitTemplate
                {
                    Model = codeModel
                };
                await Write(methodGroupIndexTemplate, Path.Combine(codeModel.PackageName, "operations", "__init__.py"));

                foreach (var methodGroupModel in codeModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, Path.Combine(codeModel.PackageName, "operations", ((string)methodGroupModel.TypeName).ToPythonCase() + ".py"));
                }
            }

            // Enums
            if (codeModel.EnumTypes.Any())
            {
                var enumTemplate = new EnumTemplate {
                    Model = codeModel.EnumTypes
                };
                await Write(enumTemplate, Path.Combine(codeModel.PackageName, "models", codeModel.Name.ToPythonCase() + "_enums.py"));
            }
        }
示例#6
0
        /// <summary>
        /// Generate Python client code for given ServiceClient.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(CodeModel cm)
        {
            var codeModel = cm as CodeModelPy;
            if (codeModel == null)
            {
                throw new Exception("Code model is not a Python Code Model");
            }

            // Service client
            var setupTemplate = new SetupTemplate { Model = codeModel };
            await Write(setupTemplate, "setup.py");

            var serviceClientInitTemplate = new ServiceClientInitTemplate { Model = codeModel };
            await Write(serviceClientInitTemplate, Path.Combine(codeModel.PackageName, "__init__.py"));

            var serviceClientTemplate = new ServiceClientTemplate { Model = codeModel };
            await Write(serviceClientTemplate, Path.Combine(codeModel.PackageName, codeModel.Name.ToPythonCase() + ".py"));

            var versionTemplate = new VersionTemplate { Model = codeModel };
            await Write(versionTemplate, Path.Combine(codeModel.PackageName, "version.py"));

            var exceptionTemplate = new ExceptionTemplate { Model = codeModel };
            await Write(exceptionTemplate, Path.Combine(codeModel.PackageName, "exceptions.py"));

            var credentialTemplate = new CredentialTemplate { Model = codeModel };
            await Write(credentialTemplate, Path.Combine(codeModel.PackageName, "credentials.py"));

            //Models
            if (codeModel.ModelTypes.Any())
            {
                var modelInitTemplate = new ModelInitTemplate { Model = codeModel };
                await Write(modelInitTemplate, Path.Combine(codeModel.PackageName, "models", "__init__.py"));

                foreach (var modelType in codeModel.ModelTemplateModels)
                {
                    var modelTemplate = new ModelTemplate
                    {
                        Model = modelType
                    };
                    await Write(modelTemplate, Path.Combine(codeModel.PackageName, "models", ((string)modelType.Name).ToPythonCase() + ".py"));
                }
            }

            //MethodGroups
            if (codeModel.MethodGroupModels.Any())
            {
                var methodGroupIndexTemplate = new MethodGroupInitTemplate
                {
                    Model = codeModel
                };
                await Write(methodGroupIndexTemplate, Path.Combine(codeModel.PackageName, "operations", "__init__.py"));

                foreach (var methodGroupModel in codeModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, Path.Combine(codeModel.PackageName, "operations", ((string) methodGroupModel.TypeName).ToPythonCase() + ".py"));
                }
            }

            // Enums
            if (codeModel.EnumTypes.Any())
            {
                var enumTemplate = new EnumTemplate { Model = codeModel.EnumTypes };
                await Write(enumTemplate, Path.Combine(codeModel.PackageName, "models", codeModel.Name.ToPythonCase() + "_enums.py"));
            }
        }
示例#7
0
        /// <summary>
        /// Generate Python client code for given ServiceClient.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(CodeModel cm)
        {
            var codeModel = cm as CodeModelPy;

            if (codeModel == null)
            {
                throw new Exception("Code model is not a Python Code Model");
            }

            writeNamespaceFolders(codeModel);

            if (codeModel.BasicSetupPy)
            {
                var setupTemplate = new SetupTemplate {
                    Model = codeModel
                };
                await Write(setupTemplate, "setup.py");
            }

            var folderName = codeModel.NoNamespaceFolders?"":Path.Combine(codeModel.Namespace.Split('.'));
            var serviceClientInitTemplate = new ServiceClientInitTemplate {
                Model = codeModel
            };

            await Write(serviceClientInitTemplate, Path.Combine(folderName, "__init__.py"));

            var serviceClientTemplate = new ServiceClientTemplate {
                Model = codeModel
            };

            await Write(serviceClientTemplate, Path.Combine(folderName, codeModel.Name.ToPythonCase() + ".py"));

            // do we need to write out the version template file?
            var versionPath = Path.Combine(folderName, "version.py");

            // protect the version file
            await Settings.Instance.Host.ProtectFiles(versionPath);

            if (true != await Settings.Instance.Host.GetValue <bool?>("keep-version-file") || string.IsNullOrEmpty(await Settings.Instance.Host.ReadFile(versionPath)))
            {
                var versionTemplate = new VersionTemplate {
                    Model = codeModel
                };
                // if they didn't say to keep the old file (or it was not there/empty), write it out.
                await Write(versionTemplate, versionPath);
            }

            //Models
            if (codeModel.ModelTypes.Any())
            {
                var modelInitTemplate = new ModelInitTemplate {
                    Model = codeModel
                };
                await Write(modelInitTemplate, Path.Combine(folderName, "models", "__init__.py"));

                foreach (var modelType in codeModel.ModelTemplateModels)
                {
                    var modelTemplate = new ModelTemplate
                    {
                        Model = modelType
                    };
                    await Write(modelTemplate, Path.Combine(folderName, "models", ((string)modelType.Name).ToPythonCase() + ".py"));

                    // Rebuild the same in Python 3 mode
                    modelTemplate.Python3Mode = true;
                    await Write(modelTemplate, Path.Combine(folderName, "models", ((string)modelType.Name).ToPythonCase() + "_py3.py"));
                }
            }

            //MethodGroups
            if (codeModel.MethodGroupModels.Any())
            {
                var methodGroupIndexTemplate = new MethodGroupInitTemplate
                {
                    Model = codeModel
                };
                await Write(methodGroupIndexTemplate, Path.Combine(folderName, "operations", "__init__.py"));

                foreach (var methodGroupModel in codeModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, Path.Combine(folderName, "operations", ((string)methodGroupModel.TypeName).ToPythonCase() + ".py"));
                }
            }

            // Enums
            if (codeModel.EnumTypes.Any())
            {
                var enumTemplate = new EnumTemplate {
                    Model = codeModel.EnumTypes
                };
                await Write(enumTemplate, Path.Combine(folderName, "models", codeModel.Name.ToPythonCase() + "_enums.py"));
            }
        }
示例#8
0
        /// <summary>
        /// Generate Python client code for given ServiceClient.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(CodeModel cm)
        {
            var codeModel = cm as CodeModelPy;

            if (codeModel == null)
            {
                throw new Exception("Code model is not a Python Code Model");
            }

            writeNamespaceFolders(codeModel);

            if (codeModel.BasicSetupPy)
            {
                var setupTemplate = new SetupTemplate {
                    Model = codeModel
                };
                await Write(setupTemplate, "setup.py");
            }

            var folderName = codeModel.NoNamespaceFolders?"":Path.Combine(codeModel.Namespace.Split('.'));
            var serviceClientInitTemplate = new ServiceClientInitTemplate {
                Model = codeModel
            };

            await Write(serviceClientInitTemplate, Path.Combine(folderName, "__init__.py"));

            var serviceClientTemplate = new ServiceClientTemplate {
                Model = codeModel
            };

            await Write(serviceClientTemplate, Path.Combine(folderName, codeModel.Name.ToPythonCase() + ".py"));

            var versionTemplate = new VersionTemplate {
                Model = codeModel
            };

            await Write(versionTemplate, Path.Combine(folderName, "version.py"));

            //Models
            if (codeModel.ModelTypes.Any())
            {
                var modelInitTemplate = new ModelInitTemplate {
                    Model = codeModel
                };
                await Write(modelInitTemplate, Path.Combine(folderName, "models", "__init__.py"));

                foreach (var modelType in codeModel.ModelTemplateModels)
                {
                    var modelTemplate = new ModelTemplate
                    {
                        Model = modelType
                    };
                    await Write(modelTemplate, Path.Combine(folderName, "models", ((string)modelType.Name).ToPythonCase() + ".py"));

                    // Rebuild the same in Python 3 mode
                    modelTemplate.Python3Mode = true;
                    await Write(modelTemplate, Path.Combine(folderName, "models", ((string)modelType.Name).ToPythonCase() + "_py3.py"));
                }
            }

            //MethodGroups
            if (codeModel.MethodGroupModels.Any())
            {
                var methodGroupIndexTemplate = new MethodGroupInitTemplate
                {
                    Model = codeModel
                };
                await Write(methodGroupIndexTemplate, Path.Combine(folderName, "operations", "__init__.py"));

                foreach (var methodGroupModel in codeModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, Path.Combine(folderName, "operations", ((string)methodGroupModel.TypeName).ToPythonCase() + ".py"));
                }
            }

            // Enums
            if (codeModel.EnumTypes.Any())
            {
                var enumTemplate = new EnumTemplate {
                    Model = codeModel.EnumTypes
                };
                await Write(enumTemplate, Path.Combine(folderName, "models", codeModel.Name.ToPythonCase() + "_enums.py"));
            }
        }