Пример #1
0
def retrieve_new_emails(client: IMAPClient,
                        user: EmailAuthUser,
                        logger: Optional[logging.Logger] = None) -> List[int]:
    """Retrieve new unseen emails for the user."""
    logger = logger or get_logger(CLIENT)
    mails: List[int] = []
    try:
        client.login(user.email_address, user.password)
        logger.info("Logged in successful for %s", user)
    except LoginError:
        logger.exception("Credentials could not be verified for '%s'.",
                         user.email_address)
        client.shutdown()
        raise
    except socket.error:
        logger.exception("Could not connect to the mail server.")
        client.shutdown()
        raise
    except Exception as exc_info:  # pylint: disable=broad-except
        logger.exception("Unknown Exception occured. %s", str(exc_info))
        client.shutdown()
        raise
    else:

        create_folder_if_not_exists(client, EmailFolders.IMPORTANT, logger)
        create_folder_if_not_exists(client, EmailFolders.URGENT, logger)

        search_key = b"UNSEEN UNKEYWORD " + FLAG_TO_CHECK

        client.select_folder(EmailFolders.INBOX, readonly=True)

        logger.debug("Searching for unseen emails flagged '%s'", FLAG_TO_CHECK)
        mails = client.search(search_key)

    return mails
Пример #2
0
def test_get_logger_on_logger_set_on_config(config_env_file):
    """Test get_logger return logger when no config is set."""
    os.environ["ZIPPY_CONFIG_FILE"] = str(config_env_file.absolute().resolve())

    from zippy.utils.log_handler import get_logger

    logger = get_logger("testA")
    assert logger.getEffectiveLevel() == logging.DEBUG
    assert logger.name == "testA"
Пример #3
0
def test_get_logger_on_no_key_found(config_file):
    """Test get_logger if no module name exist in config."""
    os.environ["ZIPPY_CONFIG_FILE"] = str(config_file.absolute().resolve())

    from zippy.utils.log_handler import get_logger

    logger = get_logger("testA")
    assert logger.getEffectiveLevel() == logging.WARNING
    assert logger.name == "testA"
Пример #4
0
def test_get_logger_positive(config_file):
    """Test get_logger happy path."""
    os.environ["ZIPPY_CONFIG_FILE"] = str(config_file.absolute().resolve())

    from zippy.utils.log_handler import get_logger

    logger = get_logger("test")
    assert logger.getEffectiveLevel() == logging.DEBUG
    assert logger.name == "test"
Пример #5
0
def main(
    config: dict,
    user: EmailAuthUser,
    client: Optional[IMAPClient] = None,
    logger: Optional[logging.Logger] = None,
):
    """Handle all updates for a specific users."""
    logger = logger or get_logger(CLIENT)
    client = client or get_client(config, logger=logger)
    with client:
        unprocessed_mails = retrieve_new_emails(client, user, logger)
        processed_messages = process_mails(client, unprocessed_mails, logger)
        online_train_all(processed_messages)
Пример #6
0
def process_mail(client: IMAPClient, uid: int, message_data: dict,
                 logger: logging.Logger) -> ProcessedMessage:
    """Process mail."""
    output = get_logger(OUTPUT)
    email_message = email.message_from_bytes(message_data[MESSAGE_FORMAT])
    *msg, threshold = rank_message(email_message)
    processed_msg = ProcessedMessage(*msg)
    output.info({
        "rank": processed_msg.rank,
        "important": processed_msg.important,
        "intent": processed_msg.intent,
        "subject": email_message["Subject"],
        "uid": uid,
        "from": email_message["From"],
        "to": email_message["To"],
        "threshold": threshold,
    })
    if processed_msg.important and not processed_msg.intent:
        shift_mail(
            client=client,
            uid=uid,
            source=EmailFolders.INBOX,
            destination=EmailFolders.IMPORTANT,
            logger=logger,
        )
    elif processed_msg.important and processed_msg.intent:
        shift_mail(
            client=client,
            uid=uid,
            source=EmailFolders.INBOX,
            destination=EmailFolders.URGENT,
            logger=logger,
        )
    else:
        mark_processed(client=client, uid=uid, logger=logger)

    return processed_msg
Пример #7
0
"""Module for online training new email messages."""

import pathlib

import nltk
import numpy as np
import pandas as pd

from sklearn.feature_extraction.text import CountVectorizer

from zippy.utils.log_handler import get_logger

LOGGER = get_logger("client")

MODEL_DIR = pathlib.Path(__file__).parents[3] / "output/models/simplerank"

try:
    VEC = CountVectorizer(stop_words=nltk.corpus.stopwords.words("english"))
except LookupError:
    nltk.download("stopwords")
    VEC = CountVectorizer(stop_words=nltk.corpus.stopwords.words("english"))


def load_weights(user="******"):
    """Load weights from the CSV."""
    from_wt = pd.read_csv(MODEL_DIR / user / "from_weight.csv")
    thread_sender_wt = pd.read_csv(MODEL_DIR / user /
                                   "thread_senders_weight.csv")
    thread_wt = pd.read_csv(MODEL_DIR / user / "thread_weights.csv")
    thread_term_wt = pd.read_csv(MODEL_DIR / user / "thread_term_weights.csv")
    msg_term_wt = pd.read_csv(MODEL_DIR / user / "msg_terms_weight.csv")
Пример #8
0
 def wrapper(*args, **kwargs):
     log = logger or get_logger(CLIENT)
     log.info("LOG: Running job %s", func.__name__)
     result = func(*args, **kwargs)
     log.info("LOG: Job '%s' completed", func.__name__)
     return result