Example #1
0
def configure_database():
    connection = connect(ReDB_HOST,
                         ReDB_PORT,
                         user=ReDB_USER,
                         password=ReDB_PASS)
    create_table(database, empty_table, connection)
    delete_all(database, table, connection)
    data = [{
        'name':
        'The Dark Knight Rises',
        'year':
        2012,
        'runtime':
        163,
        'categories': ['adventure', 'thriller'],
        'release-date':
        '2012-07-20',
        'director':
        'Christopher Nolan',
        'writer':
        ['Jonathan Nolan', 'David S. Goyer', 'Christopher Nolan', 'Bob Kane'],
        'actors': ['Christian Bale', 'Tom Hardy', 'Anne Hathway'],
        'storyline':
        'Eight years after the Joker\'s reign of anarchy,'
        ' the Dark Knight, with the help of the enigmatic'
        ' Catwoman, is forced from his imposed exile to save'
        ' Gotham City, now on the edge of total annihilation,'
        ' from the brutal guerrilla terrorist Bane.'
    }, {
        'name':
        'Avengers: Infinity War',
        'year':
        2018,
        'runtime':
        149,
        'categories': ['Action', 'Adventure', 'Fantasy'],
        'release-date':
        '2018-04-27',
        'director': ['Anthony Russo', 'Joe Russo'],
        'writer': ['Christopher Markus', 'Stephen McFeely'],
        'actors': [
            'Robert Downey Jr.', 'Chris Hemsworth', 'Mark Ruffalo',
            'Chris Evans', 'Scarlett Johansson', 'Benedict Cumberbatch'
        ],
        'storyline':
        'The Avengers and their allies must be willing to'
        ' sacrifice all in an attempt to defeat the'
        ' powerful Thanos before his blitz of devastation'
        ' and ruin puts an end to the universe.'
    }]

    result = insert(database, table, data, connection)

    global unique_id
    unique_id = result['generated_keys'][0]
