Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-1747 [created] 83206a210


cleaned up gremlin-python message serializer classes


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

Branch: refs/heads/TINKERPOP-1747
Commit: 83206a2101a0f03c515d4e1829149ed14a330dc7
Parents: 4e418c1
Author: davebshow <davebs...@gmail.com>
Authored: Wed Aug 9 15:14:44 2017 -0400
Committer: davebshow <davebs...@gmail.com>
Committed: Wed Aug 9 15:14:44 2017 -0400

----------------------------------------------------------------------
 .../jython/gremlin_python/driver/serializer.py  | 59 +++++++++++---------
 .../src/main/jython/tests/conftest.py           | 14 ++++-
 .../main/jython/tests/driver/test_serializer.py | 37 ++++++++++++
 3 files changed, 84 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/83206a21/gremlin-python/src/main/jython/gremlin_python/driver/serializer.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/serializer.py 
b/gremlin-python/src/main/jython/gremlin_python/driver/serializer.py
index 8686b7b..679a7ce 100644
--- a/gremlin-python/src/main/jython/gremlin_python/driver/serializer.py
+++ b/gremlin-python/src/main/jython/gremlin_python/driver/serializer.py
@@ -81,19 +81,35 @@ class Traversal(Processor):
         return args
 
 
-class GraphSONMessageSerializer:
-    """Message serializer for GraphSON"""
-
-    def __init__(self, reader=None, writer=None, 
version=b"application/vnd.gremlin-v3.0+json"):
-        self.version = version
+class GraphSONMessageSerializer(object):
+    """
+    Message serializer for GraphSON. Allow users to pass custom reader,
+    writer, and version kwargs for custom serialization. Otherwise,
+    use current GraphSON version as default.
+    """
+
+    # KEEP TRACK OF CURRENT DEFAULTS
+    DEFAULT_READER = graphsonV3d0.GraphSONReader()
+    DEFAULT_WRITER = graphsonV3d0.GraphSONWriter()
+    DEFAULT_VERSION = b"application/vnd.gremlin-v3.0+json"
+
+    def __init__(self, reader=None, writer=None, version=None):
+        if not version:
+            version = self.DEFAULT_VERSION
+        self._version = version
         if not reader:
-            reader = graphsonV3d0.GraphSONReader()
+            reader = self.DEFAULT_READER
         self._graphson_reader = reader
         if not writer:
-            writer = graphsonV3d0.GraphSONWriter()
+            writer = self.DEFAULT_WRITER
         self.standard = Standard(writer)
         self.traversal = Traversal(writer)
 
+    @property
+    def version(self):
+        """Read only property"""
+        return self._version
+
     def get_processor(self, processor):
         processor = getattr(self, processor, None)
         if not processor:
@@ -129,27 +145,20 @@ class GraphSONMessageSerializer:
     def deserialize_message(self, message):
         return self._graphson_reader.toObject(message)
 
+
 class GraphSONSerializersV2d0(GraphSONMessageSerializer):
     """Message serializer for GraphSON 2.0"""
-
-    def __init__(self, reader=None, writer=None):
-        GraphSONMessageSerializer.__init__(self, reader, writer, 
b"application/vnd.gremlin-v2.0+json")
-        if not reader:
-            self._graphson_reader = graphsonV2d0.GraphSONReader()
-        if not writer:
-            self._graphson_writer = graphsonV2d0.GraphSONWriter()
-        self.standard = Standard(self._graphson_writer)
-        self.traversal = Traversal(self._graphson_writer)
+    def __init__(self):
+        reader = graphsonV2d0.GraphSONReader()
+        writer = graphsonV2d0.GraphSONWriter()
+        version = b"application/vnd.gremlin-v2.0+json"
+        super(GraphSONSerializersV2d0, self).__init__(reader, writer, version)
 
 
 class GraphSONSerializersV3d0(GraphSONMessageSerializer):
     """Message serializer for GraphSON 3.0"""
