forked from aichaos/rivescript-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aichaos#84 from aichaos/feature/sessions
Add official Redis driver for RiveScript Sessions
- Loading branch information
Showing
13 changed files
with
247 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
__pycache__ | ||
*.pyc | ||
build/ | ||
dist/ | ||
*.egg-info/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Noah Petherbridge | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,57 @@ | ||
# Redis Sessions for RiveScript | ||
|
||
This module installs support for using a [Redis cache](https://redis.io/) to | ||
store user variables for RiveScript. | ||
|
||
```bash | ||
pip install rivescript-redis | ||
``` | ||
|
||
By default, RiveScript keeps user variables in an in-memory dictionary. This | ||
driver allows for using a Redis cache instead. All user variables will then be | ||
persisted to Redis automatically, which enables the bot to remember users after | ||
a reboot. | ||
|
||
## Quick Start | ||
|
||
```python | ||
from rivescript import RiveScript | ||
from rivescript_redis import RedisSessionManager | ||
|
||
# Initialize RiveScript like normal but give it the RedisSessionManager. | ||
bot = RiveScript( | ||
session_manager=RedisSessionManager( | ||
# You can customize the key prefix: this is the default. Be sure to | ||
# include a separator like '/' at the end so the keys end up looking | ||
# like e.g. 'rivescript/username' | ||
prefix='rivescript/', | ||
|
||
# All other options are passed directly through to redis.StrictRedis() | ||
host='localhost', | ||
port=6379, | ||
db=0, | ||
), | ||
) | ||
|
||
bot.load_directory("eg/brain") | ||
bot.sort_replies() | ||
|
||
# Get a reply. The user variables for 'alice' would be persisted in Redis | ||
# at the (default) key 'rivescript/alice' | ||
print(bot.reply("alice", "Hello robot!")) | ||
``` | ||
|
||
## Example | ||
|
||
An example bot that uses this driver can be found in the | ||
[`eg/sessions`](https://github.com/aichaos/rivescript-python/tree/master/eg/sessions) | ||
directory of the `rivescript-python` project. | ||
|
||
## See Also | ||
|
||
* Documentation for [redis-py](https://redis-py.readthedocs.io/en/latest/), | ||
the Redis client module used by this driver. | ||
|
||
## License | ||
|
||
This module is licensed under the same terms as RiveScript itself (MIT). |
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,2 @@ | ||
redis | ||
rivescript |
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,108 @@ | ||
# RiveScript-Python | ||
# | ||
# This code is released under the MIT License. | ||
# See the "LICENSE" file for more information. | ||
# | ||
# https://www.rivescript.com/ | ||
|
||
from __future__ import unicode_literals | ||
import json | ||
import redis | ||
from rivescript.sessions import SessionManager | ||
|
||
__author__ = 'Noah Petherbridge' | ||
__copyright__ = 'Copyright 2017, Noah Petherbridge' | ||
__license__ = 'MIT' | ||
__status__ = 'Beta' | ||
__version__ = '0.1.0' | ||
|
||
class RedisSessionManager(SessionManager): | ||
"""A Redis powered session manager for RiveScript.""" | ||
|
||
def __init__(self, prefix="rivescript/", *args, **kwargs): | ||
"""Initialize the Redis session driver. | ||
Apart from the ``prefix`` parameter, all other options are passed | ||
directly to the underlying Redis constructor, ``redis.StrictRedis()``. | ||
See the documentation of redis-py for more information. Commonly used | ||
arguments are listed below for convenience. | ||
Args: | ||
prefix (string): the key to prefix all the Redis keys with. The | ||
default is ``rivescript/``, so that for a username of ``alice`` | ||
the key would be ``rivescript/alice``. | ||
host (string): Hostname of the Redis server. | ||
port (int): Port number of the Redis server. | ||
db (int): Database number in Redis. | ||
""" | ||
self.client = redis.StrictRedis(*args, **kwargs) | ||
self.prefix = prefix | ||
self.frozen = "frozen:" + prefix | ||
|
||
def _key(self, username, frozen=False): | ||
"""Translate a username into a key for Redis.""" | ||
if frozen: | ||
return self.frozen + username | ||
return self.prefix + username | ||
|
||
def _get_user(self, username): | ||
"""Custom helper method to retrieve a user's data from Redis.""" | ||
data = self.client.get(self._key(username)) | ||
if data is None: | ||
return None | ||
return json.loads(data.decode()) | ||
|
||
# The below functions implement the RiveScript SessionManager. | ||
|
||
def set(self, username, new_vars): | ||
data = self._get_user(username) | ||
if data is None: | ||
data = self.default_session() | ||
data.update(new_vars) | ||
self.client.set(self._key(username), json.dumps(data)) | ||
|
||
def get(self, username, key): | ||
data = self._get_user(username) | ||
if data is None: | ||
return None | ||
return data.get(key, "undefined") | ||
|
||
def get_any(self, username): | ||
return self._get_user(username) | ||
|
||
def get_all(self): | ||
users = self.client.keys(self.prefix + "*") | ||
result = dict() | ||
for user in users: | ||
username = users.replace(self.prefix, "") | ||
result[username] = self._get_user(username) | ||
return result | ||
|
||
def reset(self, username): | ||
self.client.delete(self._key(username)) | ||
|
||
def reset_all(self): | ||
users = self.client.keys(self.prefix + "*") | ||
for user in users: | ||
self.c.delete(user) | ||
|
||
def freeze(self, username): | ||
data = self._get_user(username) | ||
if data is not None: | ||
self.client.set(self._key(username, True), json.dumps(data)) | ||
|
||
def thaw(self, username, action="thaw"): | ||
data = self.client.get(self.key(username, True)) | ||
if data is not None: | ||
data = json.loads(data.decode()) | ||
if action == "thaw": | ||
self.reset(username) | ||
self.set(username, data) | ||
self.c.delete(self.key(username, True)) | ||
elif action == "discard": | ||
self.c.delete(self.key(username, True)) | ||
elif action == "keep": | ||
self.reset(username) | ||
self.set(username, data) | ||
else: | ||
raise ValueError("unsupported thaw action") |
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,2 @@ | ||
[metadata] | ||
description-file = README.md |
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,29 @@ | ||
# rivescript-python setup.py | ||
|
||
import rivescript_redis | ||
from setuptools import setup | ||
|
||
setup( | ||
name = 'rivescript_redis', | ||
version = rivescript_redis.__version__, | ||
description = 'Redis driver for RiveScript', | ||
long_description = 'Stores user variables for RiveScript in a Redis cache', | ||
author = 'Noah Petherbridge', | ||
author_email = '[email protected]', | ||
url = 'https://github.com/aichaos/rivescript-python', | ||
license = 'MIT', | ||
py_modules = ['rivescript_redis'], | ||
keywords = ['rivescript'], | ||
classifiers = [ | ||
'License :: OSI Approved :: MIT License', | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 2', | ||
'Programming Language :: Python :: 3', | ||
'Development Status :: 3 - Alpha', | ||
'Intended Audience :: Developers', | ||
'Topic :: Scientific/Engineering :: Artificial Intelligence', | ||
], | ||
install_requires = [ 'setuptools', 'redis', 'rivescript' ], | ||
) | ||
|
||
# vim:expandtab |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1 @@ | ||
redis | ||
rivescript-redis |
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