|
8 | 8 | import time
|
9 | 9 | import errno
|
10 | 10 | import socket
|
| 11 | +import abc |
11 | 12 |
|
12 | 13 | import ctypes
|
13 | 14 | import ctypes.util
|
|
76 | 77 | ENCODING_DEFAULT,
|
77 | 78 | )
|
78 | 79 |
|
| 80 | +# Based on https://realpython.com/python-interface/ |
| 81 | +class ConnectionInterface(metaclass=abc.ABCMeta): |
| 82 | + @classmethod |
| 83 | + def __subclasshook__(cls, subclass): |
| 84 | + return (hasattr(subclass, 'close') and |
| 85 | + callable(subclass.close) and |
| 86 | + hasattr(subclass, 'is_closed') and |
| 87 | + callable(subclass.is_closed) and |
| 88 | + hasattr(subclass, 'connect') and |
| 89 | + callable(subclass.connect) and |
| 90 | + hasattr(subclass, 'call') and |
| 91 | + callable(subclass.call) and |
| 92 | + hasattr(subclass, 'eval') and |
| 93 | + callable(subclass.eval) and |
| 94 | + hasattr(subclass, 'replace') and |
| 95 | + callable(subclass.replace) and |
| 96 | + hasattr(subclass, 'insert') and |
| 97 | + callable(subclass.insert) and |
| 98 | + hasattr(subclass, 'delete') and |
| 99 | + callable(subclass.delete) and |
| 100 | + hasattr(subclass, 'upsert') and |
| 101 | + callable(subclass.upsert) and |
| 102 | + hasattr(subclass, 'update') and |
| 103 | + callable(subclass.update) and |
| 104 | + hasattr(subclass, 'ping') and |
| 105 | + callable(subclass.ping) and |
| 106 | + hasattr(subclass, 'select') and |
| 107 | + callable(subclass.select) and |
| 108 | + hasattr(subclass, 'execute') and |
| 109 | + callable(subclass.execute) or |
| 110 | + NotImplemented) |
| 111 | + |
| 112 | + @abc.abstractmethod |
| 113 | + def close(self): |
| 114 | + raise NotImplementedError |
| 115 | + |
| 116 | + @abc.abstractmethod |
| 117 | + def is_closed(self): |
| 118 | + raise NotImplementedError |
| 119 | + |
| 120 | + @abc.abstractmethod |
| 121 | + def connect(self): |
| 122 | + raise NotImplementedError |
| 123 | + |
| 124 | + @abc.abstractmethod |
| 125 | + def call(self, func_name, *args, **kwargs): |
| 126 | + raise NotImplementedError |
| 127 | + |
| 128 | + @abc.abstractmethod |
| 129 | + def eval(self, expr, *args, **kwargs): |
| 130 | + raise NotImplementedError |
| 131 | + |
| 132 | + @abc.abstractmethod |
| 133 | + def replace(self, space_name, values): |
| 134 | + raise NotImplementedError |
| 135 | + |
| 136 | + @abc.abstractmethod |
| 137 | + def insert(self, space_name, values): |
| 138 | + raise NotImplementedError |
| 139 | + |
| 140 | + @abc.abstractmethod |
| 141 | + def delete(self, space_name, key, **kwargs): |
| 142 | + raise NotImplementedError |
| 143 | + |
| 144 | + @abc.abstractmethod |
| 145 | + def upsert(self, space_name, tuple_value, op_list, **kwargs): |
| 146 | + raise NotImplementedError |
| 147 | + |
| 148 | + @abc.abstractmethod |
| 149 | + def update(self, space_name, key, op_list, **kwargs): |
| 150 | + raise NotImplementedError |
| 151 | + |
| 152 | + @abc.abstractmethod |
| 153 | + def ping(self, notime): |
| 154 | + raise NotImplementedError |
| 155 | + |
| 156 | + @abc.abstractmethod |
| 157 | + def select(self, space_name, key, **kwargs): |
| 158 | + raise NotImplementedError |
| 159 | + |
| 160 | + @abc.abstractmethod |
| 161 | + def execute(self, query, params, **kwargs): |
| 162 | + raise NotImplementedError |
| 163 | + |
79 | 164 |
|
80 |
| -class Connection(object): |
| 165 | +class Connection(ConnectionInterface): |
81 | 166 | '''
|
82 | 167 | Represents connection to the Tarantool server.
|
83 | 168 |
|
|
0 commit comments