def objects_from_file(file_ref): """A utility function for initializing a group of objects from a YAML file written in a certain format.""" file_info = file_finder(file_ref) if 'path' not in file_info: raise SystemError('objects_from_file: file reference ' + str(file_ref) + ' not found') objects = list() with open(file_info['fullpath'], 'r') as fp: for document in yaml.load_all(fp): if type(document) is not dict: raise SystemError('objects_from_file: file reference ' + str(file_ref) + ' contained a document that was not a YAML dictionary') if len(document): if not ('object' in document and 'items' in document): raise SystemError('objects_from_file: file reference ' + str(file_ref) + ' contained a document that did not contain an object and items declaration') if type(document['items']) is not list: raise SystemError('objects_from_file: file reference ' + str(file_ref) + ' contained a document the items declaration for which was not a dictionary') constructor = None if document['object'] in globals(): contructor = globals()[document['object']] elif document['object'] in locals(): contructor = locals()[document['object']] if not constructor: if 'module' in document: new_module = __import__(document['module'], globals(), locals(), [document['object']], -1) constructor = getattr(new_module, document['object'], None) if not constructor: raise SystemError('objects_from_file: file reference ' + str(file_ref) + ' contained a document for which the object declaration, ' + str(document['object']) + ' could not be found') for item in document['items']: if type(item) is not dict: raise SystemError('objects_from_file: file reference ' + str(file_ref) + ' contained an item, ' + str(item) + ' that was not expressed as a dictionary') objects.append(constructor(**item)) return objects
def objects_from_file(file_ref): file_info = file_finder(file_ref) if "path" not in file_info: raise SystemError("objects_from_file: file reference " + str(file_ref) + " not found") objects = list() with open(file_info["fullpath"], "r") as fp: for document in yaml.load_all(fp): if type(document) is not dict: raise SystemError( "objects_from_file: file reference " + str(file_ref) + " contained a document that was not a YAML dictionary" ) if len(document): if not ("object" in document and "items" in document): raise SystemError( "objects_from_file: file reference " + str(file_ref) + " contained a document that did not contain an object and items declaration" ) if type(document["items"]) is not list: raise SystemError( "objects_from_file: file reference " + str(file_ref) + " contained a document the items declaration for which was not a dictionary" ) constructor = None if document["object"] in globals(): contructor = globals()[document["object"]] elif document["object"] in locals(): contructor = locals()[document["object"]] if not constructor: if "module" in document: new_module = __import__(document["module"], globals(), locals(), [document["object"]], -1) constructor = getattr(new_module, document["object"], None) if not constructor: raise SystemError( "objects_from_file: file reference " + str(file_ref) + " contained a document for which the object declaration, " + str(document["object"]) + " could not be found" ) for item in document["items"]: if type(item) is not dict: raise SystemError( "objects_from_file: file reference " + str(file_ref) + " contained an item, " + str(item) + " that was not expressed as a dictionary" ) objects.append(constructor(**item)) return objects
def objects_from_file(file_ref): """A utility function for initializing a group of objects from a YAML file written in a certain format.""" file_info = file_finder(file_ref) if 'path' not in file_info: raise SystemError('objects_from_file: file reference ' + str(file_ref) + ' not found') objects = list() with open(file_info['fullpath'], 'r') as fp: for document in yaml.load_all(fp): if type(document) is not dict: raise SystemError( 'objects_from_file: file reference ' + str(file_ref) + ' contained a document that was not a YAML dictionary') if len(document): if not ('object' in document and 'items' in document): raise SystemError( 'objects_from_file: file reference ' + str(file_ref) + ' contained a document that did not contain an object and items declaration' ) if type(document['items']) is not list: raise SystemError( 'objects_from_file: file reference ' + str(file_ref) + ' contained a document the items declaration for which was not a dictionary' ) constructor = None if document['object'] in globals(): contructor = globals()[document['object']] elif document['object'] in locals(): contructor = locals()[document['object']] if not constructor: if 'module' in document: new_module = __import__(document['module'], globals(), locals(), [document['object']], -1) constructor = getattr(new_module, document['object'], None) if not constructor: raise SystemError( 'objects_from_file: file reference ' + str(file_ref) + ' contained a document for which the object declaration, ' + str(document['object']) + ' could not be found') for item in document['items']: if type(item) is not dict: raise SystemError( 'objects_from_file: file reference ' + str(file_ref) + ' contained an item, ' + str(item) + ' that was not expressed as a dictionary') objects.append(constructor(**item)) return objects
def send_email(to=None, sender=None, cc=None, bcc=None, template=None, body=None, html=None, subject="", attachments=[]): from flask_mail import Message if type(to) is not list: to = [to] if len(to) == 0: return False if template is not None: if subject is None or subject == '': subject = template.subject body_html = '<html><body>' + markdown_to_html( template.content) + '</body></html>' if body is None: body = html2text.html2text(body_html) if html is None: html = body_html if body is None and html is None: body = "" email_stringer = lambda x: email_string(x, include_name=False) msg = Message(subject, sender=email_stringer(sender), recipients=email_stringer(to), cc=email_stringer(cc), bcc=email_stringer(bcc), body=body, html=html) success = True for attachment in attachments: attachment_list = list() if type(attachment) is DAFileCollection: subattachment = getattr(attachment, 'pdf', None) if subattachment is None: subattachment = getattr(attachment, 'rtf', None) if subattachment is None: subattachment = getattr(attachment, 'tex', None) if subattachment is not None: attachment_list.append(subattachment) else: success = False elif type(attachment) is DAFile: attachment_list.append(attachment) elif type(attachment) is DAFileList: attachment_list.extend(attachment.elements) else: success = False if success: for the_attachment in attachment_list: if the_attachment.ok: file_info = file_finder(str(the_attachment.number)) if ('path' in file_info): failed = True with open(file_info['path'], 'r') as fp: msg.attach(file_info['filename'], file_info['mimetype'], fp.read()) failed = False if failed: success = False else: success = False appmail = mail_variable() if not appmail: success = False if success: try: appmail.send(msg) except Exception as errmess: logmessage("Sending mail failed: " + str(errmess)) success = False return (success)
def send_email(to=None, sender=None, cc=None, bcc=None, template=None, body=None, html=None, subject="", attachments=[]): """Sends an e-mail and returns whether sending the e-mail was successful.""" from flask_mail import Message if type(to) is not list: to = [to] if len(to) == 0: return False if template is not None: if subject is None or subject == '': subject = template.subject body_html = '<html><body>' + markdown_to_html(template.content) + '</body></html>' if body is None: body = html2text.html2text(body_html) if html is None: html = body_html if body is None and html is None: body = "" email_stringer = lambda x: email_string(x, include_name=False) msg = Message(subject, sender=email_stringer(sender), recipients=email_stringer(to), cc=email_stringer(cc), bcc=email_stringer(bcc), body=body, html=html) success = True for attachment in attachments: attachment_list = list() if type(attachment) is DAFileCollection: subattachment = getattr(attachment, 'pdf', None) if subattachment is None: subattachment = getattr(attachment, 'rtf', None) if subattachment is None: subattachment = getattr(attachment, 'tex', None) if subattachment is not None: attachment_list.append(subattachment) else: success = False elif type(attachment) is DAFile: attachment_list.append(attachment) elif type(attachment) is DAFileList: attachment_list.extend(attachment.elements) else: success = False if success: for the_attachment in attachment_list: if the_attachment.ok: file_info = file_finder(str(the_attachment.number)) if ('path' in file_info): failed = True with open(file_info['path'], 'r') as fp: msg.attach(the_attachment.filename, file_info['mimetype'], fp.read()) failed = False if failed: success = False else: success = False # appmail = mail_variable() # if not appmail: # success = False if success: try: # appmail.send(msg) logmessage("Starting to send") async_mail(msg) logmessage("Finished sending") except Exception as errmess: logmessage("Sending mail failed: " + str(errmess)) success = False return(success)
def send_email( to=None, sender=None, cc=None, bcc=None, template=None, body=None, html=None, subject="", attachments=[] ): from flask_mail import Message if type(to) is not list: to = [to] if len(to) == 0: return False if template is not None: if subject is None or subject == "": subject = template.subject body_html = "<html><body>" + markdown_to_html(template.content) + "</body></html>" if body is None: body = html2text.html2text(body_html) if html is None: html = body_html if body is None and html is None: body = "" email_stringer = lambda x: email_string(x, include_name=False) msg = Message( subject, sender=email_stringer(sender), recipients=email_stringer(to), cc=email_stringer(cc), bcc=email_stringer(bcc), body=body, html=html, ) success = True for attachment in attachments: attachment_list = list() if type(attachment) is DAFileCollection: subattachment = getattr(attachment, "pdf", None) if subattachment is None: subattachment = getattr(attachment, "rtf", None) if subattachment is None: subattachment = getattr(attachment, "tex", None) if subattachment is not None: attachment_list.append(subattachment) else: success = False elif type(attachment) is DAFile: attachment_list.append(attachment) elif type(attachment) is DAFileList: attachment_list.extend(attachment.elements) else: success = False if success: for the_attachment in attachment_list: if the_attachment.ok: file_info = file_finder(str(the_attachment.number)) if "path" in file_info: failed = True with open(file_info["path"], "r") as fp: msg.attach(file_info["filename"], file_info["mimetype"], fp.read()) failed = False if failed: success = False else: success = False appmail = mail_variable() if not appmail: success = False if success: try: appmail.send(msg) except Exception as errmess: logmessage("Sending mail failed: " + str(errmess)) success = False return success