Skip to content

Commit

Permalink
add wrapper ducts, including redis
Browse files Browse the repository at this point in the history
  • Loading branch information
danfrankcb committed Aug 29, 2018
1 parent 49f7073 commit 7af2b19
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
5 changes: 5 additions & 0 deletions omniduct/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
'pexpect', # Command line handling (including smartcard activation)
],

# Wrappers
'redis': [
'redis', # Primary Client
],

# Rest clients
'rest': [
'requests', # Library to handle underlying REST queries
Expand Down
10 changes: 5 additions & 5 deletions omniduct/duct.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class name if not specified).

atexit.register(self.disconnect)
self.__prepared = False
self.__getting = False
self.__preparing = False
self.__disconnecting = False
self.__cached_auth = {}
self.__prepreparation_values = {}
Expand Down Expand Up @@ -346,16 +346,16 @@ def for_protocol(cls, key):
def __getattribute__(self, key):
try:
if (not object.__getattribute__(self, '_Duct__prepared')
and not object.__getattribute__(self, '_Duct__getting')
and not object.__getattribute__(self, '_Duct__preparing')
and not object.__getattribute__(self, '_Duct__disconnecting')
and key in object.__getattribute__(self, '_Duct__prepare_triggers')):
object.__setattr__(self, '_Duct__getting', True)
object.__setattr__(self, '_Duct__preparing', True)
object.__getattribute__(self, 'prepare')()
object.__setattr__(self, '_Duct__getting', False)
object.__setattr__(self, '_Duct__preparing', False)
except AttributeError:
pass
except Exception as e:
object.__setattr__(self, '_Duct__getting', False)
object.__setattr__(self, '_Duct__preparing', False)
raise_with_traceback(e)
return object.__getattribute__(self, key)

Expand Down
1 change: 1 addition & 0 deletions omniduct/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
from .remotes.ssh import SSHClient
from .remotes.ssh_paramiko import ParamikoSSHClient
from .restful.base import RestClient
from .wrappers.redis import RedisClient
Empty file added omniduct/wrappers/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions omniduct/wrappers/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from abc import abstractmethod
from omniduct.duct import Duct
from omniduct.utils.docs import quirk_docs


class WrapperClient(Duct):

DUCT_TYPE = Duct.Type.OTHER

@quirk_docs('_init', mro=True)
def __init__(self, **kwargs):
Duct.__init_with_kwargs__(self, kwargs, port=self.DEFAULT_PORT)
self._init(**kwargs)

@abstractmethod
def _init(self):
pass

@property
def wrapped_field(self):
raise NotImplementedError

def __getattr__(self, key):
return getattr(object.__getattribute__(self, self.wrapped_field), key)
49 changes: 49 additions & 0 deletions omniduct/wrappers/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import absolute_import

import redis
from omniduct.utils.debug import logger
from .base import WrapperClient

from omniduct.utils.magics import (MagicsProvider, process_line_arguments,
process_line_cell_arguments)


class RedisClient(WrapperClient, MagicsProvider):
"""
This Duct connects to a redis database server using the `redis` python library.
"""
PROTOCOLS = ['redis']
DEFAULT_PORT = 6379

def _init(self):
self._redis_connection = None

def _connect(self):
self._redis_connection = redis.Redis(self.host, self.port)

def _is_connected(self):
return hasattr(self, '_redis_connection') and self._redis_connection is not None

def _disconnect(self):
logger.info('Disconnecting from Redis database ...')
self._redis_connection = None

@property
def wrapped_field(self):
return '_redis_connection'

def _register_magics(self, base_name):
"""
The following magic functions will be registered (assuming that
the base name is chosen to be 'redis'):
- Cell Magics:
- `%%redis`: Run the provided command
Documentation for these magics is provided online.
"""
from IPython.core.magic import register_cell_magic

@register_cell_magic(base_name)
@process_line_cell_arguments
def execute_command_magic(*args, **kwargs):
return self.execute_command(*args, **kwargs)

0 comments on commit 7af2b19

Please sign in to comment.