You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2016/07/15 18:06:35 UTC

tinkerpop git commit: renamed graphson_serializers to graphson. You can now registered GraphSONSerializer instances by type with a global registry. This allows users to add types like Geopoint, etc. moving forward. RawExpression will go away in light of

Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-1278 2a672b19d -> 27531851f


renamed graphson_serializers to graphson. You can now registered GraphSONSerializer instances by type with a global registry. This allows users to add types like Geopoint, etc. moving forward. RawExpression will go away in light of this. Also, graphson module now has GraphSONWriter class just like the Java counterpart with GraphSONWriter.writeObject() as the method to generate JSON of a Python object.


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/27531851
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/27531851
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/27531851

Branch: refs/heads/TINKERPOP-1278
Commit: 27531851fff921592dafc4b084bed51863dd41c0
Parents: 2a672b1
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Fri Jul 15 12:06:28 2016 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Fri Jul 15 12:06:28 2016 -0600

----------------------------------------------------------------------
 .../jython/gremlin_python/process/__init__.py   |   3 +-
 .../jython/gremlin_python/process/graphson.py   | 127 +++++++++++++++++++
 .../process/graphson_serializers.py             |  94 --------------
 .../python/jsr223/JythonScriptEngineSetup.java  |   2 +-
 .../jsr223/PythonGraphSONJavaTranslator.java    |   2 +-
 5 files changed, 131 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/27531851/gremlin-python/src/main/jython/gremlin_python/process/__init__.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/gremlin_python/process/__init__.py b/gremlin-python/src/main/jython/gremlin_python/process/__init__.py
index 0d55164..cb26ca5 100644
--- a/gremlin-python/src/main/jython/gremlin_python/process/__init__.py
+++ b/gremlin-python/src/main/jython/gremlin_python/process/__init__.py
@@ -20,7 +20,8 @@ under the License.
 from graph_traversal import GraphTraversal
 from graph_traversal import GraphTraversalSource
 from graph_traversal import __
