|
4 | 4 | This module provides low-level API for Tarantool
|
5 | 5 | '''
|
6 | 6 |
|
7 |
| -import os |
8 |
| -import time |
| 7 | +import abc |
| 8 | +import ctypes |
| 9 | +import ctypes.util |
9 | 10 | import errno
|
| 11 | +import os |
10 | 12 | import socket
|
| 13 | +import time |
| 14 | + |
| 15 | +import msgpack |
| 16 | +import tarantool.error |
| 17 | +from tarantool.const import (CONNECTION_TIMEOUT, IPROTO_GREETING_SIZE, |
| 18 | + ITERATOR_ALL, ITERATOR_EQ, RECONNECT_DELAY, |
| 19 | + RECONNECT_MAX_ATTEMPTS, REQUEST_TYPE_ERROR, |
| 20 | + REQUEST_TYPE_OK, SOCKET_TIMEOUT) |
| 21 | +from tarantool.error import (ConfigurationError, DatabaseError, DataError, |
| 22 | + Error, IntegrityError, InterfaceError, |
| 23 | + InternalError, NetworkError, NetworkWarning, |
| 24 | + NotSupportedError, OperationalError, |
| 25 | + ProgrammingError, SchemaError, |
| 26 | + SchemaReloadException, Warning, warn) |
| 27 | +from tarantool.request import (Request, RequestAuthenticate, # RequestOK, |
| 28 | + RequestCall, RequestDelete, RequestEval, |
| 29 | + RequestExecute, RequestInsert, RequestJoin, |
| 30 | + RequestPing, RequestReplace, RequestSelect, |
| 31 | + RequestSubscribe, RequestUpdate, RequestUpsert) |
| 32 | +from tarantool.response import Response |
| 33 | +from tarantool.schema import Schema |
| 34 | +from tarantool.space import Space |
| 35 | +from tarantool.utils import (ENCODING_DEFAULT, check_key, greeting_decode, |
| 36 | + string_types, version_id) |
11 | 37 |
|
12 |
| -import ctypes |
13 |
| -import ctypes.util |
14 | 38 | try:
|
15 | 39 | from ctypes import c_ssize_t
|
16 | 40 | except ImportError:
|
17 | 41 | from ctypes import c_longlong as c_ssize_t
|
18 | 42 |
|
19 |
| -import msgpack |
20 | 43 |
|
21 |
| -import tarantool.error |
22 |
| -from tarantool.response import Response |
23 |
| -from tarantool.request import ( |
24 |
| - Request, |
25 |
| - # RequestOK, |
26 |
| - RequestCall, |
27 |
| - RequestDelete, |
28 |
| - RequestEval, |
29 |
| - RequestInsert, |
30 |
| - RequestJoin, |
31 |
| - RequestReplace, |
32 |
| - RequestPing, |
33 |
| - RequestSelect, |
34 |
| - RequestSubscribe, |
35 |
| - RequestUpdate, |
36 |
| - RequestUpsert, |
37 |
| - RequestAuthenticate, |
38 |
| - RequestExecute |
39 |
| -) |
40 |
| -from tarantool.space import Space |
41 |
| -from tarantool.const import ( |
42 |
| - CONNECTION_TIMEOUT, |
43 |
| - SOCKET_TIMEOUT, |
44 |
| - RECONNECT_MAX_ATTEMPTS, |
45 |
| - RECONNECT_DELAY, |
46 |
| - REQUEST_TYPE_OK, |
47 |
| - REQUEST_TYPE_ERROR, |
48 |
| - IPROTO_GREETING_SIZE, |
49 |
| - ITERATOR_EQ, |
50 |
| - ITERATOR_ALL |
51 |
| -) |
52 |
| -from tarantool.error import ( |
53 |
| - Error, |
54 |
| - NetworkError, |
55 |
| - DatabaseError, |
56 |
| - InterfaceError, |
57 |
| - ConfigurationError, |
58 |
| - SchemaError, |
59 |
| - NetworkWarning, |
60 |
| - OperationalError, |
61 |
| - DataError, |
62 |
| - IntegrityError, |
63 |
| - InternalError, |
64 |
| - ProgrammingError, |
65 |
| - NotSupportedError, |
66 |
| - SchemaReloadException, |
67 |
| - Warning, |
68 |
| - warn |
69 |
| -) |
70 |
| -from tarantool.schema import Schema |
71 |
| -from tarantool.utils import ( |
72 |
| - check_key, |
73 |
| - greeting_decode, |
74 |
| - version_id, |
75 |
| - string_types, |
76 |
| - ENCODING_DEFAULT, |
77 |
| -) |
| 44 | + |
| 45 | +# Based on https://realpython.com/python-interface/ |
| 46 | +class ConnectionInterface(metaclass=abc.ABCMeta): |
| 47 | + @classmethod |
| 48 | + def __subclasshook__(cls, subclass): |
| 49 | + return (hasattr(subclass, 'close') and |
| 50 | + callable(subclass.close) and |
| 51 | + hasattr(subclass, 'is_closed') and |
| 52 | + callable(subclass.is_closed) and |
| 53 | + hasattr(subclass, 'connect') and |
| 54 | + callable(subclass.connect) and |
| 55 | + hasattr(subclass, 'call') and |
| 56 | + callable(subclass.call) and |
| 57 | + hasattr(subclass, 'eval') and |
| 58 | + callable(subclass.eval) and |
| 59 | + hasattr(subclass, 'replace') and |
| 60 | + callable(subclass.replace) and |
| 61 | + hasattr(subclass, 'insert') and |
| 62 | + callable(subclass.insert) and |
| 63 | + hasattr(subclass, 'delete') and |
| 64 | + callable(subclass.delete) and |
| 65 | + hasattr(subclass, 'upsert') and |
| 66 | + callable(subclass.upsert) and |
| 67 | + hasattr(subclass, 'update') and |
| 68 | + callable(subclass.update) and |
| 69 | + hasattr(subclass, 'ping') and |
| 70 | + callable(subclass.ping) and |
| 71 | + hasattr(subclass, 'select') and |
| 72 | + callable(subclass.select) and |
| 73 | + hasattr(subclass, 'execute') and |
| 74 | + callable(subclass.execute) or |
| 75 | + NotImplemented) |
| 76 | + |
| 77 | + @abc.abstractmethod |
| 78 | + def close(self): |
| 79 | + raise NotImplementedError |
| 80 | + |
| 81 | + @abc.abstractmethod |
| 82 | + def is_closed(self): |
| 83 | + raise NotImplementedError |
| 84 | + |
| 85 | + @abc.abstractmethod |
| 86 | + def connect(self): |
| 87 | + raise NotImplementedError |
| 88 | + |
| 89 | + @abc.abstractmethod |
| 90 | + def call(self, func_name, *args, **kwargs): |
| 91 | + raise NotImplementedError |
| 92 | + |
| 93 | + @abc.abstractmethod |
| 94 | + def eval(self, expr, *args, **kwargs): |
| 95 | + raise NotImplementedError |
| 96 | + |
| 97 | + @abc.abstractmethod |
| 98 | + def replace(self, space_name, values): |
| 99 | + raise NotImplementedError |
| 100 | + |
| 101 | + @abc.abstractmethod |
| 102 | + def insert(self, space_name, values): |
| 103 | + raise NotImplementedError |
| 104 | + |
| 105 | + @abc.abstractmethod |
| 106 | + def delete(self, space_name, key, **kwargs): |
| 107 | + raise NotImplementedError |
| 108 | + |
| 109 | + @abc.abstractmethod |
| 110 | + def upsert(self, space_name, tuple_value, op_list, **kwargs): |
| 111 | + raise NotImplementedError |
| 112 | + |
| 113 | + @abc.abstractmethod |
| 114 | + def update(self, space_name, key, op_list, **kwargs): |
| 115 | + raise NotImplementedError |
| 116 | + |
| 117 | + @abc.abstractmethod |
| 118 | + def ping(self, notime): |
| 119 | + raise NotImplementedError |
| 120 | + |
| 121 | + @abc.abstractmethod |
| 122 | + def select(self, space_name, key, **kwargs): |
| 123 | + raise NotImplementedError |
| 124 | + |
| 125 | + @abc.abstractmethod |
| 126 | + def execute(self, query, params, **kwargs): |
| 127 | + raise NotImplementedError |
78 | 128 |
|
79 | 129 |
|
80 |
| -class Connection(object): |
| 130 | +class Connection(ConnectionInterface): |
81 | 131 | '''
|
82 | 132 | Represents connection to the Tarantool server.
|
83 | 133 |
|
|
0 commit comments