import argparse
import os
import os.path
import pickle
import sys

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import luigi
import pandas as pd

SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']

def _authenticate():
    # authentication
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_console()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)
    return creds

def retrieve_spreadsheet(spreadsheet_id, sheet_name):
    service = build('sheets', 'v4', credentials=_authenticate())

    # Retrieve the documents contents from the Docs service.
    rnaseq_pipeline_queue = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=sheet_name).execute()

    # this will fail if people add new columns
    df = pd.DataFrame(rnaseq_pipeline_queue['values'][1:], columns=rnaseq_pipeline_queue['values'][0]+list(range(5)))

    # type adjustment
    df['priority'] = df.priority.fillna(0).replace('', '0').astype('int')

    return df

@@ -9,16 +9,31 @@
 rgparse%0A
+import logging%0A
 import o
@@ -34,16 +34,16 @@
 port os%0A
-
 import o
@@ -73,16 +73,62 @@
 port sys
+%0Afrom os.path import dirname, expanduser, join
 %0A%0Afrom g
@@ -303,16 +303,41 @@
 as as pd
+%0Aimport xdg.BaseDirectory
 %0A%0ASCOPES
@@ -398,16 +398,63 @@
 only'%5D%0A%0A
+logger = logging.getLogger('luigi-interface')%0A%0A
 def _aut
@@ -684,40 +684,134 @@
     
-if os.path.exists('token.pickle'
+token_path = join(xdg.BaseDirectory.save_data_path('pavlab-rnaseq-pipeline'), 'token.pickle')%0A    if os.path.exists(token_path
 ):%0A 
@@ -827,30 +827,26 @@
 th open(
-'
 token
-.pickle'
+_path
 , 'rb') 
@@ -1327,30 +1327,26 @@
 th open(
-'
 token
-.pickle'
+_path
 , 'wb') 
@@ -1389,24 +1389,104 @@
 eds, token)%0A
+            logger.info(f'Created Google Sheets API token under %7Btoken_path%7D.')%0A
     return c

Store token under XDG data directory