Skip to content

Commit

Permalink
Update Python API generator for Python 3
Browse files Browse the repository at this point in the history
Change PythonAPIGenerator to use the six library, for compatibility with
Python 2 and 3. Also, tweak it to be more compliant with PEP8.

For/from zaproxy/zap-api-python#4 - Fix for Python 3
  • Loading branch information
thc202 committed May 5, 2017
1 parent 6cb4ab3 commit a544467
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/org/zaproxy/zap/extension/api/PythonAPIGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void generatePythonFiles(List<ApiImplementor> implementors) throws IOExce
private void generatePythonElement(ApiElement element, String component,
String type, Writer out) throws IOException {

out.write("\n\n");
boolean hasParams = (element.getMandatoryParamNames() != null &&
element.getMandatoryParamNames().size() > 0) ||
(element.getOptionalParamNames() != null &&
Expand Down Expand Up @@ -163,15 +164,15 @@ private void generatePythonElement(ApiElement element, String component,
} else {
reqParams.append(", ");
}
reqParams.append("'" + param + "' : " + param.toLowerCase());
reqParams.append("'" + param + "': " + param.toLowerCase());
}
}
if (type.equals(ACTION_ENDPOINT) || type.equals(OTHER_ENDPOINT)) {
// Always add the API key - we've no way of knowing if it will be required or not
if (!first) {
reqParams.append(", ");
}
reqParams.append("'").append(API.API_KEY_PARAM).append("' : ").append(API.API_KEY_PARAM);
reqParams.append("'").append(API.API_KEY_PARAM).append("': ").append(API.API_KEY_PARAM);
}
reqParams.append("}");

Expand All @@ -191,7 +192,7 @@ private void generatePythonElement(ApiElement element, String component,
if (type.equals(OTHER_ENDPOINT)) {
out.write(" return (");
} else {
out.write(" return next(");
out.write(" return six.next(six.itervalues(");
}
out.write("self.zap." + method + "(self.zap." + baseUrl + " + '" +
component + "/" + type + "/" + element.getName() + "/'");
Expand All @@ -202,16 +203,15 @@ private void generatePythonElement(ApiElement element, String component,
out.write(reqParams.toString());
out.write(")");
if (!type.equals(OTHER_ENDPOINT)) {
out.write(".itervalues())");
out.write("))");
} else {
out.write(")");
}
} else if (!type.equals(OTHER_ENDPOINT)) {
out.write(").itervalues())");
out.write(")))");
} else {
out.write(")");
}
out.write("\n\n");

}

Expand All @@ -221,10 +221,10 @@ protected void generateAPIFiles(ApiImplementor imp) throws IOException {
System.out.println("Generating " + file.toAbsolutePath());
try (BufferedWriter out = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
out.write(HEADER);
out.write("import six\n\n\n");
out.write("class " + safeName(imp.getPrefix()) + "(object):\n\n");
out.write(" def __init__(self, zap):\n");
out.write(" self.zap = zap\n");
out.write("\n");
out.write(" self.zap = zap");

for (ApiElement view : imp.getApiViews()) {
this.generatePythonElement(view, imp.getPrefix(), VIEW_ENDPOINT, out);
Expand Down

0 comments on commit a544467

Please sign in to comment.