-from graphson_serializers import GraphSONSerializer
+from graphson import GraphSONWriter
+from graphson import serializers
 from traversal import Barrier
 from traversal import Bytecode
 from traversal import Cardinality

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/27531851/gremlin-python/src/main/jython/gremlin_python/process/graphson.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/gremlin_python/process/graphson.py b/gremlin-python/src/main/jython/gremlin_python/process/graphson.py
new file mode 100644
index 0000000..ef12c7e
--- /dev/null
+++ b/gremlin-python/src/main/jython/gremlin_python/process/graphson.py
@@ -0,0 +1,127 @@
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+'''
+
+import json
+from abc import abstractmethod
+from aenum import Enum
+
+from traversal import Bytecode
+from traversal import P
+from traversal import Traversal
+
+
+class GraphSONWriter(object):
+    @staticmethod
+    def _dictify(object):
+        for key in serializers:
+            if isinstance(object, key):
+                return serializers[key]._dictify(object)
+        return object
+
+    @staticmethod
+    def writeObject(object):
+        return json.dumps(GraphSONWriter._dictify(object))
+
+
+'''
+Serializers
+'''
+
+
+class GraphSONSerializer(object):
+    @abstractmethod
+    def _dictify(self, object):
+        return object
+
+
+class BytecodeSerializer(GraphSONSerializer):
+    def _dictify(self, bytecode):
+        dict = {}
+        dict["@type"] = "Bytecode"
+        sources = []
+        for instruction in bytecode.source_instructions:
+            inst = []
+            inst.append(_SymbolHelper.toJava(instruction[0]))
+            for arg in instruction[1]:
+                inst.append(GraphSONWriter._dictify(arg))
+            sources.append(inst)
+        steps = []
+        for instruction in bytecode.step_instructions:
+            inst = []
+            inst.append(_SymbolHelper.toJava(instruction[0]))
+            for arg in instruction[1]:
+                inst.append(GraphSONWriter._dictify(arg))
+            steps.append(inst)
+        if len(sources) > 0:
+            dict["source"] = sources
+        if len(steps) > 0:
+            dict["step"] = steps
+        return dict
+
+
+class EnumSerializer(GraphSONSerializer):
+    def _dictify(self, enum):
+        dict = {}
+        dict["@type"] = _SymbolHelper.toJava(type(enum).__name__)
+        dict["value"] = _SymbolHelper.toJava(str(enum.name))
+        return dict
+
+
+class PSerializer(GraphSONSerializer):
+    def _dictify(self, p):
+        dict = {}
+        dict["@type"] = "P"
+        dict["predicate"] = _SymbolHelper.toJava(p.operator)
+        if p.other is None:
+            dict["value"] = GraphSONWriter._dictify(p.value)
+        else:
+            dict["value"] = [GraphSONWriter._dictify(p.value), GraphSONWriter._dictify(p.other)]
+        return dict
+
+
+class TraversalSerializer(BytecodeSerializer):
+    def _dictify(self, traversal):
+        return BytecodeSerializer._dictify(self, traversal.bytecode)
+
+
+class _SymbolHelper(object):
+    symbolMap = {"_global": "global", "_as": "as", "_in": "in", "_and": "and",
+                 "_or": "or", "_is": "is", "_not": "not", "_from": "from"}
+
+    @staticmethod
+    def toJava(symbol):
+        if (symbol in _SymbolHelper.symbolMap):
+            return _SymbolHelper.symbolMap[symbol]
+        else:
+            return symbol
+
+    @staticmethod
+    def mapEnum(enum):
+        if (enum in enumMap):
+            return enumMap[enum]
+        else:
+            return enum
+
+
+serializers = {
+    Bytecode: BytecodeSerializer(),
+    P: PSerializer(),
+    Enum: EnumSerializer(),
+    Traversal: TraversalSerializer()
+}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/27531851/gremlin-python/src/main/jython/gremlin_python/process/graphson_serializers.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/gremlin_python/process/graphson_serializers.py b/gremlin-python/src/main/jython/gremlin_python/process/graphson_serializers.py
deleted file mode 100644
index 2d7fd16..0000000
--- a/gremlin-python/src/main/jython/gremlin_python/process/graphson_serializers.py
+++ /dev/null
@@ -1,94 +0,0 @@
-'''
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements.  See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership.  The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License.  You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied.  See the License for the
-specific language governing permissions and limitations
-under the License.
-'''
-
-import json
-from aenum import Enum
-
-from traversal import Bytecode
-from traversal import P
-from traversal import Traversal
-
-
-class GraphSONSerializer(object):
-    @staticmethod
-    def __dictify(thing):
-        if isinstance(thing, Traversal):
-            return GraphSONSerializer.__dictify(thing.bytecode)
-        elif isinstance(thing, Bytecode):
-            dict = {}
-            dict["@type"] = "Bytecode"
-            sources = []
-            for instruction in thing.source_instructions:
-                inst = []
-                inst.append(_SymbolHelper.toJava(instruction[0]))
-                for arg in instruction[1]:
-                    inst.append(GraphSONSerializer.__dictify(arg))
-                sources.append(inst)
-            steps = []
-            for instruction in thing.step_instructions:
-                inst = []
-                inst.append(_SymbolHelper.toJava(instruction[0]))
-                for arg in instruction[1]:
-                    inst.append(GraphSONSerializer.__dictify(arg))
-                steps.append(inst)
-            if len(sources) > 0:
-                dict["source"] = sources
-            if len(steps) > 0:
-                dict["step"] = steps
-            return dict
-        elif isinstance(thing, Enum):
-            dict = {}
-            dict["@type"] = _SymbolHelper.toJava(type(thing).__name__)
-            dict["value"] = _SymbolHelper.toJava(str(thing.name))
-            return dict
-        elif isinstance(thing, P):
-            dict = {}
-            dict["@type"] = "P"
-            dict["predicate"] = _SymbolHelper.toJava(thing.operator)
-            if thing.other is None:
-                dict["value"] = GraphSONSerializer.__dictify(thing.value)
-            else:
-                dict["value"] = [GraphSONSerializer.__dictify(thing.value), GraphSONSerializer.__dictify(thing.other)]
-            return dict
-        else:
-            return thing
-
-    @staticmethod
-    def serialize(thing):
-        return json.dumps(GraphSONSerializer.__dictify(thing))
-
-
-TO_JAVA_MAP = {"_global": "global", "_as": "as", "_in": "in", "_and": "and",
-               "_or": "or", "_is": "is", "_not": "not", "_from": "from"}
-
-
-class _SymbolHelper(object):
-    @staticmethod
-    def toJava(symbol):
-        if (symbol in TO_JAVA_MAP):
-            return TO_JAVA_MAP[symbol]
-        else:
-            return symbol
-
-    @staticmethod
-    def mapEnum(enum):
-        if (enum in enumMap):
-            return enumMap[enum]
-        else:
-            return enum

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/27531851/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/JythonScriptEngineSetup.java
----------------------------------------------------------------------
diff --git a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/JythonScriptEngineSetup.java b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/JythonScriptEngineSetup.java
index 7c55efe..7992da4 100644
--- a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/JythonScriptEngineSetup.java
+++ b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/JythonScriptEngineSetup.java
@@ -42,7 +42,7 @@ public class JythonScriptEngineSetup {
             jythonEngine.eval("from gremlin_python.driver.rest_remote_connection import RESTRemoteConnection");
             jythonEngine.eval("from gremlin_python.process.traversal import Bytecode");
             jythonEngine.eval("from gremlin_python.structure.remote_graph import RemoteGraph");
-            jythonEngine.eval("from gremlin_python.process.graphson_serializers import GraphSONSerializer");
+            jythonEngine.eval("from gremlin_python.process.graphson import GraphSONWriter");
         } catch (final ScriptException e) {
             throw new IllegalStateException(e.getMessage(), e);
         }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/27531851/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonGraphSONJavaTranslator.java
----------------------------------------------------------------------
diff --git a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonGraphSONJavaTranslator.java b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonGraphSONJavaTranslator.java
index 50d5994..7b13c00 100644
--- a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonGraphSONJavaTranslator.java
+++ b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonGraphSONJavaTranslator.java
@@ -76,7 +76,7 @@ public class PythonGraphSONJavaTranslator<S extends TraversalSource, T extends T
             final ScriptEngine jythonEngine = ScriptEngineCache.get("jython");
             jythonEngine.getBindings(ScriptContext.ENGINE_SCOPE)
                     .put(this.pythonTranslator.getTraversalSource(), jythonEngine.eval("RemoteGraph(None).traversal()"));
-            final String graphsonBytecode = jythonEngine.eval("GraphSONSerializer.serialize(" + this.pythonTranslator.translate(bytecode) + ")").toString();
+            final String graphsonBytecode = jythonEngine.eval("GraphSONWriter.writeObject(" + this.pythonTranslator.translate(bytecode) + ")").toString();
             //System.out.println(graphsonBytecode + "!!!!");
             return this.javaTranslator.translate(this.reader.readObject(new ByteArrayInputStream(graphsonBytecode.getBytes()), Bytecode.class));
         } catch (final Exception e) {