<commit_before>import logging
from operator import itemgetter

from django.db import transaction
from django.shortcuts import render_to_response
from django.template import RequestContext
from requests import HTTPError
import reversion
from social.backends.github import GithubOAuth2
from social.exceptions import InvalidEmail
from social.pipeline.partial import partial

from judge.forms import ProfileForm
from judge.models import Profile, Language


logger = logging.getLogger('judge.social_auth')


class GitHubSecureEmailOAuth2(GithubOAuth2):
    name = 'github-secure'

    def user_data(self, access_token, *args, **kwargs):
        data = self._user_data(access_token)
        try:
            emails = self._user_data(access_token, '/emails')
        except (HTTPError, ValueError, TypeError):
            emails = []

        emails = [(e, e.get('primary'), False) for e in emails if isinstance(e, dict) and e.get('verified')]
        emails.sort(key=itemgetter(1), reverse=True)
        emails = map(itemgetter(0), emails)

        if emails:
            data['email'] = emails[0]
        else:
            data['email'] = None


def verify_email(backend, details, *args, **kwargs):
    if not details['email']:
        raise InvalidEmail(backend)


@partial
def make_profile(backend, user, response, is_new=False, *args, **kwargs):
    logger.info('Info from %s: %s', backend.name, response)

    if is_new:
        if not hasattr(user, 'profile'):
            profile = Profile(user=user)
            profile.language = Language.get_python2()
            logger.info('Creating profile for %s', user.username)
            if backend.name == 'google-oauth2':<commit_msg>Completely lost in the control flow. #50<commit_after>import logging
from operator import itemgetter

from django.db import transaction
from django.shortcuts import render_to_response
from django.template import RequestContext
from requests import HTTPError
import reversion
from social.backends.github import GithubOAuth2
from social.exceptions import InvalidEmail
from social.pipeline.partial import partial

from judge.forms import ProfileForm
from judge.models import Profile, Language


logger = logging.getLogger('judge.social_auth')


class GitHubSecureEmailOAuth2(GithubOAuth2):
    name = 'github-secure'

    def user_data(self, access_token, *args, **kwargs):
        data = self._user_data(access_token)
        try:
            emails = self._user_data(access_token, '/emails')
        except (HTTPError, ValueError, TypeError):
            emails = []

        logger.info('Got emails from GitHub: %s', emails)
        emails = [(e, e.get('primary'), False) for e in emails if isinstance(e, dict) and e.get('verified')]
        emails.sort(key=itemgetter(1), reverse=True)
        emails = map(itemgetter(0), emails)
        logger.info('Usable emails: %s', emails)

        if emails:
            data['email'] = emails[0]
        else:
            data['email'] = None
        logger.info('Setting email: %s', data['email'])


def verify_email(backend, details, *args, **kwargs):
    if not details['email']:
        logger.info('Denying: %s', details)
        raise InvalidEmail(backend)
    else:
        logger.info('Allowing: %s', details)


@partial
def make_profile(backend, user, response, is_new=False, *args, **kwargs):
    logger.info('Info from %s: %s', backend.name, response)

    if is_new:
        if not hasattr(user, 'profile'):
            profile = Profile(user=user)
            profile.language = Language.get_python2()
            logger.info('Creating profile for %s', user.username)
            if backend.name == 'google-oauth2':
{'fraction_non_alphanumeric': 0.10983292248698986, 'fraction_numerical': 0.0049301561216105174, 'mean_word_length': 3.3270142180094786, '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': '2098414', 'n_tokens_mistral': 1053, 'n_tokens_neox': 1045, 'n_words': 294}