<commit_before>""" Operations for syncing back local datastore changes to Gmail. """

from inbox.crispin import writable_connection_pool
from inbox.actions.backends.generic import (set_remote_starred,
                                            set_remote_unread,
                                            remote_delete_draft,
                                            remote_save_draft,
                                            uids_by_folder)
from inbox.mailsync.backends.imap.generic import uidvalidity_cb
from inbox.models.category import Category

PROVIDER = 'gmail'

__all__ = ['set_remote_starred', 'set_remote_unread', 'remote_save_draft',
           'remote_change_labels', 'remote_delete_draft',
           'remote_create_label', 'remote_update_label']


def remote_change_labels(account, message_id, db_session, removed_labels,
                         added_labels):
    uids_for_message = uids_by_folder(message_id, db_session)
    with writable_connection_pool(account.id).get() as crispin_client:
        for folder_name, uids in uids_for_message.items():
            crispin_client.select_folder(folder_name, uidvalidity_cb)
            crispin_client.conn.add_gmail_labels(uids, added_labels)
            crispin_client.conn.remove_gmail_labels(uids, removed_labels)


def remote_create_label(account, category_id, db_session):
    category = db_session.query(Category).get(category_id)
    with writable_connection_pool(account.id).get() as crispin_client:
        crispin_client.conn.create_folder(category.display_name)


def remote_update_label(account, category_id, db_session, old_name):<commit_msg>Quote labels before mutating them.<commit_after>""" Operations for syncing back local datastore changes to Gmail. """

from inbox.crispin import writable_connection_pool
from inbox.actions.backends.generic import (set_remote_starred,
                                            set_remote_unread,
                                            remote_delete_draft,
                                            remote_save_draft,
                                            uids_by_folder)
from inbox.mailsync.backends.imap.generic import uidvalidity_cb
from inbox.models.category import Category

PROVIDER = 'gmail'

__all__ = ['set_remote_starred', 'set_remote_unread', 'remote_save_draft',
           'remote_change_labels', 'remote_delete_draft',
           'remote_create_label', 'remote_update_label']


# Lifted from imaplib. Quote arguments ourselves, pending fix in imapclient.
def _quote(arg):
    arg = arg.replace('\\', '\\\\')
    arg = arg.replace('"', '\\"')
    return u'"{}"'.format(arg)


def remote_change_labels(account, message_id, db_session, removed_labels,
                         added_labels):
    added_labels = map(_quote, added_labels)
    removed_labels = map(_quote, removed_labels)
    uids_for_message = uids_by_folder(message_id, db_session)
    with writable_connection_pool(account.id).get() as crispin_client:
        for folder_name, uids in uids_for_message.items():
            crispin_client.select_folder(folder_name, uidvalidity_cb)
            crispin_client.conn.add_gmail_labels(uids, added_labels)
            crispin_client.conn.remove_gmail_labels(uids, removed_labels)


def remote_create_label(account, category_id, db_session):
    category = db_session.query(Category).get(category_id)
    with writable_connection_pool(account.id).get() as crispin_client:
        crispin_client.conn.create_folder(category.display_name)


def remote_update_label(account, category_id, db_session, old_name):
{'fraction_non_alphanumeric': 0.08151260504201681, 'fraction_numerical': 0.0, 'mean_word_length': 3.0033632286995515, '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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5876696', 'n_tokens_mistral': 1018, 'n_tokens_neox': 1010, 'n_words': 187}