Skip to content

Commit 8407849

Browse files
thc202juhakivekas
authored andcommitted
Update Python API generator for Python 3
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
1 parent 6f246ca commit 8407849

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/org/zaproxy/zap/extension/api/PythonAPIGenerator.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public void generatePythonFiles(List<ApiImplementor> implementors) throws IOExce
9292
private void generatePythonElement(ApiElement element, String component,
9393
String type, Writer out) throws IOException {
9494

95+
out.write("\n\n");
9596
boolean hasParams = (element.getMandatoryParamNames() != null &&
9697
element.getMandatoryParamNames().size() > 0) ||
9798
(element.getOptionalParamNames() != null &&
@@ -163,15 +164,15 @@ private void generatePythonElement(ApiElement element, String component,
163164
} else {
164165
reqParams.append(", ");
165166
}
166-
reqParams.append("'" + param + "' : " + param.toLowerCase());
167+
reqParams.append("'" + param + "': " + param.toLowerCase());
167168
}
168169
}
169170
if (type.equals(ACTION_ENDPOINT) || type.equals(OTHER_ENDPOINT)) {
170171
// Always add the API key - we've no way of knowing if it will be required or not
171172
if (!first) {
172173
reqParams.append(", ");
173174
}
174-
reqParams.append("'").append(API.API_KEY_PARAM).append("' : ").append(API.API_KEY_PARAM);
175+
reqParams.append("'").append(API.API_KEY_PARAM).append("': ").append(API.API_KEY_PARAM);
175176
}
176177
reqParams.append("}");
177178

@@ -191,7 +192,7 @@ private void generatePythonElement(ApiElement element, String component,
191192
if (type.equals(OTHER_ENDPOINT)) {
192193
out.write(" return (");
193194
} else {
194-
out.write(" return next(");
195+
out.write(" return six.next(six.itervalues(");
195196
}
196197
out.write("self.zap." + method + "(self.zap." + baseUrl + " + '" +
197198
component + "/" + type + "/" + element.getName() + "/'");
@@ -202,16 +203,15 @@ private void generatePythonElement(ApiElement element, String component,
202203
out.write(reqParams.toString());
203204
out.write(")");
204205
if (!type.equals(OTHER_ENDPOINT)) {
205-
out.write(".itervalues())");
206+
out.write("))");
206207
} else {
207208
out.write(")");
208209
}
209210
} else if (!type.equals(OTHER_ENDPOINT)) {
210-
out.write(").itervalues())");
211+
out.write(")))");
211212
} else {
212213
out.write(")");
213214
}
214-
out.write("\n\n");
215215

216216
}
217217

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

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

0 commit comments

Comments
 (0)