-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49f7073
commit 7af2b19
Showing
6 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |