#!/usr/bin/env python

"""Create Google API credentials

Usage:
  google_api_auth.py [NAME]

Options:
  -h --help         Show usage

"""
from __future__ import print_function

import os.path
import sys
import argparse

from docopt import docopt
try:
    # Python 2
   input = raw_input
except NameError:
   pass
from flask import Flask
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from oauth2client import tools


app = Flask(__name__, instance_relative_config=True,
            instance_path=os.path.abspath(os.path.join(
                os.path.dirname(__file__), '..', 'app')))
app.config.from_envvar('JARVIS_SETTINGS')


def get_config(name):
    return app.config['JOBS'][name]


def create_credentials(name):
    config = get_config(name)
    if 'client_id' not in config \
            or 'client_secret' not in config:
        print('Error: client_id, client_secret is not set.\n\n'
              'Please create a client ID here and update '
              'config.py:\n\nhttps://code.google.com/apis/console/#:access')
        sys.exit(1)

    FLOW = OAuth2WebServerFlow(
        client_id=config['client_id'],
        client_secret=config['client_secret'],
        scope='https://www.googleapis.com/auth/{}.readonly'.format(name))

    parser = argparse.ArgumentParser(parents=[tools.argparser])
    run_flags = parser.parse_args()

    credentials_file = os.path.join(app.instance_path, 'jobs',
                                    '.{}.json'.format(name))
    storage = Storage(credentials_file)
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        credentials = tools.run_flow(FLOW, storage, run_flags)
    else:
        print('Google API credentials already exist: %s' % (credentials_file,))


def main():
    args = docopt(__doc__)
    name = args['NAME']

    if name is None:
        name = input(('Enter widget to generate credentials for (gmail'
                      ' or calendar): '))

    if name not in ('calendar', 'gmail'):
        print('Name must be either \'gmail\' or \'calendar\'')
        sys.exit(1)

    create_credentials(name)


if __name__ == '__main__':
    main()

@@ -20,123 +20,8 @@
 on%0A%0A
-%22%22%22Create Google API credentials%0A%0AUsage:%0A  google_api_auth.py %5BNAME%5D%0A%0AOptions:%0A  -h --help         Show usage%0A%0A%22%22%22%0A
 from
@@ -102,34 +102,8 @@
 se%0A%0A
-from docopt import docopt%0A
 try:
@@ -161,16 +161,16 @@
 or:%0A
-
    pass%0A
 from
@@ -165,16 +165,17 @@
    pass%0A
+%0A
 from fla
@@ -1147,102 +1147,54 @@
     
-parser = argparse.ArgumentParser(parents=%5Btools.argparser%5D)%0A    run_flags = parser.parse_
+run_flags = tools.argparser.parse_args(
 args
-(
+=%5B%5D
 )%0A%0A 
@@ -1614,29 +1614,175 @@
     
+p
 ar
-gs = docopt(__doc__
+ser = argparse.ArgumentParser(description='Create Google API credentials.')%0A    parser.add_argument('name', metavar='NAME', nargs='?')%0A    args = parser.parse_args(
 )%0A  
@@ -1798,16 +1798,13 @@
 args
-%5B'NAME'%5D
+.name
 %0A%0A  

Fix Google credentials script