-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.py
112 lines (85 loc) · 5.17 KB
/
tools.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
"""main.py : Run any operation needed in the mongoDB """
__author__ = "Marta Huertas"
__version__ = "0.1"
__maintainer__ = "Marta Huertas"
__email__ = "[email protected]"
__status__ = "development"
# Import packages
import sys
from pymongo import MongoClient
import conf
from source import insert, update_value, restore_value, rename_field, new_field, remove_field, mongoConnection, upsert
# Functions
def print_help():
print('USAGE:')
print('This script combines different tools to manage the BioMongoDB in EGA')
print('First thing to do is to write your needs in the conf.py file')
print('Follow the guidelines in that file.')
def connect_mongo():
try:
# Connect to MongoDB
client = MongoClient(mongoConnection.mongo_host, mongoConnection.mongo_port, username=mongoConnection.username, password=mongoConnection.password, authSource=mongoConnection.auth_source)
# Acces the database:
db = client[conf.database_name]
# If connection successful, print success message
print("Connection to BioMongoDB established.")
return db
except ConnectionError as e:
# If connection fails, print error message
print("Failed to connect to MongoDB:", e)
def run_operation():
"""
Run operations in the BioMongoDB by reading the conf file
"""
# Get database connection:
db = connect_mongo()
if conf.operation == 'insert' and conf.json_documents != '':
insert.insertDocuments(conf.operation, db, conf.collection_name, conf.json_documents, conf.name, conf.method)
elif conf.operation == 'update_one' and conf.update_field != '' and conf.new_value != '':
update_value.updateOne(conf.operation, db, conf.collection_name, conf.update_criteria, conf.update_field, conf.new_value, conf.name, conf.method)
elif conf.operation == 'update_all' and conf.update_field != '' and conf.new_value != '':
update_value.updateAll(conf.operation, db, conf.collection_name, conf.update_field, conf.new_value, conf.name, conf.method)
elif conf.operation == 'update_with_file' and conf.update_file != '':
update_value.updateFile(conf.operation, db, conf.collection_name, conf.update_file, conf.name, conf.method)
elif conf.operation == 'upsert_one' and conf.update_field != '' and conf.new_value != '':
upsert.upsertOne(conf.operation, db, conf.collection_name, conf.update_criteria, conf.update_field, conf.new_value, conf.name, conf.method)
elif conf.operation == 'upsert_all' and conf.update_field != '' and conf.new_value != '':
upsert.upsertAll(conf.operation, db, conf.collection_name, conf.update_field, conf.new_value, conf.name, conf.method)
elif conf.operation == 'upsert_with_file' and conf.update_file != '':
upsert.upsertFile(conf.operation, db, conf.collection_name, conf.update_file, conf.name, conf.method)
elif conf.operation == 'restore_one' and conf.restore_criteria != '' and conf.log_id != '':
restore_value.restoreOne(conf.operation, db, conf.collection_name, conf.restore_criteria, conf.log_id, conf.name, conf.method)
elif conf.operation == 'restore_all' and conf.log_id != '':
restore_value.restoreAll(conf.operation, db, conf.collection_name, conf.log_id, conf.name, conf.method)
elif conf.operation == 'add_empty_field' and conf.new_field != '':
new_field.addNullField(conf.operation, db, conf.collection_name, conf.new_field, conf.name, conf.method)
elif conf.operation == 'add_field_with_file' and conf.new_field_file != '':
new_field.addFieldFile(conf.operation, db, conf.collection_name, conf.new_field_file, conf.name, conf.method)
elif conf.operation == 'rename_field' and conf.field_name != '' and conf.new_field_name != '':
rename_field.renameField(conf.operation, db, conf.collection_name, conf.field_name, conf.new_field_name, conf.name, conf.method)
elif conf.operation == 'remove_field' and conf.field_to_remove != '':
remove_field.removeField(conf.operation, db, conf.collection_name, conf.field_to_remove, conf.name, conf.method)
else:
print('Something is missing in the conf.py file')
def main():
if conf.operation == '' and conf.database_name == '' and conf.collection_name == '' and conf.name == '' and conf.method == '':
# First print help message just in case.
print_help()
elif conf.operation == '' or conf.operation not in ['insert', 'update_one', 'update_all', 'update_with_file', 'upsert_one', 'upsert_all', 'upsert_with_file', 'restore_one', 'restore_all', 'add_empty_field', 'add_field_with_file', 'rename_field', 'remove_field']:
print("Operation is missing or wrong.")
elif conf.database_name == '':
print("Database is missing.")
elif conf.collection_name == '':
print("Collection name is missing.")
elif conf.name == '':
print("Your name is missing.")
elif conf.method == '':
print("The method you used to obtain the information is missing.")
else:
print(f'Operation: {conf.operation}')
print(f'Database: {conf.database_name}')
# Run the operation determined in conf:
run_operation()
if __name__ == main():
main()