-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminio.py
198 lines (148 loc) · 6.68 KB
/
minio.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Import different libraries
import json, os
from minio import Minio
from minio.error import (ResponseError, BucketAlreadyOwnedByYou,
BucketAlreadyExists)
# smtplib module included in python by default for email purpose
from string import Template
import smtplib, json
# import more necessary packages for sending email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Initialize the client server with an endpoint, access and secret keys.
minioClient = Minio(os.environ['SERVER_NAME'],
access_key=os.environ['ACCESS_KEY'],
secret_key=os.environ['SECRET_KEY'],
secure=False)
class mockTest:
# Save the outputs of user as in json file
# function for doing the make_bucket API call and taking the inputs
def input(self):
try:
username = 'user-id'
bucket_name = username+"-bucket"
minioClient.make_bucket(bucket_name)
except BucketAlreadyOwnedByYou as err:
pass
except BucketAlreadyExists as err:
pass
except ResponseError as err:
print(err)
inp = {} # to store the outputs produced by the user as in dictionary
count = 1 # counter for various different keys in dictionary
while True:
print("Enter answer:")
x=os.popen(input()).read().split('\n')[0]
if x=='end':
break
inp['Q ' + str(count)] = x
count+=1
# convert all the output data into json file
with open('course-title.json', 'w') as json_file:
json.dump(inp, json_file)
# Verification of the format in console and display it
y = json.dumps(inp)
print(y)
return;
# Saving concept goes here like json to Minio interaction
def save(self):
try:
bucket_name = "user-id-bucket"
object_name = "course-title.json"
file_path = "course-title.json"
minioClient.fput_object(bucket_name, object_name, file_path)
except ResponseError as err:
print(err)
return;
# Process the data which is retrieved from the minio server to evaluate the result
# Retrieving concept goes here like Minio to json interaction
def process(self):
try:
username = "user-id" # fetch user id from db
bucket_name = username+"-bucket" # refer to the current user bucket by her id
object_name = "course-title.json" # all the submissions with same name in different bucket names
file_path = "course-title-fetch.json"
minioClient.fget_object(bucket_name, object_name, file_path)
except ResponseError as err:
print(err)
data = open("course-title-fetch.json", 'r')
data = json.load(data)
sol = open('course-title-key.json', 'r')
sol = json.load(sol)
count=1
total_score=0
for i in range(0,len(data)):
if(sol["Q "+str(count)] == data["Q "+str(count)]):
data["Q " +str(count)]="correct"
total_score+=1
else:
data["Q " +str(count)]="incorrect"
count+=1
# to store the final score of candidate
data["total_score"]=total_score
# convert into json
with open('course-title-result.json', 'w') as json_file:
json.dump(data, json_file)
# Saving concept goes here like json to Minio interaction
try:
bucket_name = "user-id-bucket"
object_name = "course-title-result.json"
file_path = "course-title-result.json"
minioClient.fput_object(bucket_name, object_name, file_path)
except ResponseError as err:
print(err)
return;
# Program to read contacts from a file and return a list of name and emails
# Followed by emailing the results back to the user
# 3 email functions whose definition starts here
def get_contacts(self, filename):
names=[] # this will contain all the names of users
emails=[] # it will contain the corresponding emails of users
with open(filename, mode='r', encoding='utf-8') as contacts_file:
for contact in contacts_file:
names.append(contact.split()[0])
emails.append(contact.split()[1])
return names, emails
def read_template(self, filename):
with open(filename, mode='r', encoding='utf-8') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
def email(self, names, emails, message_template ):
# set up the SMTP server
s = smtplib.SMTP(host='smtp.gmail.com', port=587) # email service provider info goes here
s.starttls()
MY_ADDRESS = '[email protected]'
PASSWORD = '*********'
s.login(MY_ADDRESS, PASSWORD) # email and password of the senders account
# Sending emails logic starts here
# For each contact, send the email:
for name, email1 in zip(names, emails):
msg = MIMEMultipart() # create a message
res = open('course-title-result.json', 'r')
res = json.load(res)
res1 = ''
for x in res:
res1 = res1 + ("%s - %s" %(x, res[x])) + "\n"
# add in the actual person name to the message template
message = message_template.substitute(PERSON_NAME=name.title(),RESULT=res1)
print("email sent") # to check the format of message being sent
# setup the parameters of the message
msg['From']=MY_ADDRESS
msg['To']=email1
msg['Subject']="Mock Test Result - "
# add in the message body
msg.attach(MIMEText(message, 'plain'))
# send the message via the server set up earlier.
s.send_message(msg)
del msg
# terminate the SMTP connection and close the connection
s.quit()
def __init__(self, name):
print("hello world from "+name)
obj = mockTest('ash')
obj.input()
obj.save()
obj.process()
names, emails = obj.get_contacts('contacts.txt') # read contacts
message_template = obj.read_template('message.txt') # define a template for body
obj.email(names, emails, message_template)