pulumi/pkg/codegen/python/utilities.go
CyrusNajmabadi 66bd3f4aa8
Breaking changes due to Feature 2.0 work
* Make `async:true` the default for `invoke` calls (#3750)

* Switch away from native grpc impl. (#3728)

* Remove usage of the 'deasync' library from @pulumi/pulumi. (#3752)

* Only retry as long as we get unavailable back.  Anything else continues. (#3769)

* Handle all errors for now. (#3781)


* Do not assume --yes was present when using pulumi in non-interactive mode (#3793)

* Upgrade all paths for sdk and pkg to v2

* Backport C# invoke classes and other recent gen changes (#4288)

Adjust C# generation

* Replace IDeployment with a sealed class (#4318)

Replace IDeployment with a sealed class

* .NET: default to args subtype rather than Args.Empty (#4320)

* Adding system namespace for Dotnet code gen

This is required for using Obsolute attributes for deprecations

```
Iam/InstanceProfile.cs(142,10): error CS0246: The type or namespace name 'ObsoleteAttribute' could not be found (are you missing a using directive or an assembly reference?) [/Users/stack72/code/go/src/github.com/pulumi/pulumi-aws/sdk/dotnet/Pulumi.Aws.csproj]
Iam/InstanceProfile.cs(142,10): error CS0246: The type or namespace name 'Obsolete' could not be found (are you missing a using directive or an assembly reference?) [/Users/stack72/code/go/src/github.com/pulumi/pulumi-aws/sdk/dotnet/Pulumi.Aws.csproj]
```

* Fix the nullability of config type properties in C# codegen (#4379)
2020-04-14 09:30:25 +01:00

75 lines
2.6 KiB
Go

package python
import (
"io"
"strings"
"unicode"
"github.com/pulumi/pulumi/pkg/v2/codegen"
)
// pythonKeywords is a map of reserved keywords used by python 2 and 3. We use this to avoid generating unspeakable
// names in the resulting code. This map was sourced by merging the following reference material:
//
// * Python 2: https://docs.python.org/2.5/ref/keywords.html
// * Python 3: https://docs.python.org/3/reference/lexical_analysis.html#keywords
//
var pythonKeywords = codegen.NewStringSet("false", "none", "true", "and", "as", "assert", "async", "await", "break",
"class", "continue", "def", "del", "elif", "else", "except", "exec", "finally", "for", "from", "global", "if",
"import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "print", "raise", "return", "try", "while",
"with", "yield")
func isReservedWord(word string) bool {
return pythonKeywords.Has(word)
}
// isLegalIdentifierStart returns true if it is legal for c to be the first character of a Python identifier as per
// https://docs.python.org/3.7/reference/lexical_analysis.html#identifiers.
func isLegalIdentifierStart(c rune) bool {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_' ||
unicode.In(c, unicode.Lu, unicode.Ll, unicode.Lt, unicode.Lm, unicode.Lo, unicode.Nl)
}
// isLegalIdentifierPart returns true if it is legal for c to be part of a Python identifier (besides the first
// character) as per https://docs.python.org/3.7/reference/lexical_analysis.html#identifiers.
func isLegalIdentifierPart(c rune) bool {
return isLegalIdentifierStart(c) || c >= '0' && c <= '9' ||
unicode.In(c, unicode.Lu, unicode.Ll, unicode.Lt, unicode.Lm, unicode.Lo, unicode.Nl, unicode.Mn, unicode.Mc,
unicode.Nd, unicode.Pc)
}
// isLegalIdentifier returns true if s is a legal Python identifier as per
// https://docs.python.org/3.7/reference/lexical_analysis.html#identifiers.
func isLegalIdentifier(s string) bool {
reader := strings.NewReader(s)
c, _, _ := reader.ReadRune()
if !isLegalIdentifierStart(c) {
return false
}
for {
c, _, err := reader.ReadRune()
if err != nil {
return err == io.EOF
}
if !isLegalIdentifierPart(c) {
return false
}
}
}
// cleanName replaces characters that are not allowed in Python identifiers with underscores. No attempt is made to
// ensure that the result is unique.
func cleanName(name string) string {
var builder strings.Builder
for i, c := range name {
if !isLegalIdentifierPart(c) {
builder.WriteRune('_')
} else {
if i == 0 && !isLegalIdentifierStart(c) {
builder.WriteRune('_')
}
builder.WriteRune(c)
}
}
return builder.String()
}