Skip to content

Commit

Permalink
Merge pull request #1200 from microsoft/samwelkanda/codegen-python
Browse files Browse the repository at this point in the history
Samwelkanda/codegen python
  • Loading branch information
samwelkanda authored Aug 18, 2022
2 parents 889476c + e0e0641 commit 5022d30
Show file tree
Hide file tree
Showing 39 changed files with 2,786 additions and 48 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": "Launch Python",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net6.0/kiota.dll",
"args": ["--openapi",
"https://raw.githubusercontent.com/microsoftgraph/msgraph-sdk-powershell/dev/openApiDocs/v1.0/Mail.yml",
"--language",
"python",
"-o",
"${workspaceFolder}/samples/msgraph-mail/python",
"-n",
"GraphPythonv1" ],
"cwd": "${workspaceFolder}/src/kiota",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for no-content responses in python abstractions and http packages. [#1630](https://github.com/microsoft/kiota/issues/1459)
- Added support for vendor-specific content types in python. [#1631](https://github.com/microsoft/kiota/issues/1463)
- Simplified field deserializers for json in Python. [#1632](https://github.com/microsoft/kiota/issues/1492)
- Adds python code generation support. [#1200](https://github.com/microsoft/kiota/issues/163)
- Added native type support for Duration, Time Only, and Date Only in Ruby. [#1644](https://github.com/microsoft/kiota/issues/1644)
- Added a `--additional-data` argument to generate the AdditionalData properties [#1772](https://github.com/microsoft/kiota/issues/1772)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The following table provides an overview of the languages supported by Kiota and
| Go | [](https://github.com/microsoft/kiota/projects/8) | [](https://github.com/microsoft/kiota-abstractions-go) | [JSON](https://github.com/microsoft/kiota-serialization-json-go), [TEXT](https://github.com/microsoft/kiota-serialization-text-go) | [Anonymous](https://github.com/microsoft/kiota-abstractions-go/blob/main/authentication/anonymous_authentication_provider.go), [Azure](https://github.com/microsoft/kiota-authentication-azure-go/) | [](https://github.com/microsoft/kiota-http-go/) | [link](https://microsoft.github.io/kiota/get-started/go) |
| Java | [](https://github.com/microsoft/kiota/projects/7) | [](https://github.com/microsoft/kiota-java/tree/main/components/abstractions) | [JSON](https://github.com/microsoft/kiota-java/tree/main/components/serialization/json), [TEXT](https://github.com/microsoft/kiota-java/tree/main/components/serialization/text) | [Anonymous](https://github.com/microsoft/kiota-java/blob/main/components/abstractions/src/main/java/com/microsoft/kiota/authentication/AnonymousAuthenticationProvider.java), [Azure](https://github.com/microsoft/kiota-java/tree/main/components/authentication/azure) | [](https://github.com/microsoft/kiota-java/tree/main/components/http/okHttp) | [link](https://microsoft.github.io/kiota/get-started/java) |
| PHP | [](https://github.com/microsoft/kiota/projects/4) | [](https://github.com/microsoft/kiota-abstractions-php) | [JSON](https://github.com/microsoft/kiota-serialization-json-php), [TEXT](https://github.com/microsoft/kiota-serialization-text-php) | [Anonymous](https://github.com/microsoft/kiota-abstractions-php/blob/main/src/Authentication/AnonymousAuthenticationProvider.php), [✔️ PHP League](https://github.com/microsoft/kiota-authentication-phpleague-php) | [](https://github.com/microsoft/kiota-http-guzzle-php) | [link](https://microsoft.github.io/kiota/get-started/php) |
| Python | [](https://github.com/microsoft/kiota/projects/3) | [](./abstractions/python) | [JSON](./serialization/python/json), [❌ TEXT](https://github.com/microsoft/kiota/issues/1406) | [Anonymous](./abstractions/python/kiota/abstractions/authentication/anonymous_authentication_provider.py), [Azure](./authentication/python/azure) | [](./http/python/requests) | |
| Python | [](https://github.com/microsoft/kiota/projects/3) | [](./abstractions/python) | [JSON](./serialization/python/json), [❌ TEXT](https://github.com/microsoft/kiota/issues/1406) | [Anonymous](./abstractions/python/kiota/abstractions/authentication/anonymous_authentication_provider.py), [Azure](./authentication/python/azure) | [](./http/python/requests) | |
| Ruby | [](https://github.com/microsoft/kiota/projects/6) | [](./abstractions/ruby) | [JSON](./serialization/ruby/json/microsoft_kiota_serialization), [❌ TEXT](https://github.com/microsoft/kiota/issues/1049) | [Anonymous](./abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/authentication/anonymous_authentication_provider.rb), [❌ Azure](https://github.com/microsoft/kiota/issues/421) | [](./http/ruby/nethttp/microsoft_kiota_nethttplibrary)| [link](https://microsoft.github.io/kiota/get-started/ruby) |
| TypeScript/JavaScript | [](https://github.com/microsoft/kiota/projects/2) | [](https://github.com/microsoft/kiota-typescript/tree/main/packages/abstractions) | [JSON](https://github.com/microsoft/kiota-typescript/tree/main/packages/serialization/json), [TEXT](https://github.com/microsoft/kiota-typescript/tree/main/packages/serialization/text) | [Anonymous](https://github.com/microsoft/kiota-typescript/blob/main/packages/abstractions/src/authentication/anonymousAuthenticationProvider.ts), [Azure](https://github.com/microsoft/kiota-typescript/tree/main/packages/authentication/azure) | [](https://github.com/microsoft/kiota-typescript/tree/main/packages/http/fetch) | [link](https://microsoft.github.io/kiota/get-started/typescript) |
| Shell | [](https://github.com/microsoft/kiota/projects/10) | [](./abstractions/dotnet), [](./cli/commonc) | [JSON](./serialization/dotnet/json), [TEXT](./serialization/dotnet/text) | [Anonymous](./abstractions/dotnet/src/authentication/AnonymousAuthenticationProvider.cs), [Azure](./authentication/dotnet/azure) | [](./http/dotnet/httpclient) | [link](https://microsoft.github.io/kiota/get-started/dotnet) |
Expand Down
17 changes: 17 additions & 0 deletions src/Kiota.Builder/PathSegmenters/PythonPathSegmenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Kiota.Builder.Extensions;

namespace Kiota.Builder;
public class PythonPathSegmenter : CommonPathSegmenter
{
public PythonPathSegmenter(string rootPath, string clientNamespaceName) : base(rootPath, clientNamespaceName) { }
public override string FileSuffix => ".py";
public override string NormalizeFileName(CodeElement currentElement) {
return currentElement switch
{
CodeNamespace => "__init__",
_ => GetDefaultFileName(currentElement)
};
}
private static string GetDefaultFileName(CodeElement currentElement) => GetLastFileNameSegment(currentElement).ToSnakeCase();
public override string NormalizeNamespaceSegment(string segmentName) => segmentName.ToSnakeCase();
}
31 changes: 20 additions & 11 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ internal void AddInnerClasses(CodeElement current, bool prefixClassNameWithParen
private static readonly CodeUsingComparer usingComparerWithoutDeclarations = new(false);
protected readonly GenerationConfiguration _configuration;

protected static void AddPropertiesAndMethodTypesImports(CodeElement current, bool includeParentNamespaces, bool includeCurrentNamespace, bool compareOnDeclaration) {
protected static void AddPropertiesAndMethodTypesImports(CodeElement current, bool includeParentNamespaces, bool includeCurrentNamespace, bool compareOnDeclaration, Func<IEnumerable<CodeTypeBase>, IEnumerable<CodeTypeBase>> codeTypeFilter = default) {
if(current is CodeClass currentClass &&
currentClass.StartBlock is ClassDeclaration currentClassDeclaration) {
var currentClassNamespace = currentClass.GetImmediateParentOfType<CodeNamespace>();
Expand Down Expand Up @@ -503,24 +503,33 @@ protected static void AddPropertiesAndMethodTypesImports(CodeElement current, bo
.SelectMany(static x => x.ErrorMappings)
.Select(static x => x.Value)
.Distinct();
var usingsToAdd = propertiesTypes
var typesCollection = propertiesTypes
.Union(methodsParametersTypes)
.Union(methodsReturnTypes)
.Union(indexerTypes)
.Union(inheritTypes)
.Union(errorTypes)
.Where(static x => x != null)
.SelectMany(static x => x?.AllTypes?.Select(static y => (type: y, ns: y?.TypeDefinition?.GetImmediateParentOfType<CodeNamespace>())))
.Where(x => x.ns != null && (includeCurrentNamespace || x.ns != currentClassNamespace))
.Where(x => includeParentNamespaces || !currentClassNamespace.IsChildOf(x.ns))
.Select(static x => new CodeUsing { Name = x.ns.Name, Declaration = x.type })
.Where(x => x.Declaration?.TypeDefinition != current)
.Distinct(compareOnDeclaration ? usingComparerWithDeclarations : usingComparerWithoutDeclarations)
.ToArray();
.Where(static x => x != null);

if (codeTypeFilter != default)
{
typesCollection = codeTypeFilter?.Invoke(typesCollection);
}

var usingsToAdd = typesCollection.Where(static x => x != null)
.SelectMany(static x => x?.AllTypes?.Select(static y => (type: y, ns: y?.TypeDefinition?.GetImmediateParentOfType<CodeNamespace>())))
.Where(x => x.ns != null && (includeCurrentNamespace || x.ns != currentClassNamespace))
.Where(x => includeParentNamespaces || !currentClassNamespace.IsChildOf(x.ns))
.Select(static x => new CodeUsing { Name = x.ns.Name, Declaration = x.type })
.Where(x => x.Declaration?.TypeDefinition != current)
.Distinct(compareOnDeclaration ? usingComparerWithDeclarations : usingComparerWithoutDeclarations)
.ToArray();


if(usingsToAdd.Any())
(currentClass.Parent is CodeClass parentClass ? parentClass : currentClass).AddUsing(usingsToAdd); //lots of languages do not support imports on nested classes
}
CrawlTree(current, (x) => AddPropertiesAndMethodTypesImports(x, includeParentNamespaces, includeCurrentNamespace, compareOnDeclaration));
CrawlTree(current, (x) => AddPropertiesAndMethodTypesImports(x, includeParentNamespaces, includeCurrentNamespace, compareOnDeclaration, codeTypeFilter));
}
protected static void CrawlTree(CodeElement currentElement, Action<CodeElement> function) {
foreach(var childElement in currentElement.GetChildElements())
Expand Down
3 changes: 3 additions & 0 deletions src/Kiota.Builder/Refiners/ILanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public static void Refine(GenerationConfiguration config, CodeNamespace generate
case GenerationLanguage.Swift:
new SwiftRefiner(config).Refine(generatedCode);
break;
case GenerationLanguage.Python:
new PythonRefiner(config).Refine(generatedCode);
break;
default:
break; //Do nothing
}
Expand Down
Loading

0 comments on commit 5022d30

Please sign in to comment.