forked from disqus/nydus
/
base.py
77 lines (58 loc) · 1.69 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
nydus.db.backends.base
~~~~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2011 DISQUS.
:license: Apache License 2.0, see LICENSE for more details.
"""
__all__ = ('BaseConnection',)
class BasePipeline(object):
"""
Base Pipeline class.
This basically is absolutely useless, and just provides a sample
API for dealing with pipelined commands.
"""
def __init__(self, connection):
self.pending = []
self.connection = connection
def add(self, command):
self.pending.append(command)
def execute(self):
results = {}
for command in self.pending:
results[command._ident] = command(*command._args, **command._kwargs)
return results
class BaseConnection(object):
"""
Base connection class.
Child classes should implement at least
connect() and disconnect() methods.
"""
retryable_exceptions = ()
supports_pipelines = False
def __init__(self, num, **options):
self._connection = None
self.num = num
self.options = options
@property
def identifier(self):
return repr(self)
@property
def connection(self):
if self._connection is None:
self._connection = self.connect()
return self._connection
def close(self):
if self._connection:
self.disconnect()
self._connection = None
def connect(self):
"""
connect() must return a connection object
"""
raise NotImplementedError
def disconnect(self):
raise NotImplementedError
def get_pipeline(self):
return BasePipeline(self)
def __getattr__(self, name):
return getattr(self.connection, name)