def get_team_drive(service: Resource): """ Get the aerial baboons team drive. """ results = ( service.drives() # pylint: disable=maybe-no-member .list(fields="nextPageToken, drives(id, name)").execute()) return [ d for d in results.get("drives", []) if d["name"] == "E4E_Aerial_Baboons" ][0]
def select_teamdrive(service: Resource) -> str: """ Allows the user to select the teamdrive for which strm files are to be generated. Will be used to let the user select a source incase a direct id is not supplied. Remarks -------- Will internally handle any error/unexpected input. The only value returned by this method will be the id of the teamdrive that is to be used. Returns -------- String containing the ID of the teamdrive selected by the user. """ # Initializing as non-zero integer to ensure that the loop runs once. nextPageToken: Optional[str] = None # Creating a list with the first element filled. Since the numbers being displayed # on the screen start from 1, filling the first element of the list with trash # ensures that the input from the user can used directly. teamdrives: List[str] = [''] counter: int = 1 while True: result = service.drives().list( pageSize=100, pageToken=nextPageToken ).execute() for item in result['drives']: td_list = f' {counter} ' + ('/' if counter % 2 else '\\') td_list += f'\t{item["name"]}{Style.RESET_ALL}' print(f'{Fore.GREEN if counter % 2 else Fore.CYAN}{td_list}') # Adding the id for this teamdrive to the list of teamdrives. teamdrives.append(item['id']) # Finally, incrementing the counter counter += 1 try: nextPageToken = result['nextPageToken'] if not nextPageToken: break except KeyError: # Key error will occur if there is no next page token -- breaking out of # the loop in such scenario. break # Adding an extra line. print() while True: print('Select a teamdrive from the list above.') try: td_id = input('teamdrive> ') if not match(r'^[0-9]+$', td_id): # Handling the scenario if the input is not an integer. Using regex # since direct type-cast to float will also accept floating-point, # which would be incorrect. raise ValueError td_id = int(td_id) if td_id <= 0 or td_id > len(teamdrives): # Handling the scenario if the input is not within the accepted range. # The zero-eth element of this list is garbage value, thus discarding # the input even at zero. raise ValueError # If the flow-of-control reaches here, the returning the id of the teamdrive # located at this position. return teamdrives[td_id] except ValueError: # Will reach here if the user enters a non-integer input print(f'\t{Fore.RED}Incorrect input detected. {Style.RESET_ALL}\n')