Skip to content

Commit

Permalink
Add async Python 3.7 reserved keyword
Browse files Browse the repository at this point in the history
This patch add support for async reserved keyword, which is new in
Python 3.7, so now in Python 3.7 users should use async_ instead.
But we still support to pass async, parameter for old Python
versions.

Change-Id: I847b30c5ce961895edef5725b4cf1ae3421d36c0
Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1622043
Signed-off-by: Ondra Machacek <omachace@redhat.com>
  • Loading branch information
machacekondra committed Oct 9, 2018
1 parent dd60293 commit 7f75f47
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ private void init() {
words.add("and");
words.add("as");
words.add("assert");
words.add("async");
words.add("await");
words.add("break");
words.add("class");
words.add("continue");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ private void generateAddHttpPost(Method method) {
buffer.addLine("headers=None,");
buffer.addLine("query=None,");
buffer.addLine("wait=True,");
buffer.addLine("**kwargs");
buffer.endBlock();
buffer.addLine("):");
buffer.startBlock();
Expand All @@ -207,6 +208,8 @@ private void generateAddHttpPost(Method method) {
buffer.addLine("])");
buffer.addLine();

proccessKwargs(secondaryParameters);

// Generate the code to build the URL query:
buffer.addLine("# Build the URL:");
buffer.addLine("query = query or {}");
Expand Down Expand Up @@ -238,6 +241,7 @@ private void generateActionHttpPost(Method method) {
buffer.addLine("headers=None,");
buffer.addLine("query=None,");
buffer.addLine("wait=True,");
buffer.addLine("**kwargs");
buffer.endBlock();
buffer.addLine("):");

Expand All @@ -254,6 +258,9 @@ private void generateActionHttpPost(Method method) {
buffer.addLine("])");
buffer.addLine();

// Process kwargs parameters:
proccessKwargs(inParameters);

// Generate the code to populate the action:
buffer.addLine("# Populate the action:");
buffer.addLine("action = types.Action(");
Expand Down Expand Up @@ -306,6 +313,7 @@ private void generateHttpGet(Method method) {
buffer.addLine("headers=None,");
buffer.addLine("query=None,");
buffer.addLine("wait=True,");
buffer.addLine("**kwargs");
buffer.endBlock();
buffer.addLine("):");

Expand All @@ -322,6 +330,8 @@ private void generateHttpGet(Method method) {
buffer.addLine("])");
buffer.addLine();

proccessKwargs(inParameters);

// Generate the code to build the URL query:
buffer.addLine("# Build the URL:");
buffer.addLine("query = query or {}");
Expand Down Expand Up @@ -354,6 +364,7 @@ private void generateHttpPut(Method method) {
buffer.addLine("headers=None,");
buffer.addLine("query=None,");
buffer.addLine("wait=True,");
buffer.addLine("**kwargs");
buffer.endBlock();
buffer.addLine("):");

Expand All @@ -371,6 +382,8 @@ private void generateHttpPut(Method method) {
buffer.addLine("])");
buffer.addLine();

proccessKwargs(getSecondaryParameters(method));

// Generate the code to build the URL query:
buffer.addLine("# Build the URL:");
buffer.addLine("query = query or {}");
Expand Down Expand Up @@ -401,6 +414,7 @@ private void generateHttpDelete(Method method) {
buffer.addLine("headers=None,");
buffer.addLine("query=None,");
buffer.addLine("wait=True,");
buffer.addLine("**kwargs");
buffer.endBlock();
buffer.addLine("):");

Expand All @@ -417,6 +431,8 @@ private void generateHttpDelete(Method method) {
buffer.addLine("])");
buffer.addLine();

proccessKwargs(inParameters);

// Generate the code to build the URL query:
buffer.addLine("# Build the URL:");
buffer.addLine("query = query or {}");
Expand All @@ -432,6 +448,26 @@ private void generateHttpDelete(Method method) {
buffer.addLine();
}


/**
* This method proccess kwargs parameters, which are used for backward compatibility.
*/
private void proccessKwargs(List<Parameter> inParameters) {
boolean asyncPresent = inParameters.stream()
.filter(p -> p.getName().equals(NameParser.parseUsingCase("async")))
.findAny()
.isPresent();

if (asyncPresent) {
buffer.addLine("# Since Python 3.7 async is reserved keyword, support backward compatibility");
buffer.addLine("if 'async' in kwargs:");
buffer.startBlock();
buffer.addLine("async_ = kwargs.get('async')");
buffer.endBlock();
buffer.addLine();
}
}

private void generateFormalParameter(Parameter parameter) {
buffer.addLine("%1$s=None,", pythonNames.getMemberStyleName(parameter.getName()));
}
Expand Down

0 comments on commit 7f75f47

Please sign in to comment.