<commit_before>import logging

from threading import Timer

from tornado.escape import json_decode
from tornado.httpclient import HTTPClient, HTTPRequest

from newebe.core.handlers import NewebeAuthHandler
from newebe.profile.models import UserManager
from newebe.contacts.models import ContactManager

from newebe.activities.models import Activity

logger = logging.getLogger("newebe.core")


class ProfileUpdater:
    '''
    Utility class to handle profile forwarding to contacts. Set error
    log inside acitivity when error occurs.
    '''

    sending_data = False
    contact_requests = {}


    def send_profile_to_contacts(self):
        '''
        External methods to not send too much times the changed profile. 
        A timer is set to wait for other modifications before running this
        function that sends modification requests to every contacts.
        '''

        client = HTTPClient()
        self.sending_data = False

        user = UserManager.getUser()
        jsonbody = user.toJson()

        activity = Activity(
            authorKey = user.key,
            author = user.name,
            verb = "modifies",
            docType = "profile",
            method = "PUT",
            docId = "none",
            isMine = True
        )
        activity.save()     

        for contact in ContactManager.getTrustedContacts():
            request = HTTPRequest("%scontacts/update-profile/" % contact.url, 
                         method="PUT", body=jsonbody)

            response = client.fetch(request)

            if response.error:
                logger.error("""
                    Profile sending to a contact failed, error infos are 
                    stored inside activity.
                """)
                activity.add_error(contact)
                activity.save()    
            logger.info("Profile update successfully sent.")


    def forward_profile(self):
        '''
        Because profile modification occurs a lot in a short time. The 
        profile is forwarded only every ten minutes. It avoids to create
        too much activities for this profile modification.
        '''<commit_msg>make profile update request asynchronous<commit_after>import logging

from threading import Timer

from tornado import gen
from tornado.web import asynchronous

from newebe.lib.http_util import ContactClient

from newebe.core.handlers import NewebeAuthHandler
from newebe.profile.models import UserManager
from newebe.contacts.models import ContactManager

from newebe.activities.models import Activity

logger = logging.getLogger("newebe.core")


class ProfileUpdater:
    '''
    Utility class to handle profile forwarding to contacts. Set error
    log inside acitivity when error occurs.
    '''

    sending_data = False
    contact_requests = {}

    @asynchronous
    @gen.engine
    def send_profile_to_contacts(self):
        '''
        External methods to not send too much times the changed profile. 
        A timer is set to wait for other modifications before running this
        function that sends modification requests to every contacts.
        '''

        client = ContactClient()
        self.sending_data = False

        user = UserManager.getUser()
        jsonbody = user.toJson()

        activity = Activity(
            authorKey = user.key,
            author = user.name,
            verb = "modifies",
            docType = "profile",
            method = "PUT",
            docId = "none",
            isMine = True
        )
        activity.save()     

        for contact in ContactManager.getTrustedContacts():
            client.put("%scontacts/update-profile/" % contact.url, 
                       body=jsonbody, callback=(yield gen.Callback("profile")))
            response = yield gen.Wait("profile")

            if response.error:
                logger.error("""
                    Profile sending to a contact failed, error infos are 
                    stored inside activity.
                """)
                activity.add_error(contact)
                activity.save()    
            logger.info("Profile update successfully sent.")


    def forward_profile(self):
        '''
        Because profile modification occurs a lot in a short time. The 
        profile is forwarded only every two minutes. It avoids to create
        too much activities for this profile modification.
        '''
{'fraction_non_alphanumeric': 0.06987295825771325, 'fraction_numerical': 0.0, 'mean_word_length': 2.0767620376831823, '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': '6095198', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1034, 'n_words': 360}