import os import smtplib import ssl from config import Config from email.message import EmailMessage from config import Config from log import log from typing import Any, List from PyQt5.QtWidgets import QMessageBox def ask_yes_no(title: str, question: str) -> bool: """Ask question; return True for yes, False for no""" button_reply = QMessageBox.question(None, title, question) return button_reply == QMessageBox.Yes def format_username(account) -> str: """ Format account username according to whether we follow that account or not. """ username = account.username if account.followed: colour = Config.FOLLOWED_COLOUR else: colour = Config.NORMAL_COLOUR return '' + username + '' def index_ojects_by_parameter(object_list: List, param: Any): """ Create a dictionary from passed list where each list entry is keyed by passed param n """ results = {} for obj in object_list: results[getattr(obj, param)] = obj return results def send_mail(to_addr, from_addr, subj, body): # From https://docs.python.org/3/library/email.examples.html # Create a text/plain message msg = EmailMessage() msg.set_content(body) msg['Subject'] = subj msg['From'] = from_addr msg['To'] = to_addr # Send the message via SMTP server. context = ssl.create_default_context() try: s = smtplib.SMTP(host=Config.MAIL_SERVER, port=Config.MAIL_PORT) if Config.MAIL_USE_TLS: s.starttls(context=context) if Config.MAIL_USERNAME and Config.MAIL_PASSWORD: s.login(Config.MAIL_USERNAME, Config.MAIL_PASSWORD) s.send_message(msg) except Exception as e: print(e) finally: s.quit() def show_OK(title: str, msg: str) -> None: """Display a message to user""" QMessageBox.information(None, title, msg, buttons=QMessageBox.Ok) def show_warning(title: str, msg: str) -> None: """Display a warning to user""" QMessageBox.warning(None, title, msg, buttons=QMessageBox.Cancel)