示例#1
0
    def test_auto_instance(self, db):
        instance = Instance(db)

        class Doc(Document):
            pass

        doc_impl_cls = instance.register(Doc)
        assert doc_impl_cls.collection == db['doc']
        assert issubclass(doc_impl_cls, framework_pymongo.PyMongoDocument)
示例#2
0
文件: mongo.py 项目: JBKerby/poketwo
    def __init__(self, bot, host, dbname):
        self.db = AsyncIOMotorClient(host, io_loop=bot.loop)[dbname]
        instance = Instance(self.db)

        g = globals()

        for x in (
            "PokemonBase",
            "Pokemon",
            "EmbeddedPokemon",
            "Member",
            "Listing",
            "Guild",
            "Channel",
            "Counter",
            "Sponsor",
        ):
            setattr(self, x, instance.register(g[x]))
            getattr(self, x).bot = bot
示例#3
0
def patch_umongo_meta(odm_cls, **kargs):
    meta = getattr(odm_cls, META)
    if meta is None:
        return False
    connection = kargs.get(CONNECTION, None)
    database = kargs.get(DATABASE, None)
    collection = kargs.get(COLLECTION, None)

    instance = Instance(connection[database])
    if connection is not None and \
        isinstance(connection, MongoClient) and \
        database is not None and \
        collection is not None:
        col = connection[database][collection]
        setattr(odm_cls, META, col)

    if connection is not None and \
        isinstance(connection, MongoClient) and \
        database is not None:
        instance = instance.register(odm_cls)
        setattr(odm_cls, META, instance)
    return True
示例#4
0
    def __init__(self, bot: commands.Bot):
        self.bot = bot
        self.client = AsyncIOMotorClient(bot.config.DATABASE_URI,
                                         io_loop=bot.loop)
        self.db = self.client[bot.config.DATABASE_NAME]
        instance = Instance(self.db)

        g = globals()

        for x in (
                "PokemonBase",
                "Pokemon",
                "EmbeddedPokemon",
                "Member",
                "Guild",
                "Channel",
                "Counter",
                "Blacklist",
                "Sponsor",
                "Auction",
        ):
            setattr(self, x, instance.register(g[x]))
            getattr(self, x).bot = bot
示例#5
0
    def __init__(self, bot, host, dbname):
        database_uri = os.getenv("DATABASE_URI")
        database_name = os.getenv("DATABASE_NAME")

        self.db = AsyncIOMotorClient(host, io_loop=bot.loop)[dbname]
        instance = Instance(self.db)

        self.Pokemon = instance.register(Pokemon)
        self.Pokemon.bot = bot
        self.Member = instance.register(Member)
        self.Member.bot = bot
        self.Listing = instance.register(Listing)
        self.Listing.bot = bot
        self.Guild = instance.register(Guild)
        self.Guild.bot = bot
        self.Counter = instance.register(Counter)
        self.Counter.bot = bot
        self.Blacklist = instance.register(Blacklist)
        self.Blacklist.bot = bot
示例#6
0
from db.database import Database
from db.chat import Chats
from db.message import Messages
from db.user import Users

from umongo import Instance

db_instance = Database()

instance = Instance(db_instance.db)

instance.register(Chats)
instance.register(Messages)
instance.register(Users)

__all__ = ["Database", "Chats", "Messages", "Users"]
示例#7
0
class DBInstance:
    """
    Rapresents an instance of the database and its rapresentations(User, Message, ...)

    :ivar marshmallow.Schema user_schema: marshmallow web schema for User
    :ivar marshmallow.Schema message_schema: marshmallow web schema for Message
    """

    def __init__(self,
                 database_name: str,
                 database_host: str,
                 database_port: int,
                 database_user: str,
                 database_pwd: str,
                 loop=None):
        if database_port >= 65535:
            raise Exception('Enviroment variable DBPORT is non valid port')

        self._db_name: str = database_name

        uri: str = 'mongodb://{DBUSER}:{DBPSWD}@{DBHOST}:{DBPORT}/{DBNAME}'.format(
            DBUSER=database_user,
            DBPSWD=database_pwd,
            DBHOST=database_host,
            DBPORT=database_port,
            DBNAME=database_name)
        self._connection = motor.motor_asyncio.AsyncIOMotorClient(
            uri, io_loop=loop)
        self._instance = Instance(self._connection[database_name])

        for odm_model in [
                types.User, types.Action, types.Rule, types.Topic, types.Params,
                types.Message, types.UserMessage, types.BotMessage,
                types.WozUserMessage, types.WozBotMessage, types.Context
        ]:
            self._instance.register(odm_model)

        user_meta = get_meta_fields(load=('username',), dump=('id', 'username'))

        message_meta = get_meta_fields(
            load=('text',), dump=('id', 'text', 'timestamp', 'bot'))

        um = self._instance.UserMessage
        uwm = self._instance.WozUserMessage

        class UserWebSchema(self._instance.User.schema.as_marshmallow_schema()):
            __mongoload__ = user_meta.load
            __mongodump__ = user_meta.dump

            class Meta:
                fields = user_meta.fields
                load_only = user_meta.load_only
                dump_only = user_meta.dump_only

        class MessageWebSchema(
                self._instance.Context.schema.as_marshmallow_schema()):
            __mongoload__ = message_meta.load
            __mongodump__ = ('id', 'message.text', 'message._cls', 'timestamp')

            text = marshmallow.fields.Method('_get_text')
            bot = marshmallow.fields.Method('_get_bot')

            def _get_text(self, obj):
                return obj.message.text

            def _get_bot(self, obj):
                return not isinstance(obj.message, (um, uwm))

            class Meta:
                fields = message_meta.fields
                load_only = message_meta.load_only
                dump_only = message_meta.dump_only

        self.user_schema = UserWebSchema()
        self.message_schema = MessageWebSchema()

    def context(self) -> DBContext:
        return DBContext(self._instance)

    async def drop_db(self):
        await self._connection.drop_database(self._db_name)
        self._connection.close()