async def data_manager(websocket, path):
    logging.info(f"[INFO] Current path: {str(path)}")
    try:
        if path == "/":
            raise Exception(f"No operation defined on the current path."
                            f"\nReceived path: {str(path)}")
        data = await websocket.recv()
        data = json.loads(data)

        identifier = data["id"]
        request_type = data["type"]
        payload = data["payload"]
        time = data["time"]
        logging.info(f"[INFO] Data received: {data}")

        if request_type != RDM_CALL:
            raise Exception(f"Type of service not as expected. Probably a call"
                            f"for other service. Verify logs for further infor"
                            f"mation!")

        if save_audit(identifier, request_type, payload, time):
            pass
        else:
            logging.error(f"[ERROR] Error while trying to save data to audit"
                          f" database. Verify Rethink Data Manager logs for "
                          f"furhter information!")

        try:
            connection = configure_database(ReDB_HOST, ReDB_PORT,
                                            ReDB_DEFAULT_DB, ReDB_USER,
                                            ReDB_PASS)

            if str(path) == op[1]:
                database = payload["database"]
                table = payload["table"]
                payload = payload["data"]
                logging.info(f"[INFO] Inserting data:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tDATA: "
                             f"{payload}")
                result = insert(database, table, payload, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to insert data. "
                                    f"Verify the logs for the full traceback. "
                                    f"The data was not inserted!")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[2]:
                database = payload["database"]
                table = payload["table"]
                logging.info(f"[INFO] Getting all data:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}")
                result = get_all(database, table, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to getting data. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither the table is empty or no table"
                                    f" was found!")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[3]:
                database = payload["database"]
                table = payload["table"]
                identifier = payload["identifier"]
                logging.info(f"[INFO] Getting objetct data:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: "
                             f"{identifier}")
                result = get(database, table, identifier, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to getting data. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither the object doesn't exists or no"
                                    f" table was found!")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[4]:
                database = payload["database"]
                table = payload["table"]
                identifier = payload["identifier"]
                statment = payload["data"]
                logging.info(f"[INFO] Updating object:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: "
                             f"{identifier}\n\tUPDATE STATEMENT: {statment}")
                result = update(database, table, identifier, statment,
                                connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to update object. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither the object doesn't exists or no"
                                    f" table was found!")
                elif result["objects_updated"] == 0:
                    raise Exception(f"[ERROR] Error trying to update object. "
                                    f"Verify the logs for the full traceback.")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[5]:
                database = payload["database"]
                table = payload["table"]
                identifier = payload["identifier"]
                logging.info(f"[INFO] Deleting object:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: "
                             f"{identifier}")
                result = delete(database, table, identifier, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to delete object. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither the object doesn't exists or no"
                                    f" table was found!")
                elif result["objects_deleted"] == 0:
                    raise Exception(f"[ERROR] Error trying to delete object. "
                                    f"Verify the logs for the full traceback.")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[6]:
                database = payload["database"]
                table = payload["table"]
                logging.info(f"[INFO] Deleting all objects:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}")
                result = delete_all(database, table, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to delete objects. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither some error was found or no table"
                                    f" was found!")
                elif result["objects_deleted"] == 0:
                    raise Exception(f"[ERROR] Error trying to delete object. "
                                    f"Verify the logs for the full traceback.")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[7]:
                database = payload["database"]
                table = payload["table"]
                logging.info(f"[INFO] Creating new table:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}")
                result = create_table(database, table, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Unknown error while trying to "
                                    f"create a new table. Verify the logs"
                                    f" for the the full traceback.")
                elif result["tables_created"] == 0:
                    raise Exception(f"[ERROR] This table ({table})"
                                    f" already exists!")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[8]:
                database = payload["database"]
                table = payload["table"]
                statment = payload["filter"]
                statment = json.loads(statment)
                logging.info(f"[INFO] Getting objetct data:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tFILTER: "
                             f"{statment}")
                result = get_where(database, table, statment, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Unknown error while trying to "
                                    f"create a new table. Verify the logs"
                                    f" for the the full traceback.")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            else:
                raise Exception(f"Unknown operation on the current path."
                                f"\nReceived path: {str(path)}")
        except Exception as err:
            if connection:
                disconnect_database(connection)
                raise err
        except KeyboardInterrupt as err:
            disconnect_database(connection)
            raise err
        except ConnectionClosed as err:
            disconnect_database(connection)
            raise err
        except CancelledError as err:
            disconnect_database(connection)
            raise err
    except Exception as err:
        logging.error(f"[ERROR] Error at services/data_manager."
                      f"\nTraceback: {err}")
        await websocket.send(json.dumps(error_msg(path)))
    else:
        pass