-
-    def __init__(self, reader=None, writer=None):
-        GraphSONMessageSerializer.__init__(self, reader, writer, 
b"application/vnd.gremlin-v3.0+json")
-        if not reader:
-            self._graphson_reader = graphsonV3d0.GraphSONReader()
-        if not writer:
-            self._graphson_writer = graphsonV3d0.GraphSONWriter()
-        self.standard = Standard(self._graphson_writer)
-        self.traversal = Traversal(self._graphson_writer)
\ No newline at end of file
+    def __init__(self):
+        reader = graphsonV3d0.GraphSONReader()
+        writer = graphsonV3d0.GraphSONWriter()
+        version = b"application/vnd.gremlin-v3.0+json"
+        super(GraphSONSerializersV3d0, self).__init__(reader, writer, version)

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/83206a21/gremlin-python/src/main/jython/tests/conftest.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/tests/conftest.py 
b/gremlin-python/src/main/jython/tests/conftest.py
index d014296..6fcb8db 100644
--- a/gremlin-python/src/main/jython/tests/conftest.py
+++ b/gremlin-python/src/main/jython/tests/conftest.py
@@ -27,7 +27,9 @@ from gremlin_python.driver import serializer
 from gremlin_python.driver.driver_remote_connection import (
     DriverRemoteConnection)
 from gremlin_python.driver.protocol import GremlinServerWSProtocol
-from gremlin_python.driver.serializer import GraphSONMessageSerializer
+from gremlin_python.driver.serializer import (
+    GraphSONMessageSerializer, GraphSONSerializersV2d0,
+    GraphSONSerializersV3d0)
 from gremlin_python.driver.tornado.transport import TornadoTransport
 
 
@@ -86,3 +88,13 @@ def remote_connection_v2(request):
             remote_conn.close()
         request.addfinalizer(fin)
         return remote_conn
+
+
+@pytest.fixture
+def graphson_serializer_v2(request):
+    return GraphSONSerializersV2d0()
+
+
+@pytest.fixture
+def graphson_serializer_v3(request):
+    return GraphSONSerializersV3d0()

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/83206a21/gremlin-python/src/main/jython/tests/driver/test_serializer.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/tests/driver/test_serializer.py 
b/gremlin-python/src/main/jython/tests/driver/test_serializer.py
new file mode 100644
index 0000000..f1b4ccb
--- /dev/null
+++ b/gremlin-python/src/main/jython/tests/driver/test_serializer.py
@@ -0,0 +1,37 @@
+'''
+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.
+'''
+from gremlin_python.structure.io import graphsonV2d0
+from gremlin_python.structure.io import graphsonV3d0
+
+
+__author__ = 'David M. Brown'
+
+
+def test_graphson_serialzier_v2(graphson_serializer_v2):
+    assert graphson_serializer_v2.version == 
b"application/vnd.gremlin-v2.0+json"
+    assert isinstance(graphson_serializer_v2._graphson_reader, 
graphsonV2d0.GraphSONReader)
+    assert isinstance(graphson_serializer_v2.standard._graphson_writer, 
graphsonV2d0.GraphSONWriter)
+    assert isinstance(graphson_serializer_v2.traversal._graphson_writer, 
graphsonV2d0.GraphSONWriter)
+
+
+def test_graphson_serialzier_v3(graphson_serializer_v3):
+    assert graphson_serializer_v3.version == 
b"application/vnd.gremlin-v3.0+json"
+    assert isinstance(graphson_serializer_v3._graphson_reader, 
graphsonV3d0.GraphSONReader)
+    assert isinstance(graphson_serializer_v3.standard._graphson_writer, 
graphsonV3d0.GraphSONWriter)
+    assert isinstance(graphson_serializer_v3.traversal._graphson_writer, 
graphsonV3d0.GraphSONWriter)

Reply via email to