def restart_db(): """ This function deletes all tables in the post request and initializes them. - method: PUT - route: /retart_db - payload example: [ "rules", "players" "quests", "users" ] """ for table in request.json: if table not in ("games", "players", "quests", "users"): return make_response( "Table {} should be 'games', 'players', 'quests' or 'users' !". format(table), 400) if table in r.RethinkDB().db('test').table_list().run(): r.RethinkDB().table_drop(table).run() # initialize table r.RethinkDB().table_create(table).run() return make_response("", 204)
def quest_get(game_id, quest_number): game = r.RethinkDB().table("games").get(game_id).run() if not game: return make_response("Game's id {} does not exist !".format(game_id), 400) quest = r.RethinkDB().table("quests").get(game["quests"][quest_number]).run() if quest["status"] is None: return make_response("The vote number {} is not finished !".format(quest_number), 400) return jsonify(quest)
def __init__(self, **options): """Initializes the main parts of the bot.""" # Initializes parent class super().__init__(self._get_prefix_new, **options) # Setup self.extensions_list: List[str] = [] with open('config.json') as f: self.config = json.load(f) # Info self.prefix: List[str] = self.config['PREFIX'] self.version: str = self.config['VERSION'] self.description: str = self.config['DESCRIPTION'] self.repo: str = self.config['REPO'] self.support_server: str = self.config['SERVER'] self.perms: int = self.config['PERMS'] # Toggles self.maintenance: bool = self.config['MAINTENANCE'] self.case_insensitive: bool = self.config['CASE_INSENSITIVE'] self.custom_help: bool = self.config['CUSTOM_HELP'] self.mention_assist: bool = self.config['MENTION_ASSIST'] self.prefixless_dms: bool = self.config['PREFIXLESS_DMS'] # RethinkDB if self.config['RETHINK']['DB']: self.re = rethinkdb.RethinkDB() self.re.set_loop_type('asyncio') self.rdb: str = self.config['RETHINK']['DB'] self.conn = None self.rtables: List[str] = []
def before_request(): try: rdb = r.RethinkDB() rdb.connect("localhost", 28015).repl() g.rdb_conn = rdb except ReqlDriverError: abort(503, "Database not connected")
def quest_put(game_id, quest_number): game = r.RethinkDB().table("games").get(game_id).run() if not game: return make_response("Game's id {} does not exist !".format(game_id), 400) quest = r.RethinkDB().table("quests").get(game["quests"][quest_number]).run() if game["nb_quest_unsend"] == 5: return make_response("Game is over because 5 consecutive laps have been passed : Red team won !", 400) if "result" in game: return make_response("Game is over !", 400) if game["current_quest"] != quest_number: return make_response("Vote number {} is already established !".format(quest_number), 400) if ("status" in quest or "votes" in quest) and quest["status"] != None: return make_response("Only vote number {} is allowed !".format(game["current_quest"]), 400) if game["nb_quest_unsend"] == 5: return make_response("Game is over because 5 consecutive laps have been passed : Red team won !", 400) for player_id in request.json: if player_id not in game["players"]: return make_response("Player {} is not in this game !".format(player_id), 400) id_quest_number = game["quests"][quest_number] nb_players_to_send = db_get_value("quests", id_quest_number, "nb_players_to_send") if len(request.json) != nb_players_to_send: return make_response( "Quest number {} needs {} votes !".format(quest_number, nb_players_to_send), 400 ) if "status" in quest: r.RethinkDB().table("quests").get(id_quest_number).update({"votes": None}).run() return jsonify( r.RethinkDB().table("quests").get(id_quest_number).update( { "votes": {player_id: None for player_id in request.json}, "status": None }, return_changes=True )["changes"][0]["new_val"].run() )
def last_known_blocks(self, count): """Fetches the ids of the specified number of most recent blocks """ r = re.RethinkDB() cursor = r.db(self._name).table('blocks')\ .order_by('block_num')\ .get_field('block_id')\ .run(self._conn) return list(cursor)[-count:]
def dbSetup(): r = rethinkdb.RethinkDB() connection = r.connect(host=RDB_HOST, port=RDB_PORT) try: r.db_create(TODO_DB).run(connection) r.db(TODO_DB).table_create('todos').run(connection) print('Database setup completed') except RqlRuntimeError: print('Database already exists') finally: connection.close()
def view(table_name): """ This function visualize a table depending on the input <table_name>. - method: GET - route: /view/<table_name> (table_name is rules, games and players) - payload example: - response example: { "rules": [ { "blue": 3, "echec1": 1, "echec2": 1, "echec3": 1, "echec4": 1, "echec5": 1, "id": "5fb71032-7dad-4e48-bb14-7f4fb7c262fa", "nb_player": 5, "quest1": 2, "quest2": 3, "quest3": 2, "quest4": 3, "quest5": 3, "red": 2 }, { ... }, { "blue": 6, "echec1": 1, "echec2": 1, "echec3": 1, "echec4": 2, "echec5": 1, "id": "f9bffa54-dc75-45f2-b2f8-51ad0c4f397f", "nb_player": 10, "quest1": 3, "quest2": 4, "quest3": 4, "quest4": 5, "quest5": 5, "red": 4 } ] } """ response = {table_name: []} cursor = r.RethinkDB().table(table_name).run() for document in cursor: response[table_name].append(document) return jsonify(response)
async def open_connections(app): LOGGER.warning('opening database connection') r = rdb.RethinkDB() r.set_loop_type('asyncio') app.config.DB_CONN = await r.connect(host=app.config.DB_HOST, port=app.config.DB_PORT, db=app.config.DB_NAME) app.config.VAL_CONN = Connection(app.config.VALIDATOR_URL) LOGGER.warning('opening validator connection') app.config.VAL_CONN.open()
def __init__(self): self.r = rethinkdb.RethinkDB() self.conn = self.r.connect(db='IR') self.conn.use('IR') if 'index' not in list(self.r.table_list().run(self.conn)): self.r.table_create('index', primary_key='word').run(self.conn) self.dir_prefix_tree, self.rev_prefix_tree = {None: {}}, {None: {}} self.aux_index, self.soundex_index = {}, {} self.produce_stuff(path='../data/', dir_tree=self.dir_prefix_tree, rev_tree=self.rev_prefix_tree, sound_index=self.soundex_index)
def rethinkdb_access_test(aggressive, ip, port): r = rethinkdb.RethinkDB() try: r.connect(ip, int(port)) return True, None except rethinkdb.errors.ReqlAuthError: if aggressive: try: r.connect(ip, int(port), user="******", password="******") return True, ("admin", "admin") except rethinkdb.errors.ReqlAuthError: return False, None else: return False, None
def quest_unsend(game_id): """This function sends new quest of the game <game_id>. - method: POST - route: /<game_id>/quest_unsend - payload example: None - response example: board """ if r.RethinkDB().table("games").get(game_id).run()["nb_quest_unsend"] < 4: update_current_id_player(game_id) db_update_value("games", game_id, "nb_quest_unsend", db_get_value("games", game_id, "nb_quest_unsend") + 1) return game_get(game_id) if r.RethinkDB().table("games").get(game_id).run()["nb_quest_unsend"] == 4: db_update_value("games", game_id, "nb_quest_unsend", db_get_value("games", game_id, "nb_quest_unsend") + 1) r.RethinkDB().table("games").get(game_id).update( {"result": {"status": False}}, return_changes=True)["changes"][0]["new_val"].run() return game_get(game_id) if r.RethinkDB().table("games").get(game_id).run()["nb_quest_unsend"] == 5: return make_response("Game is over because 5 consecutive laps have been passed : Red team won !", 400)
def rethinkdb_access_test(aggressive, ip, port): r = rethinkdb.RethinkDB() try: r.connect(ip, int(port)) return True, None except rethinkdb.errors.ReqlAuthError: if aggressive: try: r.connect(ip, int(port), user="******", password="******") return True, ("admin", "admin") except rethinkdb.errors.ReqlAuthError: return False, None else: return False, None except rethinkdb.errors.ReqlDriverError: raise DatabaseConnectionError("No rethinkDB instance found running at this address")
def getlista(kw): r = rdb.RethinkDB() con = Continuos() if kw['message'].get('order'): print(f"kw XXX : {kw['message']['order'].items()}") if kw['message'].get('order'): order = [] for k, v in kw['message']['order'].items(): order.append(getattr(r, v)(k)) kw['message']['order'] = order print(f'kw2: {kw}') dat = con.consultar(kw) del con return dat
def game_get(game_id): """This function visualize the game of the <game_id>.""" if not game_id: return jsonify(get_table("games")) game = r.RethinkDB().table("games").get(game_id).run() if not game: return make_response("Game's id {} does not exist !".format(game_id), 400) game.update({ "players": resolve_key_id(table="players", list_id=game["players"]), "quests": resolve_key_id(table="quests", list_id=game["quests"]) }) return jsonify(game)
def extract_rethinkdb(ip, port, credentials=None): result = {} r = rethinkdb.RethinkDB() if credentials is not None: r.connect(ip, int(port), user=credentials[0], password=credentials[1]).repl() else: r.connect(ip, int(port)).repl() databases = r.db_list().run() for database in databases: if database != "rethinkdb": result[database] = {} tables = r.db(database).table_list().run() for table in tables: result[database][table] = [] cursor = r.db(database).table(table).run() for doc in cursor: result[database][table].append(doc) return result
def post_mp3(game_id): """This function creates the mp3file depending on roles of players. - method: GET - route: /<game_id>/mp3 - payload example: - response example: response.mpga """ # find role of each player list_roles = [] for player_id in bdd_get_value("games", game_id, "players"): list_roles.append( list(r.RethinkDB().table("players").filter({ "id": player_id }).run())[0]["role"]) # create mp3file create_mp3(list_roles) return send_file("resources/roles.mp3", attachment_filename='roles.mp3', mimetype='audio/mpeg')
def drop_fork(self, block_num): """Deletes all resources from a particular block_num """ r = re.RethinkDB() block_results = r.db(self._name).table('blocks')\ .filter(lambda rsc: rsc['block_num'].ge(block_num))\ .delete()\ .run(self._conn) resource_results = r.db(self._name).table_list()\ .for_each( lambda table_name: r.branch( r.eq(table_name, 'blocks'), [], r.eq(table_name, 'auth'), [], r.db(self._name).table(table_name) .filter(lambda rsc: rsc['start_block_num'].ge(block_num)) .delete()))\ .run(self._conn) return {k: v + resource_results[k] for k, v in block_results.items()}
from api.utils.errors import DatabaseProcessError, ValidationError from datetime import datetime import rethinkdb as rdb from jose import jwt from jose.exceptions import JWTError from passlib.hash import pbkdf2_sha256 r = rdb.RethinkDB() connection = r.connect(db='biruda') class RethinkDBModel(object): @classmethod def find(cls, id): return r.table(cls._table).get(id).run(conn) @classmethod def filter(cls, predicate): return list(r.table(cls._table).filter(predicate).run(conn)) @classmethod def update(cls, id, fields): status = r.table(cls._table).get(id).update(fields).run(conn) if status['errors']: raise DatabaseProcessError("Could not complete the update action") return True @classmethod def delete(cls, id): status = r.table(cls._table).get(id).delete().run(conn) if status['errors']:
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ------------------------------------------------------------------------------ import rethinkdb as re from rethinkdb.errors import ReqlNonExistenceError from api.errors import ApiBadRequest from db.common import fetch_holdings from db.common import fetch_latest_block_num r = re.RethinkDB() async def fetch_all_account_resources(conn): return await r.table('accounts')\ .filter((fetch_latest_block_num() >= r.row['start_block_num']) & (fetch_latest_block_num() < r.row['end_block_num']))\ .map(lambda account: account.merge( {'publicKey': account['public_key']}))\ .map(lambda account: account.merge( {'holdings': fetch_holdings(account['holdings'])}))\ .map(lambda account: (account['label'] == "").branch( account.without('label'), account))\ .map(lambda account: (account['description'] == "").branch( account.without('description'), account))\ .without('public_key', 'delta_id',
def before_request(): try: r = rethinkdb.RethinkDB() g.rdb_conn = r.connect(host=RDB_HOST, port=RDB_PORT, db=TODO_DB) except RqlDriverError: abort(503, 'No database connection could be established.')
import os import re import rethinkdb as r from datetime import datetime from flask import current_app from api.models.RethinkDBModel import RethinkDBModel rdb = r.RethinkDB() conn = rdb.connect(db=current_app.config['DATABASE_NAME']) class File(RethinkDBModel): _table = 'files' @classmethod def create(cls, **kwargs): name = kwargs.get('name') size = kwargs.get('size') uri = kwargs.get('uri') parent = kwargs.get('parent') creator = kwargs.get('creator') siteName = kwargs.get('siteName') url = kwargs.get('url') # Direct parent ID parent_id = '0' if parent is None else parent['id'] doc = { 'name': name, 'size': size,
import rethinkdb as r # host = "127.0.0.1" # port = 8080 # with r.RethinkDB().connect(host='localhost', port=28015) as conn: # r.RethinkDB().table_create("rules").run(conn) # r.RethinkDB().table("rules").insert([ # {"nb_player": 5, "BLUE": 3, "RED": 2, "q1": 2, "q2": 3, "q3": 2, "q4": 3, "q5": 3}, # {"nb_player": 6, "BLUE": 4, "RED": 2, "q1": 2, "q2": 3, "q3": 4, "q4": 3, "q5": 4}, # {"nb_player": 7, "BLUE": 4, "RED": 3, "q1": 2, "q2": 3, "q3": 3, "q4": 4, "q5": 4}, # {"nb_player": 8, "BLUE": 5, "RED": 3, "q1": 3, "q2": 4, "q3": 4, "q4": 5, "q5": 5}, # {"nb_player": 9, "BLUE": 6, "RED": 3, "q1": 3, "q2": 4, "q3": 4, "q4": 5, "q5": 5}, # {"nb_player": 10, "BLUE": 6, "RED": 4, "q1": 3, "q2": 4, "q3": 4, "q4": 5, "q5": 5}]).run(conn) r.RethinkDB().connect('localhost', 28015).repl() r.RethinkDB().db('test').table_create('tv_shows').run() r.RethinkDB().table('tv_shows').insert({'name': 'Star Trek TNG'}).run()
# -*- coding: utf-8 -*- """ rdb.registry ~~~~~~~~~~~~ Keep a record of all the models registered. """ import os import rethinkdb from rethinkdb.errors import ReqlOpFailedError from ..helpers.console import console from .connection import rconnect r = rethinkdb.RethinkDB() class RegistryError(Exception): pass class ModelRegistry(object): def __init__(self): self._models = {} self._tables = [] self._initialsed = False def get_class(self, name): if name in self._models: return self._models[name] else:
def get_table(self, table_name): """Returns a rethink table query, which can be added to, and eventually run with run_query """ r = re.RethinkDB() return r.db(self._name).table(table_name)
from discord.ext import commands from utils import settings, lang, paginator, presence import discord, asyncio, aiohttp import time import traceback import json, io import textwrap from contextlib import redirect_stdout from collections import Counter, OrderedDict from operator import itemgetter from subprocess import PIPE import sys import datadog, logging import websockets.exceptions as ws import rethinkdb as r r = r.RethinkDB() class Dev(commands.Cog): def __init__(self, bot): self.bot = bot if not settings.UseBetaBot: bot.loop.create_task(self.background_dd_report()) bot.loop.create_task(self.daily_bot_stats()) def cleanup_code(self, content): if content.startswith('```') and content.endswith('```'): return '\n'.join(content.split('\n')[1:-1]) return content.strip('` \n') def owner_only(ctx):
def insert(self, table_name, docs): """Inserts a document or a list of documents into the specified table in the database """ r = re.RethinkDB() return r.db(self._name).table(table_name).insert(docs).run(self._conn)
def fetch(self, table_name, primary_id): """Fetches a single resource by its primary id """ r = re.RethinkDB() return r.db(self._name).table(table_name)\ .get(primary_id).run(self._conn)
def connect(self): """Initializes a connection to the database """ r = re.RethinkDB() LOGGER.debug('Connecting to database: %s:%s', self._host, self._port) self._conn = r.connect(host=self._host, port=self._port)
def disconnect(self): r=re.RethinkDB() """Closes the connection to the database """ LOGGER.debug('Disconnecting from database') self._conn.close()