Example #3
0
def main():
    from tornado.options import define, options
    define("port", default=8888, help="serve web requests from the given port", type=int)
    define("debug", default=False, help="run server in debug mode", type=bool)
    define("mktables", default=False, help="bootstrap a new sqlite database")
    define("migration", default='', help="run a named database migration")

    define("twitch", default=True, help="Connect to Twitch chat servers")
    define("twitchapi", default=True, help="Connect to Twitch API")
    define("discord", default=True, help="Connect to Discord chat servers")
    define("twitter", default=True, help="Connect to Twitter")
    define("newbot", default=False, help="Generates a Discord server invite link for a new bot instance")

    define("runtests", default=False, help="Run tests")

    tornado.options.parse_command_line()

    if options.mktables:
        from loggers.models import Message, Event
        from commands.models import Quote, Command
        from networks.models import User, Moderator

        from peewee import OperationalError

        for table in [Message, Event, Quote, Command, User, Moderator]:
            try:
                db.create_table(table)
            except OperationalError as e:
                # table (probably/hopefully) exists, dump this into the console 
                warning(e)
                continue

    if options.migration:
        from db import Migrations
        if options.migration not in Migrations:
            error('No migration named "{}", ignoring.'.format(options.migration))

        else:
            info('Attempting migration {}'.format(options.migration))
            return Migrations[options.migration]()

    if options.runtests:
        tornado.testing.main()
        return

    app = App(app_debug=options.debug)

    http_server = tornado.httpserver.HTTPServer(app)
    tornado.platform.asyncio.AsyncIOMainLoop().install()  # uses default asyncio.loop()

    http_server.listen(options.port)
    info('Serving on port %d' % options.port)

    # Discord options:
    ## new bot instance authentication
    if options.newbot:
        from keys import discord_app_id
        from discord_invite import invite_link
        print("Please go to the following link to authorize the bot, then press `Enter`:\n")
        print(invite_link(discord_app_id))
        input("\nPress `Enter` to continue...")

    ## connect to discord 
    if options.discord:
        from networks.deescord import Discord
        app.Discord = Discord()
        app.Discord.application = app

        tornado.ioloop.IOLoop.instance().add_callback(app.Discord.connect)  

    # connect to Twitch API
    if options.twitchapi:
        from networks.twitch import TwitchParser, TwitchAPI
        app.TwitchAPI = TwitchAPI()
        app.TwitchAPI.application = app

        from tornado.httpclient import AsyncHTTPClient
        app.TwitchAPI.client = AsyncHTTPClient()

        tornado.ioloop.IOLoop.instance().add_callback(app.TwitchAPI.connect)  

    # connect to Twitch chat
    if options.twitch:
        app.Twitch = TwitchParser()
        app.Twitch.application = app

        tornado.ioloop.IOLoop.instance().add_callback(app.Twitch.connect)  

    if options.twitter:
        from networks.twatter import Twitter
        app.Twitter = Twitter(app)

        tornado.ioloop.IOLoop.instance().add_callback(app.Twitter.connect)

    # link the Jukebox to the application
    from commands.jukebox import J  # our instance of the Jukebox
    app.Jukebox = J  # on our instance of the Application
    
    tornado.ioloop.IOLoop.instance().start()
Example #4
0
def configure_database():
    connection = connect(ReDB_HOST,
                         ReDB_PORT,
                         user=ReDB_USER,
                         password=ReDB_PASS)
    # Initial Set Up
    drop_table(database, table, connection)
    drop_table(database, empty_table, connection)
    create_table(database, table, connection)
    create_table(database, empty_table, connection)

    data = [
        {
            "name":
            "The Dark Knight Rises",
            "year":
            2012,
            "runtime":
            163,
            "categories": ["adventure", "thriller"],
            "release-date":
            "2012-07-20",
            "director":
            "Christopher Nolan",
            "writer": [
                "Jonathan Nolan",
                "David S. Goyer",
                "Christopher Nolan",
                "Bob Kane",
            ],
            "actors": ["Christian Bale", "Tom Hardy", "Anne Hathway"],
            "storyline":
            "Eight years after the Joker's reign of anarchy,"
            " the Dark Knight, with the help of the enigmatic"
            " Catwoman, is forced from his imposed exile to save"
            " Gotham City, now on the edge of total annihilation,"
            " from the brutal guerrilla terrorist Bane.",
        },
        {
            "name":
            "Avengers: Infinity War",
            "year":
            2018,
            "runtime":
            149,
            "categories": ["Action", "Adventure", "Fantasy"],
            "release-date":
            "2018-04-27",
            "director": ["Anthony Russo", "Joe Russo"],
            "writer": ["Christopher Markus", "Stephen McFeely"],
            "actors": [
                "Robert Downey Jr.",
                "Chris Hemsworth",
                "Mark Ruffalo",
                "Chris Evans",
                "Scarlett Johansson",
                "Benedict Cumberbatch",
            ],
            "storyline":
            "The Avengers and their allies must be willing to"
            " sacrifice all in an attempt to defeat the"
            " powerful Thanos before his blitz of devastation"
            " and ruin puts an end to the universe.",
        },
    ]

    result = insert(database, table, data, connection)

    global unique_id
    unique_id = result.get("generated_keys")[0]
Example #5
0
def test_unsuccessful_create_table(rethink_connect):
    result = create_table(database, empty_table, rethink_connect)
    assert result is not None
    assert len(result) == 1
    assert result["tables_created"] == 0
Example #6
0
def test_successful_create_table(rethink_connect):
    result = create_table(database, empty_table, rethink_connect)
    assert result is not None
    assert len(result) is 1
    assert result['tables_created'] is 1