Skip to content

Commit

Permalink
connection: introduce common interface
Browse files Browse the repository at this point in the history
Introduce connection interface to be used in connection pool
implementation. Only CRUD and base connect/close API is required
by the interface.

Part of #196
  • Loading branch information
DifferentialOrange committed Apr 20, 2022
1 parent 7c5b57f commit 70b1448
Showing 1 changed file with 86 additions and 1 deletion.
87 changes: 86 additions & 1 deletion tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
import errno
import socket
import abc

import ctypes
import ctypes.util
Expand Down Expand Up @@ -76,8 +77,92 @@
ENCODING_DEFAULT,
)

# Based on https://realpython.com/python-interface/
class ConnectionInterface(metaclass=abc.ABCMeta):
@classmethod
def __subclasshook__(cls, subclass):
return (hasattr(subclass, 'close') and
callable(subclass.close) and
hasattr(subclass, 'is_closed') and
callable(subclass.is_closed) and
hasattr(subclass, 'connect') and
callable(subclass.connect) and
hasattr(subclass, 'call') and
callable(subclass.call) and
hasattr(subclass, 'eval') and
callable(subclass.eval) and
hasattr(subclass, 'replace') and
callable(subclass.replace) and
hasattr(subclass, 'insert') and
callable(subclass.insert) and
hasattr(subclass, 'delete') and
callable(subclass.delete) and
hasattr(subclass, 'upsert') and
callable(subclass.upsert) and
hasattr(subclass, 'update') and
callable(subclass.update) and
hasattr(subclass, 'ping') and
callable(subclass.ping) and
hasattr(subclass, 'select') and
callable(subclass.select) and
hasattr(subclass, 'execute') and
callable(subclass.execute) or
NotImplemented)

@abc.abstractmethod
def close(self):
raise NotImplementedError

@abc.abstractmethod
def is_closed(self):
raise NotImplementedError

@abc.abstractmethod
def connect(self):
raise NotImplementedError

@abc.abstractmethod
def call(self, func_name, *args, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def eval(self, expr, *args, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def replace(self, space_name, values):
raise NotImplementedError

@abc.abstractmethod
def insert(self, space_name, values):
raise NotImplementedError

@abc.abstractmethod
def delete(self, space_name, key, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def upsert(self, space_name, tuple_value, op_list, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def update(self, space_name, key, op_list, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def ping(self, notime):
raise NotImplementedError

@abc.abstractmethod
def select(self, space_name, key, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def execute(self, query, params, **kwargs):
raise NotImplementedError


class Connection(object):
class Connection(ConnectionInterface):
'''
Represents connection to the Tarantool server.
Expand Down

0 comments on commit 70b1448

Please sign in to comment.