Esempio n. 1
0
def main():
    # setup
    store_file = devconfig.secrets('google', 'api', 'drive', 'store-file')
    if not Path(store_file).exists():
        SCOPES = 'https://www.googleapis.com/auth/drive.readonly'  # FIXME config this
        creds_file = devconfig.secrets('google', 'api', 'creds-file')
        get_oauth_service(store_file, creds_file, SCOPES)
Esempio n. 2
0
    def populate_annos(group_name='sparc-curation'):
        from hyputils import hypothesis as hyp
        if hyp.api_token == 'TOKEN':  # FIXME does not work
            hyp.api_token = devconfig.secrets('hypothesis', 'api',
                                              devconfig.hypothesis_api_user)

        group = devconfig.secrets('hypothesis', 'group', group_name)
        get_annos, annos, stream_thread, exit_loop = annoSync(
            group_to_memfile(group + 'sparcur'),
            helpers=(HypothesisHelper, Hybrid, protc),
            group=group,
            sync=False)

        [protc(a, annos) for a in annos]
        [Hybrid(a, annos) for a in annos]
Esempio n. 3
0
def update_sheet_values(spreadsheet_name, sheet_name, values, spreadsheet_service=None):
    SPREADSHEET_ID = devconfig.secrets('google', 'sheets', spreadsheet_name)  # FIXME wrong order ...
    if spreadsheet_service is None:
        service = get_oauth_service(readonly=False)
        ss = service.spreadsheets()
    else:
        ss = spreadsheet_service
    """
    requests = [
        {'updateCells': {
            'start': {'sheetId': TODO,
                      'rowIndex': 0,
                      'columnIndex': 0}
            'rows': {'values'}
        }
        }]
    response = ss.batchUpdate(
        spreadsheetId=SPREADSHEET_ID, range=sheet_name,
        body=body).execute()

    """
    body = {'values': values}

    response = ss.values().update(
        spreadsheetId=SPREADSHEET_ID, range=sheet_name,
        valueInputOption='USER_ENTERED', body=body).execute()

    return response
Esempio n. 4
0
def get_oauth_service(api='sheets', version='v4', readonly=True, SCOPES=None):
    if readonly:  # FIXME the division isn't so clean for drive ...
        store_file = devconfig.secrets('google', 'api', 'store-file-readonly')
    else:
        store_file = devconfig.secrets('google', 'api', 'store-file')

    store = file.Storage((spath / store_file).as_posix())
    creds = store.get()
    if not creds or creds.invalid:
        # the first time you run this you will need to use the --noauth_local_webserver args
        creds_file = devconfig.secrets('google', 'api', 'creds-file')
        flow = client.flow_from_clientsecrets((spath / creds_file).as_posix(), SCOPES)
        creds = tools.run_flow(flow, store)

    service = build(api, version, http=creds.authorize(Http()))
    return service
Esempio n. 5
0
 def setup(cls, creds_file=None):
     if creds_file is None:
         try:
             creds_file = devconfig.secrets('protocols-io', 'api',
                                            'creds-file')
         except KeyError as e:
             raise TypeError('creds_file is a required argument'
                             ' unless you have it in secrets') from e
     _pio_creds = get_protocols_io_auth(creds_file)
     cls._pio_header = QuietDict(
         {'Authorization': 'Bearer ' + _pio_creds.access_token})
Esempio n. 6
0
    def get_doc(self, doc_name, mimeType='text/plain'):
        file_id = devconfig.secrets('google', 'docs', doc_name)
        request = self.service.files().export_media(fileId=file_id,
                                                    mimeType=mimeType)
        fh = io.BytesIO()
        downloader = MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Download %d%%." % int(status.progress() * 100))

        fh.seek(0)
        return fh.read()
Esempio n. 7
0
def get_sheet_values(spreadsheet_name, sheet_name, fetch_grid=False, spreadsheet_service=None):
    SPREADSHEET_ID = devconfig.secrets('google', 'sheets', spreadsheet_name)
    if spreadsheet_service is None:
        service = get_oauth_service()
        ss = service.spreadsheets()
    else:
        ss = spreadsheet_service
    if fetch_grid:
        grid = ss.get(spreadsheetId=SPREADSHEET_ID, includeGridData=True).execute()
        notes = get_notes_from_grid(grid, sheet_name)
        notes_index = {(i, j):v for i, j, v in notes}
    else:
        grid = {}
        notes_index = {}

    result = ss.values().get(spreadsheetId=SPREADSHEET_ID, range=sheet_name).execute()
    values = result.get('values', [])
    return values, grid, notes_index
Esempio n. 8
0
 def get_doc_json(self, doc_name):
     file_id = devconfig.secrets('google', 'docs', doc_name)
     return self.service.documents().get(documentId=file_id,
                                         suggestionsViewMode='SUGGESTIONS_INLINE').execute()
def _store_file():
    try:
        return devconfig.secrets('protocols-io', 'api', 'store-file')
    except KeyError:
        return 'protocols-io-api-token-rw.json'