Skip to content

Commit d131c28

Browse files
committed
add wrapper ducts, including redis
1 parent 49f7073 commit d131c28

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

omniduct/duct.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ class name if not specified).
292292

293293
atexit.register(self.disconnect)
294294
self.__prepared = False
295-
self.__getting = False
295+
self.__preparing = False
296296
self.__disconnecting = False
297297
self.__cached_auth = {}
298298
self.__prepreparation_values = {}
@@ -346,16 +346,16 @@ def for_protocol(cls, key):
346346
def __getattribute__(self, key):
347347
try:
348348
if (not object.__getattribute__(self, '_Duct__prepared')
349-
and not object.__getattribute__(self, '_Duct__getting')
349+
and not object.__getattribute__(self, '_Duct__preparing')
350350
and not object.__getattribute__(self, '_Duct__disconnecting')
351351
and key in object.__getattribute__(self, '_Duct__prepare_triggers')):
352-
object.__setattr__(self, '_Duct__getting', True)
352+
object.__setattr__(self, '_Duct__preparing', True)
353353
object.__getattribute__(self, 'prepare')()
354-
object.__setattr__(self, '_Duct__getting', False)
354+
object.__setattr__(self, '_Duct__preparing', False)
355355
except AttributeError:
356356
pass
357357
except Exception as e:
358-
object.__setattr__(self, '_Duct__getting', False)
358+
object.__setattr__(self, '_Duct__preparing', False)
359359
raise_with_traceback(e)
360360
return object.__getattribute__(self, key)
361361

omniduct/protocols.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
from .remotes.ssh import SSHClient
1414
from .remotes.ssh_paramiko import ParamikoSSHClient
1515
from .restful.base import RestClient
16+
from .wrappers.redis import RedisClient

omniduct/wrappers/__init__.py

Whitespace-only changes.

omniduct/wrappers/base.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from abc import abstractmethod
2+
from omniduct.duct import Duct
3+
from omniduct.utils.docs import quirk_docs
4+
5+
6+
class WrapperClient(Duct):
7+
8+
DUCT_TYPE = Duct.Type.OTHER
9+
10+
@quirk_docs('_init', mro=True)
11+
def __init__(self, **kwargs):
12+
"""
13+
wrapped (dict): A mapping of default session properties
14+
"""
15+
Duct.__init_with_kwargs__(self, kwargs, port=self.DEFAULT_PORT)
16+
self._init(**kwargs)
17+
18+
@abstractmethod
19+
def _init(self):
20+
pass
21+
22+
@property
23+
def wrapped_field(self):
24+
raise NotImplementedError
25+
26+
def __getattr__(self, key):
27+
return getattr(object.__getattribute__(self, self.wrapped_field), key)

omniduct/wrappers/redis.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from __future__ import absolute_import
2+
3+
import redis
4+
from omniduct.utils.debug import logger
5+
from .base import WrapperClient
6+
7+
from omniduct.utils.magics import (MagicsProvider, process_line_arguments,
8+
process_line_cell_arguments)
9+
10+
11+
class RedisClient(WrapperClient, MagicsProvider):
12+
"""
13+
This Duct connects to a redis database server using the `redis` python library.
14+
"""
15+
PROTOCOLS = ['redis']
16+
DEFAULT_PORT = 6379
17+
18+
def _init(self):
19+
self._redis_connection = None
20+
21+
def _connect(self):
22+
self._redis_connection = redis.Redis(self.host, self.port)
23+
24+
def _is_connected(self):
25+
return hasattr(self, '_redis_connection') and self._redis_connection is not None
26+
27+
def _disconnect(self):
28+
logger.info('Disconnecting from Redis database ...')
29+
self.__redis_connection = None
30+
31+
@property
32+
def wrapped_field(self):
33+
return '_redis_connection'
34+
35+
def _register_magics(self, base_name):
36+
"""
37+
The following magic functions will be registered (assuming that
38+
the base name is chosen to be 'redis'):
39+
- Cell Magics:
40+
- `%%redis`: Run the provided command
41+
42+
Documentation for these magics is provided online.
43+
"""
44+
from IPython.core.magic import register_cell_magic
45+
46+
@register_cell_magic(base_name)
47+
@process_line_cell_arguments
48+
def execute_command_magic(*args, **kwargs):
49+
return self.execute_command(*args, **kwargs)

0 commit comments

Comments
 (0)