-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongo.py
42 lines (33 loc) · 1.57 KB
/
Mongo.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
#######################################################################
# Mongo
# Class to connect to database and collections
#######################################################################
import pymongo
from pymongo import MongoClient
from Shared import settings
import Helpers
class Mongo:
def __init__(self, nameOverride="Mongo"):
self.client = None
self.database = None
self.collection_games = None
self.collection_games_meta = None
self.collection_collector = None
self.logger = Helpers.Logger(nameOverride, Helpers.mongoLogColor)
def connect_test(self):
self.connect(True)
def connect(self, useTestingCollections = False):
testString = "test" if useTestingCollections else "live"
self.logger.log("Connecting to " + testString + " database")
try:
self.client = MongoClient(settings.mongo.connectionString)
self.database = self.client[settings.mongo.databaseName]
testPrefix = settings.mongo.collections.testingPrefix if useTestingCollections else ""
self.collection_games = self.database[testPrefix + settings.mongo.collections.games]
self.collection_games_meta = self.database[testPrefix + settings.mongo.collections.gamesMeta]
self.collection_collector = self.database[testPrefix + settings.mongo.collections.collector]
self.logger.log("Connection Ok")
except pymongo.errors.PyMongoError as e:
self.logger.log("Connection Failure: " + str(e))
return False
return True