forked from kunalshah03/PackReview_Part2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
43 lines (33 loc) · 1.35 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
utils.py
This module provides utility functions for interacting with a MongoDB database.
Functions:
- get_db(is_test=False): Connects to the MongoDB database and returns the appropriate
database instance based on the test flag.
Usage:
Call `get_db()` to retrieve the main project database, or `get_db(is_test=True)` to retrieve the
test database. The MongoDB password should be stored in a configuration file named `config.ini`
located in the same directory as this script.
Notes:
Ensure that the `config.ini` file contains the correct password for the MongoDB connection.
"""
import os
import sys
from pymongo import MongoClient
def get_db(is_test=False):
"""Connect to the MongoDB database and return the appropriate database based on the test flag.
Args:
is_test (bool): If True, return the test database. Defaults to False.
Returns:
Database: The MongoDB database instance.
"""
with open(os.path.join(sys.path[0], "config.ini"), "r", encoding="utf-8") as f:
content = f.readlines()
mongo_uri = (
"mongodb+srv://anishd1910:"
f"{content[0].strip()}@cluster0.oagwk.mongodb.net/"
"?retryWrites=true&w=majority&appName=Cluster0"
)
with MongoClient(mongo_uri) as client:
client = MongoClient(mongo_uri)
return client.SETestProj if is_test else client.SEProj2