-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit_backend.py
97 lines (79 loc) · 3.21 KB
/
credit_backend.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
#!/usr/bin/env python3 -u
import argparse
import logging
from backend.log_config import set_log_level
from backend.db_handler import db_handler
from backend.excel_handler import get_data_from_excel
logger = logging.getLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser(
prog="Credit Display", usage="Display credit expanses")
parser.add_argument('--sheet_name', help='Original excel file from credit card company',
default="credit_expenses.xls", required=True)
parser.add_argument('--db_user', help='User name to connect to database',
required=True)
parser.add_argument('--db_pass', help='Password to connect to database',
required=True)
parser.add_argument('--db_name', help='Database name', required=True)
parser.add_argument('--collection', help='Collection name', required=True)
parser.add_argument('--log_level', default='INFO', choices=('debug', 'info', 'warning', 'error', 'critical'),
help='Level of logging', required=False)
return parser.parse_args()
def function_caller(choice):
return {
'i': 'insert_transactions',
'rmc': 'remove_collection_from_db',
'gsa': 'get_shop_and_amount',
'gcl': 'get_collections_list',
'usg': 'update_shop_group',
'q': 'exit'
}.get(choice, None)
def get_parameters(function_name):
return {
'insert_transactions': 'transactions_arr',
'remove_collection_from_db': 'col',
'get_shop_and_amount': '',
'get_collections_list': '',
'usg': 'get_input_shop_group',
'q': '0'
}.get(function_name, None)
def help_print():
print("Welcome to Credit Display!\nPlease choose what to do:")
print("'i' - Inserts transactions from excel to current collection\n"
"'gsa' - Print shop and amount\n"
"'rmc' - Remove current collection\n"
"'gcl' - Get a list of all collections in DB\n"
"'usg' - Update the shop group collection\n"
"'q' - Exit the program")
def main():
args = parse_args()
set_log_level(args.log_level)
col = args.collection
db = db_handler(
"mongodb+srv://{0}:{1}@creditdata-xurnm.mongodb.net/test".format(args.db_user, args.db_pass))
db.connect_to_db(args.db_name)
db.connect_to_collection(args.collection)
transactions_arr = get_data_from_excel(args.sheet_name)
while True:
help_print()
choice = input("Please enter you choice:\n")
func = function_caller(choice)
arg = get_parameters(func)
if func:
if func == 'exit':
exit(0)
if func == 'update_shop_group':
shop = input("Please enter the shop name\n")
group = input("Please enter the group name\n")
getattr(db, func)(shop, group)
break
if arg:
ans = getattr(db, func)(eval(arg))
else:
ans = getattr(db, func)()
logger.info("Function {0} parameters: {1}, Returned:\n{2}".format(
func, arg, ans))
else:
logger.info("Function not found!")
if __name__ == "__main__":
main()