<commit_before>from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound

from inbox.models.message import Message
from inbox.models.thread import Thread
from inbox.models.folder import Folder, FolderItem

from inbox.util.file import Lock


from inbox.log import get_logger
log = get_logger()


def reconcile_message(db_session, log, inbox_uid, new_msg):
    """
    Identify a `Sent Mail` (or corresponding) message synced from the
    remote backend as one we sent and reconcile it with the message we
    created and stored in the local data store at the time of sending.

    Notes
    -----
    Our current reconciliation strategy is to keep both messages i.e.
    the one we sent and the one we synced.

    """
    try:
        message = db_session.query(Message).filter(
            Message.public_id == inbox_uid).one()
        assert message.is_created
        message.resolved_message = new_msg
        return message

    # Don't raise here because message is an Inbox created message but
    # not by this client i.e. the Inbox created version is not present in the
    # local data store.
    except NoResultFound:
        log.error('NoResultFound for this message, even though '
                  'it has the inbox-sent header: {0}'.format(inbox_uid))

    except MultipleResultsFound:
        log.error('MultipleResultsFound when reconciling message with '
                  'inbox-sent header: {0}'.format(inbox_uid))
        raise<commit_msg>Change log level on reconcile_message() logging.<commit_after>from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound

from inbox.models.message import Message
from inbox.models.thread import Thread
from inbox.models.folder import Folder, FolderItem

from inbox.util.file import Lock


from inbox.log import get_logger
log = get_logger()


def reconcile_message(db_session, log, inbox_uid, new_msg):
    """
    Identify a `Sent Mail` (or corresponding) message synced from the
    remote backend as one we sent and reconcile it with the message we
    created and stored in the local data store at the time of sending.

    Notes
    -----
    Our current reconciliation strategy is to keep both messages i.e.
    the one we sent and the one we synced.

    """
    try:
        message = db_session.query(Message).filter(
            Message.public_id == inbox_uid).one()
        assert message.is_created
        message.resolved_message = new_msg
        return message

    # Don't raise here because message is an Inbox created message but
    # not by this client i.e. the Inbox created version is not present in the
    # local data store.
    except NoResultFound:
        log.warning('NoResultFound for this message, even though '
                    'it has the inbox-sent header: {0}'.format(inbox_uid))

    except MultipleResultsFound:
        log.error('MultipleResultsFound when reconciling message with '
                  'inbox-sent header: {0}'.format(inbox_uid))
        raise
{'fraction_non_alphanumeric': 0.06751763520322472, 'fraction_numerical': 0.0013436345314074571, 'mean_word_length': 2.9443708609271524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7445924', 'n_tokens_mistral': 824, 'n_tokens_neox': 776, 'n_words': 310}