Remove dependency from bigchaindb.config

This commit is contained in:
vrde 2016-12-06 10:03:33 +01:00 committed by Sylvain Bellemare
parent 87c7228bc4
commit a08ba013a0

View File

@ -1,9 +1,11 @@
import time import time
import logging
import rethinkdb as r import rethinkdb as r
from bigchaindb.db import Connection from bigchaindb.backend.connection import Connection
import bigchaindb
logger = logging.getLogger(__name__)
class RethinkDBConnection(Connection): class RethinkDBConnection(Connection):
@ -14,19 +16,19 @@ class RethinkDBConnection(Connection):
more times to run the query or open a connection. more times to run the query or open a connection.
""" """
def __init__(self, host=None, port=None, db=None, max_tries=3): def __init__(self, host, port, dbname, max_tries=3):
"""Create a new Connection instance. """Create a new Connection instance.
Args: Args:
host (str, optional): the host to connect to. host (str, optional): the host to connect to.
port (int, optional): the port to connect to. port (int, optional): the port to connect to.
db (str, optional): the database to use. dbname (str, optional): the name of the database to use.
max_tries (int, optional): how many tries before giving up. max_tries (int, optional): how many tries before giving up.
""" """
self.host = host or bigchaindb.config['database']['host'] self.host = host
self.port = port or bigchaindb.config['database']['port'] self.port = port
self.db = db or bigchaindb.config['database']['name'] self.dbname = dbname
self.max_tries = max_tries self.max_tries = max_tries
self.conn = None self.conn = None
@ -38,7 +40,7 @@ class RethinkDBConnection(Connection):
""" """
if self.conn is None: if self.conn is None:
self.connect() self._connect()
for i in range(self.max_tries): for i in range(self.max_tries):
try: try:
@ -47,13 +49,12 @@ class RethinkDBConnection(Connection):
if i + 1 == self.max_tries: if i + 1 == self.max_tries:
raise raise
else: else:
self.connect() self._connect()
def connect(self): def _connect(self):
for i in range(self.max_tries): for i in range(self.max_tries):
try: try:
self.conn = r.connect(host=self.host, port=self.port, self.conn = r.connect(host=self.host, port=self.port, db=self.dbname)
db=self.db)
except r.ReqlDriverError as exc: except r.ReqlDriverError as exc:
if i + 1 == self.max_tries: if i + 1 == self.max_tries:
raise raise