Skip to content

Commit 02eb9a4

Browse files
committed
changes to database connection code to support "authsource" parameter in db file
1 parent 760af82 commit 02eb9a4

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

atomate/feff/database.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
class FeffCalcDb(CalcDb):
2020

2121
def __init__(self, host="localhost", port=27017, database="feff", collection="tasks", user=None,
22-
password=None):
23-
super(FeffCalcDb, self).__init__(host, port, database, collection, user, password)
22+
password=None, **kwargs):
23+
super(FeffCalcDb, self).__init__(host, port, database, collection,
24+
user, password, **kwargs)
2425

2526
def build_indexes(self, indexes=None, background=True):
2627
_indexes = indexes if indexes else ["structure.formula"]

atomate/lammps/database.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
class LammpsCalcDb(CalcDb):
2222

2323
def __init__(self, host="localhost", port=27017, database="lammps", collection="tasks",
24-
user=None, password=None):
25-
super(LammpsCalcDb, self).__init__(host, port, database, collection, user, password)
24+
user=None, password=None, **kwargs):
25+
super(LammpsCalcDb, self).__init__(host, port, database, collection,
26+
user, password, **kwargs)
2627

2728
def build_indexes(self, indexes=None, background=True):
2829
indexes = indexes or []

atomate/qchem/database.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ def __init__(self,
2525
database="qchem",
2626
collection="tasks",
2727
user=None,
28-
password=None):
28+
password=None,
29+
**kwargs):
2930
super(QChemCalcDb, self).__init__(host, port, database, collection,
30-
user, password)
31+
user, password, **kwargs)
3132

3233
def build_indexes(self, indexes=None, background=True):
3334
"""

atomate/utils/database.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525

2626
class CalcDb(six.with_metaclass(ABCMeta)):
2727

28-
def __init__(self, host, port, database, collection, user, password):
28+
def __init__(self, host, port, database, collection, user, password, **kwargs):
2929
self.host = host
3030
self.db_name = database
3131
self.user = user
3232
self.password = password
3333
self.port = int(port)
34+
3435
try:
35-
self.connection = MongoClient(self.host, self.port)
36+
self.connection = MongoClient(host=self.host, port=self.port,
37+
username=self.user,
38+
password=self.password, **kwargs)
3639
self.db = self.connection[self.db_name]
3740
except:
3841
logger.error("Mongodb connection failed")
@@ -122,5 +125,9 @@ def from_db_file(cls, db_file, admin=True):
122125
user = creds.get("readonly_user")
123126
password = creds.get("readonly_password")
124127

128+
kwargs = {}
129+
if "authsource" in creds:
130+
kwargs["authsource"] = creds["authsource"]
131+
125132
return cls(creds["host"], int(creds["port"]), creds["database"], creds["collection"],
126-
user, password)
133+
user, password, **kwargs)

atomate/utils/utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,23 @@ def get_uri(dir_name):
337337

338338
def get_database(config_file=None, settings=None, admin=False, **kwargs):
339339
d = loadfn(config_file) if settings is None else settings
340-
conn = MongoClient(host=d["host"], port=d["port"], **kwargs)
341-
db = conn[d["database"]]
340+
342341
try:
343342
user = d["admin_user"] if admin else d["readonly_user"]
344343
passwd = d["admin_password"] if admin else d["readonly_password"]
345-
db.authenticate(user, passwd)
346344
except (KeyError, TypeError, ValueError):
347345
logger.warning("No {admin,readonly}_user/password found in config. file, "
348346
"accessing DB without authentication")
347+
user = None
348+
passwd = None
349+
350+
if "authsource" in d and "authsource" not in kwargs:
351+
kwargs["authsource"] = d["authsource"]
352+
353+
conn = MongoClient(host=d["host"], port=d["port"], username=user,
354+
password=passwd, **kwargs)
355+
db = conn[d["database"]]
356+
349357
return db
350358

351359

atomate/vasp/database.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class VaspCalcDb(CalcDb):
3434
"""
3535

3636
def __init__(self, host="localhost", port=27017, database="vasp", collection="tasks", user=None,
37-
password=None):
38-
super(VaspCalcDb, self).__init__(host, port, database, collection, user, password)
37+
password=None, **kwargs):
38+
super(VaspCalcDb, self).__init__(host, port, database, collection, user,
39+
password, **kwargs)
3940

4041
def build_indexes(self, indexes=None, background=True):
4142
"""

0 commit comments

Comments
 (0)