Juan Hernandez has uploaded a new change for review.

Change subject: sdk: Support "basestring" in Python 3
......................................................................

sdk: Support "basestring" in Python 3

The code generated by the "generateDS.py" tool uses the "basestring"
builtin. This has been removed in Python 3. In order to support both
Pyton 2 and Python 3 this patch creates "basestring" as follows:

  try:
      basestring = basestring
  except NameError:
      basestring = str

Change-Id: Ic8f357aafb361e2c5d5d314260222571720def73
Related: https://bugzilla.redhat.com/1096137
Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com>
---
M generator/src/main/java/org/ovirt/engine/sdk/generator/python/XsdCodegen.java
M src/ovirtsdk/xml/params.py
2 files changed, 37 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk refs/changes/33/41333/1

diff --git 
a/generator/src/main/java/org/ovirt/engine/sdk/generator/python/XsdCodegen.java 
b/generator/src/main/java/org/ovirt/engine/sdk/generator/python/XsdCodegen.java
index 6cd87b3..3dde7b1 100644
--- 
a/generator/src/main/java/org/ovirt/engine/sdk/generator/python/XsdCodegen.java
+++ 
b/generator/src/main/java/org/ovirt/engine/sdk/generator/python/XsdCodegen.java
@@ -30,6 +30,7 @@
 import java.util.regex.Pattern;
 
 import org.ovirt.engine.sdk.generator.XsdData;
+import org.ovirt.engine.sdk.generator.python.templates.BasestringHackTemplate;
 import org.ovirt.engine.sdk.generator.python.templates.BrokersImportsTemplate;
 import org.ovirt.engine.sdk.generator.python.templates.FindRootClassTemplate;
 import org.ovirt.engine.sdk.generator.python.templates.SuperAttributesTemplate;
@@ -78,6 +79,7 @@
 
         // Modify the code:
         addImports();
+        addBasestringHack();
         fixExternalEncoding();
         addSuperAttributes();
         renameExportMethod();
@@ -106,6 +108,33 @@
     }
 
     private void addImports() throws IOException {
+        String text = new BrokersImportsTemplate().evaluate();
+        String[] lines = text.split("\n");
+        addLines(findLastImport() + 1, 0, lines);
+    }
+
+    /**
+     * The code generated by {@code generateDS.py} uses the Python 2 {@code 
basestring} builtin, but this has been
+     * removed in Python 3. In order to support both Python 2 and Python 3 
this method adds code that creates
+     * {@code basestring} assigning {@code str} to it if it doesn't exist:
+     *
+     * <pre>
+     * try:
+     *     basestring = basestring
+     * except NameError:
+     *     basestring = str
+     * </pre>
+     */
+    private void addBasestringHack() throws IOException {
+        String text = new BasestringHackTemplate().evaluate();
+        String[] lines = text.split("\n");
+        addLines(findLastImport() + 1, 0, lines);
+    }
+
+    /**
+     * Returns the index of the last {@code import} line.
+     */
+    private int findLastImport() throws IOException {
         // Find the last line containing an import statement:
         int index = 0;
         for (int i = 0; i < source.size(); i++) {
@@ -117,11 +146,7 @@
         if (index == 0) {
             throw new IOException("Can't find position to add imports.");
         }
-
-        // Add the new imports:
-        String text = new BrokersImportsTemplate().evaluate();
-        String[] lines = text.split("\n");
-        addLines(index + 1, 0, lines);
+        return index;
     }
 
     /**
diff --git a/src/ovirtsdk/xml/params.py b/src/ovirtsdk/xml/params.py
index cd5ee21..5c05afa 100644
--- a/src/ovirtsdk/xml/params.py
+++ b/src/ovirtsdk/xml/params.py
@@ -2,7 +2,7 @@
 # -*- coding: utf-8 -*-
 
 #
-# Generated Fri May 22 13:03:05 2015 by generateDS.py version 2.12a.
+# Generated Fri May 22 13:40:09 2015 by generateDS.py version 2.12a.
 #
 
 import sys
@@ -10,6 +10,12 @@
 import re as re_
 import base64
 import datetime as datetime_
+# Begin NOT_GENERATED
+try:
+    basestring = basestring
+except NameError:
+    basestring = str
+# End NOT_GENERATED
 
 # Begin NOT_GENERATED
 from ovirtsdk.utils.reflectionhelper import ReflectionHelper


-- 
To view, visit https://gerrit.ovirt.org/41333
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic8f357aafb361e2c5d5d314260222571720def73
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-sdk
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to