DriverRemoteConnection.open()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 30
rs 9.4
c 0
b 0
f 0
cc 4
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
import asyncio
2
from urllib.parse import urlparse
3
4
from aiogremlin.driver.cluster import Cluster
5
from gremlin_python.driver import serializer
6
from aiogremlin.remote.driver_remote_side_effects import (
7
    AsyncRemoteTraversalSideEffects)
8
from gremlin_python.driver.remote_connection import RemoteTraversal
9
10
11
__author__ = 'David M. Brown ([email protected])'
12
13
14
class DriverRemoteConnection:
15
    """
16
    Remote connection to a Gremlin Server. Do not instantiate directly,
17
    instead use :py:meth:`DriverRemoteConnection.open` or
18
    :py:meth:`DriverRemoteConnection.using`
19
20
    :param aiogremlin.driver.client.Client client:
21
    :param asyncio.BaseEventLoop loop:
22
    :param aiogremlin.driver.cluster.Cluster cluster:
23
    """
24
25
    def __init__(self, client, loop, *, cluster=None):
26
        self._client = client
27
        self._loop = loop
28
        self._cluster = cluster
29
30
    @property
31
    def client(self):
32
        return self._client
33
34
    @property
35
    def config(self):
36
        return self._cluster.config
37
38
    @classmethod
39
    async def using(cls, cluster, aliases=None):
40
        """
41
        Create a :py:class:`DriverRemoteConnection` using a specific
42
        :py:class:`Cluster<aiogremlin.driver.cluster.Cluster>`
43
44
        :param aiogremlin.driver.cluster.Cluster cluster:
45
        :param dict aliases: Optional mapping for aliases. Default is `None`.
46
            Also accepts `str` argument which will be assigned to `g`
47
        """
48
        client = await cluster.connect(aliases=aliases)
49
        loop = cluster._loop
50
        return cls(client, loop)
51
52
    @classmethod
53
    async def open(cls, url=None, aliases=None, loop=None, *,
54
                   graphson_reader=None, graphson_writer=None, **config):
55
        """
56
        :param str url: Optional url for host Gremlin Server
57
58
        :param dict aliases: Optional mapping for aliases. Default is `None`.
59
            Also accepts `str` argument which will be assigned to `g`
60
        :param asyncio.BaseEventLoop loop:
61
        :param graphson_reader: Custom graphson_reader
62
        :param graphson_writer: Custom graphson_writer
63
        :param config: Optional cluster configuration passed as kwargs or `dict`
64
        """
65
        if url:
66
            parsed_url = urlparse(url)
67
            config.update({
68
                'scheme': parsed_url.scheme,
69
                'hosts': [parsed_url.hostname],
70
                'port': parsed_url.port})
71
        if isinstance(aliases, str):
72
            aliases = {'g': aliases}
73
        if not loop:
74
            loop = asyncio.get_event_loop()
75
        message_serializer = serializer.GraphSONMessageSerializer(
76
            reader=graphson_reader,
77
            writer=graphson_writer)
78
        config.update({'message_serializer': message_serializer})
79
        cluster = await Cluster.open(loop, aliases=aliases, **config)
80
        client = await cluster.connect()
81
        return cls(client, loop, cluster=cluster)
82
83
    async def close(self):
84
        """
85
        Close underlying cluster if applicable. If created with
86
        :py:meth:`DriverRemoteConnection.using`, cluster is NOT closed.
87
        """
88
        if self._cluster:
89
            await self._cluster.close()
90
91
    async def submit(self, bytecode):
92
        """Submit bytecode to the Gremlin Server"""
93
        result_set = await self._client.submit(bytecode)
94
        side_effects = AsyncRemoteTraversalSideEffects(result_set.request_id,
95
                                                  self._client)
96
        return RemoteTraversal(result_set, side_effects)
97
98
    async def __aenter__(self):
99
        return self
100
101
    async def __aexit__(self, exc_type, exc, tb):
102
        await self.close()
103
        self._client = None
104
        self._cluster = None
105