From 019c38039873cd60df915b8026bd8abd50e648b5 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sun, 27 Sep 2020 01:11:23 +0530 Subject: removed common functions and added to misc --- app/misc/__init__.py | 0 app/misc/common.py | 33 +++++++++++++++++++++++++++++++++ app/misc/email.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 app/misc/__init__.py create mode 100644 app/misc/common.py create mode 100644 app/misc/email.py (limited to 'app/misc') diff --git a/app/misc/__init__.py b/app/misc/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/misc/common.py b/app/misc/common.py new file mode 100644 index 0000000..1463c73 --- /dev/null +++ b/app/misc/common.py @@ -0,0 +1,33 @@ +import os +import glob +from shutil import copy + +def CopyContentOfFolder(sauce,destination): + src_files = os.listdir(sauce) + for file_name in src_files: + full_file_name = os.path.join(sauce, file_name) + if os.path.isfile(full_file_name): + copy(full_file_name, destination) + +def RemoveAllFilesMatching(directory,pattern): + print(directory+"/*"+pattern) + FileList = glob.glob(directory+"/*"+pattern) + for FilePath in FileList: + try: + os.remove(FilePath) + except: + print("Error in removing misc file") + +def get3DModel(protein,ligand): + try: + import pymol2 + except ImportError: + print("🤭 PyMOL 2 has not been installed correctly") + return None + session = pymol2.PyMOL() + session.start() + cmd = session.cmd + cmd.load(protein,"target") + cmd.load(ligand,"ligand") + cmd.save("model.dae") + session.stop() \ No newline at end of file diff --git a/app/misc/email.py b/app/misc/email.py new file mode 100644 index 0000000..d2967bb --- /dev/null +++ b/app/misc/email.py @@ -0,0 +1,48 @@ +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.mime.base import MIMEBase +from email import encoders + +import configparser +iniConfig = configparser.ConfigParser() +iniConfig.read('config.ini') + +try: + iniConfig['DATABASE'] +except KeyError: + iniConfig.read("../config.ini") + + +def email(toaddr,jobID,date,description,zipArchive=None,complete=True,reason=None): + fromaddr = iniConfig['SMTP']['EMAIL'] + + msg = MIMEMultipart() + msg['From'] = fromaddr + msg['To'] = toaddr + msg['Subject'] = "Curie Web Results for Job ID " + str(jobID) + if complete: + body = "Attached Zip contains the docked files, PLIP report and PyMOL Visualisations. If the ZIP file does not contain these files, please report this issue by replying to this email. Job was submitted on {} with the description {}".format(date, description) + else: + body = "Task unsuccessful :( \n Job was submitted on {} with the description {}".format(date, description) + + if reason != None: + body = reason + str(body) + + msg.attach(MIMEText(body, 'plain')) + if zipArchive != None: + filename = "Curie_Web_Results_Job_ID_" + str(jobID) + ".zip" + p = MIMEBase('application', 'octet-stream') + with open((str(zipArchive) + ".zip"), "rb") as attachment: + p.set_payload((attachment).read()) + encoders.encode_base64(p) + p.add_header('Content-Disposition', "attachment; filename= %s" % filename) + msg.attach(p) + + s = smtplib.SMTP(iniConfig['SMTP']['SERVER'], iniConfig['SMTP']['PORT']) + s.starttls() + s.login(fromaddr, iniConfig['SMTP']['PASSWORD']) + text = msg.as_string() + + s.sendmail(fromaddr, toaddr, text) + s.quit() -- cgit v1.2.3