Skip to content

Commit

Permalink
Disconnect storage from adapter class
Browse files Browse the repository at this point in the history
This makes a change to seperate the storage adapter from the base
Adapter class. This is being done because the methods on the base
Adapter class are not needed on the storage adapter.

The main change is that is no longer possible for the storage
adapter to access the ChatBot instance. This should not cause
any probems because there are no "known" use cases where this
is needed.

This also allows a special case to be removed in the class
validation utility so that a check to the "Adapter" class
is never made. This allows the class validation utility to
be more generic.
  • Loading branch information
gunthercox committed Jan 4, 2017
1 parent 3e6135e commit 78beaf8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
1 change: 0 additions & 1 deletion chatterbot/chatterbot.py
Expand Up @@ -51,7 +51,6 @@ def __init__(self, name, **kwargs):

# Add the chatbot instance to each adapter to share information such as
# the name, the current conversation, or other adapters
self.storage.set_chatbot(self)
self.logic.set_chatbot(self)
self.input.set_chatbot(self)
self.output.set_chatbot(self)
Expand Down
44 changes: 32 additions & 12 deletions chatterbot/storage/storage_adapter.py
@@ -1,7 +1,7 @@
from chatterbot.adapters import Adapter
import logging


class StorageAdapter(Adapter):
class StorageAdapter(object):
"""
This is an abstract class that represents the interface
that all storage adapters should implement.
Expand All @@ -11,9 +11,8 @@ def __init__(self, base_query=None, *args, **kwargs):
"""
Initialize common attributes shared by all storage adapters.
"""
super(StorageAdapter, self).__init__(**kwargs)

self.kwargs = kwargs
self.logger = kwargs.get('logger', logging.getLogger(__name__))
self.read_only = kwargs.get('read_only', False)
self.adapter_supports_queries = True
self.base_query = None
Expand Down Expand Up @@ -46,21 +45,27 @@ def count(self):
"""
Return the number of entries in the database.
"""
raise self.AdapterMethodNotImplementedError()
raise self.AdapterMethodNotImplementedError(
'The `count` method is not implemented by this adapter.'
)

def find(self, statement_text):
"""
Returns a object from the database if it exists
"""
raise self.AdapterMethodNotImplementedError()
raise self.AdapterMethodNotImplementedError(
'The `find` method is not implemented by this adapter.'
)

def remove(self, statement_text):
"""
Removes the statement that matches the input text.
Removes any responses from statements where the response text matches
the input text.
"""
raise self.AdapterMethodNotImplementedError()
raise self.AdapterMethodNotImplementedError(
'The `remove` method is not implemented by this adapter.'
)

def filter(self, **kwargs):
"""
Expand All @@ -70,26 +75,34 @@ def filter(self, **kwargs):
all listed attributes and in which all values
match for all listed attributes will be returned.
"""
raise self.AdapterMethodNotImplementedError()
raise self.AdapterMethodNotImplementedError(
'The `filter` method is not implemented by this adapter.'
)

def update(self, statement):
"""
Modifies an entry in the database.
Creates an entry if one does not exist.
"""
raise self.AdapterMethodNotImplementedError()
raise self.AdapterMethodNotImplementedError(
'The `update` method is not implemented by this adapter.'
)

def get_random(self):
"""
Returns a random statement from the database
"""
raise self.AdapterMethodNotImplementedError()
raise self.AdapterMethodNotImplementedError(
'The `get_random` method is not implemented by this adapter.'
)

def drop(self):
"""
Drop the database attached to a given adapter.
"""
raise self.AdapterMethodNotImplementedError()
raise self.AdapterMethodNotImplementedError(
'The `drop` method is not implemented by this adapter.'
)

def get_response_statements(self):
"""
Expand Down Expand Up @@ -119,8 +132,15 @@ def get_response_statements(self):

class EmptyDatabaseException(Exception):

def __init__(self, value="The database currently contains no entries. At least one entry is expected. You may need to train your chat bot to populate your database."):
def __init__(self, value='The database currently contains no entries. At least one entry is expected. You may need to train your chat bot to populate your database.'):
self.value = value

def __str__(self):
return repr(self.value)

class AdapterMethodNotImplementedError(NotImplementedError):
"""
An exception to be raised when a storage adapter method has not been implemented.
Typically this indicates that the method should be implement in a subclass.
"""
pass
8 changes: 0 additions & 8 deletions chatterbot/utils.py
Expand Up @@ -109,14 +109,6 @@ def validate_adapter_class(validate_class, adapter_class):
)
)

if not issubclass(import_module(validate_class), Adapter):
raise Adapter.InvalidAdapterTypeException(
'{} must be a subclass of {}'.format(
validate_class,
Adapter.__name__
)
)

if not issubclass(import_module(validate_class), adapter_class):
raise Adapter.InvalidAdapterTypeException(
'{} must be a subclass of {}'.format(
Expand Down

0 comments on commit 78beaf8

Please sign in to comment.