Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use utilix for client #356

Merged
merged 5 commits into from
Nov 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 35 additions & 12 deletions strax/corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,44 @@ class CorrectionsInterface:
this means a unique set of correction maps.
"""

def __init__(self, host='127.0.0.1', username=None, password=None,
database_name='corrections'):
def __init__(self, client=None, database_name='corrections',
host=None, username=None, password=None,):
"""
:param host: DB host
:param username: DB username
:param password: DB password
:param database_name: DB name
Start the CorrectionsInterface. To initialize you need either:
- a pymongo.MongoClient (add as argument client) OR
- the credentials and url to connect to the pymongo instance
one wants to be using.
:param client: pymongo client, a pymongo.MongoClient object
:param database_name: Database name

(optional if client is not provided)
:param host: DB host or IP address e.g. "127.0.0.1"
:param username: Database username
:param password: Database password
"""
# Let's see if someone just provided a pymongo.MongoClient
if (client is not None) and (
host is None and
username is None and
password is None):
if not isinstance(client, pymongo.MongoClient):
raise TypeError(f'{client} is not a pymongo.MongoClient.')
self.client = client
# In this case, let's just initialize a new pymongo.MongoClient
elif (client is None) and (
host is not None and
username is not None
and password is not None):
self.client = pymongo.MongoClient(host=host,
username=username,
password=password)
else:
# Let's not be flexible with our inputs to prevent later
# misunderstandings because someone thought to be handling a
# different client than anticipated.
raise ValueError('Can only init using *either* the "client" or the '
'combination of "host+username+password", not both')

self.host = host
self.username = username
self.password = password
self.client = pymongo.MongoClient(host=self.host,
username=self.username,
password=self.password)
self.database_name = database_name

def list_corrections(self):
Expand Down