<commit_before>'''
Authenticates user for accessing the ISB-CGC Endpoint APIs.

May be run from the command line or in scripts/ipython.

The credentials file can be copied to any machine from which you want
to access the API.  The token can also be used directly.

1. cmd line -- saves FILE                                   
   python ./isb_auth.py          saves the user's credentials;
                      OPTIONAL:
                         -v       for verbose (returns token!)
                         -s FILE  sets credentials file [default: ~/.isb_credentials]
                         -u       URL-only: for use over terminal connections;
                                  gives user a URL to paste into their browser,
                                  and asks for an auth code in return


2. script/ipython -- returns a credentials object; token can be used as an
                     argument to some endpoints URLs in the CGC APIs.
   import isb_auth
   token = isb_auth.get_credentials().access_token


3. Google Cloud Datalab -- run locally, on your home machine:
   python ./isb_auth.py -v

   Generates something like:
   --verbose: printing extra information
   credentials stored in /path/to/.isb_credentials
   access_token: <PASSWORD>...    >------+-- Copy & Paste this into datalab
   ...                                             |
                                                   |
   datalab> token = '<PASSWORD>...'  <---+


   See isb_curl.py help header for more information.
'''

from argparse import ArgumentParser
import os

from oauth2client.client import OAuth2WebServerFlow
from oauth2client import tools
from oauth2client.file import Storage

VERBOSE = False
# for native application - same as settings.INSTALLED_APP_CLIENT_ID
CLIENT_ID = '907668440978-0ol0griu70qkeb6k3gnn2vipfa5mgl60.apps.googleusercontent.com'
# NOTE: this is NOT actually a 'secret' -- we're using the 'installed
# application' OAuth pattern here
CLIENT_SECRET = '<KEY>'

EMAIL_SCOPE = 'https://www.googleapis.com/auth/userinfo.email'
DEFAULT_STORAGE_FILE = os.path.join(os.path.expanduser("~"), '.isb_credentials')


def maybe_print(msg):
    if VERBOSE:
        print msg


def get_credentials(storage=None, oauth_flow_args=[]):
    if __name__ != '__main__':
        oauth_flow_args = list( set(oauth_flow_args).add('--noauth_local_webserver') )
    if storage is None:
        storage = Storage(DEFAULT_STORAGE_FILE)
    credentials = storage.get()<commit_msg>Fixed error when oauth_flow_args is empty in get_credentials().<commit_after>'''
Authenticates user for accessing the ISB-CGC Endpoint APIs.

May be run from the command line or in scripts/ipython.

The credentials file can be copied to any machine from which you want
to access the API.  The token can also be used directly.

1. cmd line -- saves FILE                                   
   python ./isb_auth.py          saves the user's credentials;
                      OPTIONAL:
                         -v       for verbose (returns token!)
                         -s FILE  sets credentials file [default: ~/.isb_credentials]
                         -u       URL-only: for use over terminal connections;
                                  gives user a URL to paste into their browser,
                                  and asks for an auth code in return


2. script/ipython -- returns a credentials object; token can be used as an
                     argument to some endpoints URLs in the CGC APIs.
   import isb_auth
   token = isb_auth.get_credentials().access_token


3. Google Cloud Datalab -- run locally, on your home machine:
   python ./isb_auth.py -v

   Generates something like:
   --verbose: printing extra information
   credentials stored in /path/to/.isb_credentials
   access_token: <PASSWORD>...    >------+-- Copy & Paste this into datalab
   ...                                             |
                                                   |
   datalab> token = '<PASSWORD>...'  <---+


   See isb_curl.py help header for more information.
'''

from argparse import ArgumentParser
import os

from oauth2client.client import OAuth2WebServerFlow
from oauth2client import tools
from oauth2client.file import Storage

VERBOSE = False
# for native application - same as settings.INSTALLED_APP_CLIENT_ID
CLIENT_ID = '907668440978-0ol0griu70qkeb6k3gnn2vipfa5mgl60.apps.googleusercontent.com'
# NOTE: this is NOT actually a 'secret' -- we're using the 'installed
# application' OAuth pattern here
CLIENT_SECRET = '<KEY>'

EMAIL_SCOPE = 'https://www.googleapis.com/auth/userinfo.email'
DEFAULT_STORAGE_FILE = os.path.join(os.path.expanduser("~"), '.isb_credentials')


def maybe_print(msg):
    if VERBOSE:
        print msg


def get_credentials(storage=None, oauth_flow_args=[]):
    noweb = '--noauth_local_webserver'
    if __name__ != '__main__' and noweb not in oauth_flow_args:
        oauth_flow_args.append(noweb)
    if storage is None:
        storage = Storage(DEFAULT_STORAGE_FILE)
    credentials = storage.get()
{'fraction_non_alphanumeric': 0.07863826398566594, 'fraction_numerical': 0.011546884332072467, 'mean_word_length': 2.2020395156150414, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2182237', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1399, 'n_words': 471}