Skip to content
Permalink
Browse files
make redis configurable
  • Loading branch information
soperd committed Nov 11, 2018
1 parent 0bdf537 commit 90bd8e173d10cc328339db347b9c9d3c50191f45
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
@@ -7,5 +7,9 @@
"SERVICE_1": {
"token": "SERVICE_TOKEN"
}
},
"redis": {
"host": "localhost",
"port": 6379
}
}
@@ -4,6 +4,7 @@ from os import path
from frozen import FrozenClass



class ConfigDict(FrozenClass):
""" ConfigDict is a dict with attributes as it's contents. """

@@ -28,6 +29,12 @@ class ConfigDict(FrozenClass):
def __contains__(self, attr):
return True if attr in self.__dict__.keys() else False

def to_dict(self):
return {
k: v
for k, v in self.__dict__.items()
if not k.startswith("_")}


class Configuration(ConfigDict):
""" Configuration is a ConfigDict with ability to read json files. """
@@ -0,0 +1,14 @@
import redis

from config import global_config



def create_datastore(**kwargs):
""" create_datastore creates a Redis datastore with a config. """
kwargs["decode_responses"] = True
return redis.StrictRedis(**kwargs)


# create global datastore
global_datastore = create_datastore(**global_config.redis.to_dict())
@@ -4,22 +4,28 @@ import os
sys.path.insert(0, os.path.abspath('..'))

from config import ConfigDict
from datastore import global_datastore
from handlers import global_services



class ChatBot(object):
""" ChatBot is an interface for chat bots. """

def __init__(self, config=ConfigDict(), services=None):
if type(config) is dict:
def __init__(self, config=ConfigDict(), services=None, datastore=None):
if isinstance(config, dict):
config = ConfigDict(**config)
elif type(config) is not ConfigDict \
and not issubclass(type(config), ConfigDict):
elif not isinstance(config, ConfigDict):
raise TypeError("config must be dict or ConfigDict")

self.services = services
self.config = config

if datastore:
self.datastore = datastore
else:
self.datastore = global_datastore

# get service configs
if "services" in self.config and services is None:
self.services = {
@@ -25,8 +25,6 @@ class DiscordBot(ChatBot):
await self._handle_message(message)

self._interpreter = Interpreter.load("./training/models/current/nlu")
self._datastore = redis.StrictRedis(host="localhost", port=6379,
decode_responses=True)
self.client.run(self.config.token)

async def _ready(self):
@@ -112,9 +110,9 @@ class DiscordBot(ChatBot):
"location_long": data["longitude"],
"location_lat" : data["latitude"],
}
return self._datastore.hmset(key, mapping)
return self.datastore.hmset(key, mapping)

def get_user_location(self, user):
""" get_user_location returns the stored user location. """
key = "user:{}".format(user.id)
return self._datastore.hgetall(key)
return self.datastore.hgetall(key)

0 comments on commit 90bd8e1

Please sign in to comment.