Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A2CE52004F5 for ; Fri, 11 Aug 2017 14:43:50 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A14F716D364; Fri, 11 Aug 2017 12:43:50 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C0C5016D362 for ; Fri, 11 Aug 2017 14:43:49 +0200 (CEST) Received: (qmail 2668 invoked by uid 500); 11 Aug 2017 12:43:49 -0000 Mailing-List: contact commits-help@tinkerpop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tinkerpop.apache.org Delivered-To: mailing list commits@tinkerpop.apache.org Received: (qmail 2658 invoked by uid 99); 11 Aug 2017 12:43:49 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Aug 2017 12:43:49 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6C276DFC28; Fri, 11 Aug 2017 12:43:47 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davebshow@apache.org To: commits@tinkerpop.apache.org Date: Fri, 11 Aug 2017 12:43:48 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/2] tinkerpop git commit: cleaned up gremlin-python message serializer classes archived-at: Fri, 11 Aug 2017 12:43:50 -0000 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/e9f03d5b Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/e9f03d5b Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/e9f03d5b Branch: refs/heads/master Commit: e9f03d5ba8615b325fbb61ea571fa409f848e78d Parents: d083c94 Author: davebshow Authored: Wed Aug 9 15:14:44 2017 -0400 Committer: davebshow Committed: Fri Aug 11 08:41:51 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/e9f03d5b/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/e9f03d5b/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/e9f03d5b/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)