# Path: dockit/backends/base.py
# class BaseDocumentStorage(BaseStorage):
#     """
#     The :class:`~dockit.backends.base.BaseDocumentStorage` class provides an interface
#     for implementing document storage backends.
#     """
#     
#     _connections = DOCUMENT_BACKEND_CONNECTIONS
#     _threaded_connections = THREADED_DOCUMENT_BACKEND_CONNECTIONS
#     
#     def get_query(self, query_index):
#         """
#         return an implemented `BaseDocumentQuerySet` that contains all the documents
#         """
#         raise NotImplementedError
#     
#     def register_document(self, document):
#         """
#         is called for every document registered in the system
#         """
#         pass
#     
#     def save(self, doc_class, collection, data):
#         """
#         stores the given primitive data in the specified collection
#         """
#         raise NotImplementedError
#     
#     def get(self, doc_class, collection, doc_id):
#         """
#         returns the primitive data for the document belonging in the specified collection
#         """
#         raise NotImplementedError
#     
#     def delete(self, doc_class, collection, doc_id):
#         """
#         deletes the given document from the specified collection
#         """
#         raise NotImplementedError
#     
#     def get_id(self, data):
#         """
#         returns the id from the primitive data
#         """
#         return data.get(self.get_id_field_name())
#     
#     def get_id_field_name(self):
#         """
#         returns a string representing the primary key field name
#         """
#         raise NotImplementedError
# 
# class BaseIndexStorage(BaseStorage):
#     """
#     The :class:`~dockit.backends.base.BaseIndexStorage` class provides an interface
#     for implementing index storage backends.
#     """
#     
#     _connections = INDEX_BACKEND_CONNECTIONS
#     _threaded_connections = THREADED_INDEX_BACKEND_CONNECTIONS
#     
#     _indexers = dict()
#     
#     @classmethod
#     def register_indexer(cls, index_cls, *names):
#         for name in names:
#             cls._indexers[name] = index_cls
#     
#     @classmethod
#     def get_indexer(cls, name):
#         return cls._indexers[name]
#     
#     def _get_indexer_for_operation(self, document, op):
#         indexer = self.get_indexer(op.operation)
#         return indexer(document, op)
#     
#     def register_index(self, query_index):
#         """
#         register the query index with this backend
#         """
#         raise NotImplementedError
#     
#     def destroy_index(self, query_index):
#         """
#         release the query index with this backend
#         """
#         raise NotImplementedError
#     
#     def get_query(self, query_index):
#         """
#         returns an implemented `BaseDocumentQuerySet` representing the query index
#         """
#         raise NotImplementedError
#     
#     def register_document(self, document):
#         """
#         is called for every document registered in the system
#         """
#         pass
#     
#     def on_save(self, doc_class, collection, doc_id, data):
#         """
#         is called for every document save
#         """
#         raise NotImplementedError
#     
#     def on_delete(self, doc_class, collection, doc_id):
#         """
#         is called for every document delete
#         """
#         raise NotImplementedError
# 
# Path: dockit/backends/queryset.py
# class BaseDocumentQuery(object):
#     """
#     Implemented by the backend to execute a certain index
#     """
#     def __init__(self, query_index, backend=None):
#         self.query_index = query_index
#         self._backend = backend
#     
#     @property
#     def document(self):
#         return self.query_index.document
#     
#     @property
#     def backend(self):
#         if self._backend:
#             return self._backend
#         return self.document._meta.get_index_backend_for_read(self.query_index)
#     
#     def _get_indexer_for_operation(self, document, op):
#         return self.backend._get_indexer_for_operation(document, op)
#     
#     def __len__(self):
#         raise NotImplementedError
#     
#     def count(self):
#         return self.__len__()
#     
#     def exists(self):
#         return bool(self.__len__())
#     
#     def delete(self):
#         raise NotImplementedError
#     
#     def get(self, **kwargs):
#         filter_operations = self.query_index._parse_kwargs(kwargs)
#         return self.get_from_filter_operations(filter_operations)
#     
#     def get_from_filter_operations(self, filter_operations):
#         raise NotImplementedError
#     
#     def values(self, *limit_to, **kwargs):
#         raise NotImplementedError
#     
#     def __getitem__(self, val):
#         raise NotImplementedError
#     
#     def __nonzero__(self):
#         raise NotImplementedError


from pymongo import Connection
    from bson.objectid import ObjectId
    from pymongo.objectid import ObjectId
from dockit.backends.base import BaseDocumentStorage, BaseIndexStorage
from dockit.backends.queryset import BaseDocumentQuery
        if params:
            return self.collection.find(params, fields=fields, as_class=ValuesResultClass)
        return self.collection.find(fields=fields, as_class=ValuesResultClass)
    
    def __len__(self):
        return self.queryset.count()
    
    def __nonzero__(self):
        return bool(self.queryset)
    
    def __getitem__(self, val):
        if isinstance(val, slice):
            results = list()
            #TODO i don't think mongo supports passing a slice
            for entry in self.queryset[val]:
                results.append(self.wrap(entry))
            return results
        else:
            return self.wrap(self.queryset[val])

class MongoStorageMixin(object):
    def __init__(self, username=None, password=None, host=None, port=None, db=None, **kwargs):
        self.connection = Connection(kwargs.get('HOST', host), kwargs.get('PORT', port))
        self.db = self.connection[kwargs.get('DB', db)]
        if username:
            self.db.authenticate(kwargs.get('USER', username), kwargs.get('PASSWORD', password))
    
    def get_collection(self, collection):
        return self.db[collection]


