Example #1
0
class Notifier(object):

    distrib_list = []
    sent_from = ""
    initiated = False
    smtp_username = ""
    smtp_password = ""

    # navigate to https://myaccount.google.com/lesssecureapps with your account to enable less secure logins
    def __init__(self,
                 server="smtp.gmail.com",
                 port=587,
                 username="",
                 password="",
                 auth_file=""):
        """
        init
        """

        try:
            self.sheet = Sheet(auth_file_name=auth_file, sheet_name="contacts")
            self.sheet.open_sheet()
            self.config_distrib_list()

            self.smtpserver = smtplib.SMTP(server, port)
            self.smtpserver.ehlo()
            self.smtpserver.starttls()
            self.smtpserver.ehlo()

            self.smtpserver.login(self.smtp_username, self.smtp_password)

            self.initiated = True

        except Exception as e:

            logging.debug(" unable to initiate email notifications ")
            logging.debug(e)

    def config_distrib_list(self):
        """
        get distirib list
        """

        for row in self.sheet.all_sheet_records:
            self.distrib_list.append(row['contacts'])

        self.sent_from = self.sheet.all_sheet_records[0]['from']
        self.smtp_username = self.sheet.all_sheet_records[0]['credentials']
        self.smtp_password = self.sheet.all_sheet_records[1]['credentials']

    def send(self, content="", subject=""):
        """
        send notification
        """

        self.sheet.login()

        if (self.initiated):

            try:

                body = '\r\n'.join([
                    'To: %s' % ", ".join(self.distrib_list),
                    'From: %s' % self.sent_from,
                    'Subject: %s' % subject, '', content
                ])

                self.smtpserver.sendmail(self.sent_from, self.distrib_list,
                                         body)
                logging.info("\nSending notification ")
                logging.info("From: " + self.sent_from)
                logging.info("To: " + ", ".join(self.distrib_list))
                logging.info("\n")

            except Exception as e:
                logging.debug(e)

        else:
            logging.debug(" the email client was not initiated ")
Example #2
0
class NtMgr(QThread):
    """
    manages instantiation of google services
    
    bot constructor calls the thread start and waits for the signal
    
    once the signal is done bot constructor gets the inited sheet and notifier objects from NtMgr and enables controls
    
    """

    # the ui link
    ui = None

    # signal to exit thread
    nt_ready_signal = pyqtSignal()

    # notifier instance
    ntfr = None

    # sheet instance
    sheet = None

    def __init__(self, ui=None, signal_callback=None, auth_file=""):
        """
        init
        """
        QThread.__init__(self)

        self.ui = ui
        self.nt_ready_signal.connect(signal_callback)

    def run(self):
        """
        initialize sheets and notifier
        """

        try:
            logging.info("\nOpening notification sheet ... ")
            self.ntfr = Notifier(auth_file=self.ui.get_google_auth_file_name())

            logging.info("\nOpening the tracking sheet ... ")
            if (self.ui.get_google_auth_file_name() != ""):
                self.sheet = Sheet(
                    auth_file_name=self.ui.get_google_auth_file_name())
            else:
                self.sheet = Sheet()

            self.sheet.open_sheet()

            # get back to caller from this thread
            self.nt_ready_signal.emit()

            #self.ui.activateButton.setEnabled(True)
            #self.ui.deactivateButton.setEnabled(False)

        except Exception as e:

            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            logging.debug(exc_type, fname, exc_tb.tb_lineno)
            logging.debug(e)

            logging.error(
                " An issue occurred while initializing network components ")
            logging.error(e)

            #self.ui.activateButton.setEnabled(False)
            #self.ui.refreshButton.setEnabled(True)

            self.nt_ready_signal.emit()

    def get_inited_objects(self):
        """
        get the objects
        """

        return self.ntfr, self.sheet