<commit_before>from flask import current_app, redirect, session
from requests import post, get
from urllib import urlencode
import random
from string import ascii_uppercase, digits
import os
import posixpath
import json
from datetime import date, timedelta
from .view_functions import WriteLocked, ReadLocked

GA_CONFIG_FILENAME = 'ga_config.json'
GOOGLE_ACCESS_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'
GOOGLE_PLUS_WHOAMI_URL = 'https://www.googleapis.com/plus/v1/people/me'
GOOGLE_ANALYTICS_PROPERTIES_URL = 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties'

def authorize_google():
    ''' Authorize google via oauth2
    '''
    #
    # This is how google says the state should be generated
    #
    state = ''.join(random.choice(ascii_uppercase + digits) for x in xrange(32))
    session['state'] = state

    query_string = urlencode(dict(client_id=current_app.config['GA_CLIENT_ID'], redirect_uri=current_app.config['GA_REDIRECT_URI'], scope='openid profile https://www.googleapis.com/auth/analytics', state=state, response_type='code', access_type='offline', approval_prompt='force'))
    return redirect('https://accounts.google.com/o/oauth2/auth' + '?' + query_string)

def get_google_client_info():
    ''' Return client ID and secret for Google OAuth use.
    '''
    return current_app.config['GA_CLIENT_ID'], current_app.config['GA_CLIENT_SECRET']

def request_new_google_access_and_refresh_tokens(request):
    ''' Get new access and refresh tokens from the Google API.
    '''
    if request.args.get('state') != session['state']:
        raise Exception()

    code = request.args.get('code')
    redirect_uri = '{0}://{1}/callback'.format(request.scheme, request.host)

    data = dict(client_id=current_app.config['GA_CLIENT_ID'], client_secret=current_app.config['GA_CLIENT_SECRET'],
                code=code, redirect_uri=redirect_uri,
                grant_type='authorization_code')

    response = post(GOOGLE_ACCESS_TOKEN_URL, data=data)
    access = response.json()

    if response.status_code != 200:
        if 'error_description' in access:
            raise Exception('Google says "{0}"'.format(access['error_description']))
        else:
            raise Exception('Google Error')

    # write the new tokens to the config file
    ga_config_path = os.path.join(current_app.config['RUNNING_STATE_DIR'], GA_CONFIG_FILENAME)
    with WriteLocked(ga_config_path) as iofile:
        # read the json from the file
        ga_config = json.load(iofile)
        # change the values of the access and refresh tokens
        ga_config['access_token'] = access['access_token']
        ga_config['refresh_token'] = access['refresh_token']
        # write the new config json
        iofile.seek(0)
        iofile.truncate(0)
        json.dump(ga_config, iofile, indent=2, ensure_ascii=False)

    return access['access_token'], access['refresh_token']

def get_new_access_token(refresh_token):
    ''' Get a new access token with the refresh token so a user doesn't need to
        authorize the app again
    '''
    if not refresh_token:
        return False

    data = dict(client_id=current_app.config['GA_CLIENT_ID'], client_secret=current_app.config['GA_CLIENT_SECRET'],
                refresh_token=refresh_token, grant_type='refresh_token')

    resp = post(GOOGLE_ACCESS_TOKEN_URL, data=data)<commit_msg>missed a rename of get_new_access_token to request_new_google_access_token<commit_after>from flask import current_app, redirect, session
from requests import post, get
from urllib import urlencode
import random
from string import ascii_uppercase, digits
import os
import posixpath
import json
from datetime import date, timedelta
from .view_functions import WriteLocked, ReadLocked

GA_CONFIG_FILENAME = 'ga_config.json'
GOOGLE_ACCESS_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'
GOOGLE_PLUS_WHOAMI_URL = 'https://www.googleapis.com/plus/v1/people/me'
GOOGLE_ANALYTICS_PROPERTIES_URL = 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties'

def authorize_google():
    ''' Authorize google via oauth2
    '''
    #
    # This is how google says the state should be generated
    #
    state = ''.join(random.choice(ascii_uppercase + digits) for x in xrange(32))
    session['state'] = state

    query_string = urlencode(dict(client_id=current_app.config['GA_CLIENT_ID'], redirect_uri=current_app.config['GA_REDIRECT_URI'], scope='openid profile https://www.googleapis.com/auth/analytics', state=state, response_type='code', access_type='offline', approval_prompt='force'))
    return redirect('https://accounts.google.com/o/oauth2/auth' + '?' + query_string)

def get_google_client_info():
    ''' Return client ID and secret for Google OAuth use.
    '''
    return current_app.config['GA_CLIENT_ID'], current_app.config['GA_CLIENT_SECRET']

def request_new_google_access_and_refresh_tokens(request):
    ''' Get new access and refresh tokens from the Google API.
    '''
    if request.args.get('state') != session['state']:
        raise Exception()

    code = request.args.get('code')
    redirect_uri = '{0}://{1}/callback'.format(request.scheme, request.host)

    data = dict(client_id=current_app.config['GA_CLIENT_ID'], client_secret=current_app.config['GA_CLIENT_SECRET'],
                code=code, redirect_uri=redirect_uri,
                grant_type='authorization_code')

    response = post(GOOGLE_ACCESS_TOKEN_URL, data=data)
    access = response.json()

    if response.status_code != 200:
        if 'error_description' in access:
            raise Exception('Google says "{0}"'.format(access['error_description']))
        else:
            raise Exception('Google Error')

    # write the new tokens to the config file
    ga_config_path = os.path.join(current_app.config['RUNNING_STATE_DIR'], GA_CONFIG_FILENAME)
    with WriteLocked(ga_config_path) as iofile:
        # read the json from the file
        ga_config = json.load(iofile)
        # change the values of the access and refresh tokens
        ga_config['access_token'] = access['access_token']
        ga_config['refresh_token'] = access['refresh_token']
        # write the new config json
        iofile.seek(0)
        iofile.truncate(0)
        json.dump(ga_config, iofile, indent=2, ensure_ascii=False)

    return access['access_token'], access['refresh_token']

def request_new_google_access_token(refresh_token):
    ''' Get a new access token with the refresh token so a user doesn't need to
        authorize the app again
    '''
    if not refresh_token:
        return False

    data = dict(client_id=current_app.config['GA_CLIENT_ID'], client_secret=current_app.config['GA_CLIENT_SECRET'],
                refresh_token=refresh_token, grant_type='refresh_token')

    resp = post(GOOGLE_ACCESS_TOKEN_URL, data=data)
{'fraction_non_alphanumeric': 0.10874013446360713, 'fraction_numerical': 0.00467699503069278, 'mean_word_length': 4.172335600907029, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 6, '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': '5807783', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2068, 'n_words': 465}