# Path: dockit/backends/base.py
# class BaseDocumentStorage(BaseStorage):
#     """
#     The :class:`~dockit.backends.base.BaseDocumentStorage` class provides an interface
#     for implementing document storage backends.
#     """
#     
#     _connections = DOCUMENT_BACKEND_CONNECTIONS
#     _threaded_connections = THREADED_DOCUMENT_BACKEND_CONNECTIONS
#     
#     def get_query(self, query_index):
#         """
#         return an implemented `BaseDocumentQuerySet` that contains all the documents
#         """
#         raise NotImplementedError
#     
#     def register_document(self, document):
#         """
#         is called for every document registered in the system
#         """
#         pass
#     
#     def save(self, doc_class, collection, data):
#         """
#         stores the given primitive data in the specified collection
#         """
#         raise NotImplementedError
#     
#     def get(self, doc_class, collection, doc_id):
#         """
#         returns the primitive data for the document belonging in the specified collection
#         """
#         raise NotImplementedError
#     
#     def delete(self, doc_class, collection, doc_id):
#         """
#         deletes the given document from the specified collection
#         """
#         raise NotImplementedError
#     
#     def get_id(self, data):
#         """
#         returns the id from the primitive data
#         """
#         return data.get(self.get_id_field_name())
#     
#     def get_id_field_name(self):
#         """
#         returns a string representing the primary key field name
#         """
#         raise NotImplementedError
# 
# class BaseIndexStorage(BaseStorage):
#     """
#     The :class:`~dockit.backends.base.BaseIndexStorage` class provides an interface
#     for implementing index storage backends.
#     """
#     
#     _connections = INDEX_BACKEND_CONNECTIONS
#     _threaded_connections = THREADED_INDEX_BACKEND_CONNECTIONS
#     
#     _indexers = dict()
#     
#     @classmethod
#     def register_indexer(cls, index_cls, *names):
#         for name in names:
#             cls._indexers[name] = index_cls
#     
#     @classmethod
#     def get_indexer(cls, name):
#         return cls._indexers[name]
#     
#     def _get_indexer_for_operation(self, document, op):
#         indexer = self.get_indexer(op.operation)
#         return indexer(document, op)
#     
#     def register_index(self, query_index):
#         """
#         register the query index with this backend
#         """
#         raise NotImplementedError
#     
#     def destroy_index(self, query_index):
#         """
#         release the query index with this backend
#         """
#         raise NotImplementedError
#     
#     def get_query(self, query_index):
#         """
#         returns an implemented `BaseDocumentQuerySet` representing the query index
#         """
#         raise NotImplementedError
#     
#     def register_document(self, document):
#         """
#         is called for every document registered in the system
#         """
#         pass
#     
#     def on_save(self, doc_class, collection, doc_id, data):
#         """
#         is called for every document save
#         """
#         raise NotImplementedError
#     
#     def on_delete(self, doc_class, collection, doc_id):
#         """
#         is called for every document delete
#         """
#         raise NotImplementedError
# 
# Path: dockit/backends/queryset.py
# class BaseDocumentQuery(object):
#     """
#     Implemented by the backend to execute a certain index
#     """
#     def __init__(self, query_index, backend=None):
#         self.query_index = query_index
#         self._backend = backend
#     
#     @property
#     def document(self):
#         return self.query_index.document
#     
#     @property
#     def backend(self):
#         if self._backend:
#             return self._backend
#         return self.document._meta.get_index_backend_for_read(self.query_index)
#     
#     def _get_indexer_for_operation(self, document, op):
#         return self.backend._get_indexer_for_operation(document, op)
#     
#     def __len__(self):
#         raise NotImplementedError
#     
#     def count(self):
#         return self.__len__()
#     
#     def exists(self):
#         return bool(self.__len__())
#     
#     def delete(self):
#         raise NotImplementedError
#     
#     def get(self, **kwargs):
#         filter_operations = self.query_index._parse_kwargs(kwargs)
#         return self.get_from_filter_operations(filter_operations)
#     
#     def get_from_filter_operations(self, filter_operations):
#         raise NotImplementedError
#     
#     def values(self, *limit_to, **kwargs):
#         raise NotImplementedError
#     
#     def __getitem__(self, val):
#         raise NotImplementedError
#     
#     def __nonzero__(self):
#         raise NotImplementedError


# Path: dockit/backends/mongo/backend.py
from pymongo import Connection
    from bson.objectid import ObjectId
    from pymongo.objectid import ObjectId
from dockit.backends.base import BaseDocumentStorage, BaseIndexStorage
from dockit.backends.queryset import BaseDocumentQuery

        if params:
            return self.collection.find(params, fields=fields, as_class=ValuesResultClass)
        return self.collection.find(fields=fields, as_class=ValuesResultClass)
    
    def __len__(self):
        return self.queryset.count()
    
    def __nonzero__(self):
        return bool(self.queryset)
    
    def __getitem__(self, val):
        if isinstance(val, slice):
            results = list()
            #TODO i don't think mongo supports passing a slice
            for entry in self.queryset[val]:
                results.append(self.wrap(entry))
            return results
        else:
            return self.wrap(self.queryset[val])

class MongoStorageMixin(object):
    def __init__(self, username=None, password=None, host=None, port=None, db=None, **kwargs):
        self.connection = Connection(kwargs.get('HOST', host), kwargs.get('PORT', port))
        self.db = self.connection[kwargs.get('DB', db)]
        if username:
            self.db.authenticate(kwargs.get('USER', username), kwargs.get('PASSWORD', password))
    
    def get_collection(self, collection):
        return self.db[collection]


class MongoIndexStorage(BaseIndexStorage, MongoStorageMixin):