Beispiel #1
0
def batch(
    arguments: List[BatchArgument],
    *,
    uniform_mime_type: str,
    include_output: bool = True,
    drive_service: Optional[discovery.Resource] = None,
) -> List[drive_api.ResourceID]:
    """
    Batch create Google Drive file with specific MIME type.

    Returns list of IDs in order of passed names.
    """

    # callback will append resulting IDs in order
    result: List[drive_api.ResourceID] = []

    def batch_response(request_id, response, exception) -> None:
        nonlocal result
        result.append(response.get("id"))

    requests = [
        request(
            name=argument.name,
            mime_type=uniform_mime_type,
            parent_folder_id=argument.parent_folder_id,
            drive_service=drive_service,
        ) for argument in arguments
    ]
    kwargs = {"requests": requests, "drive_service": drive_service}
    if include_output:
        kwargs["callback"] = batch_response
    drive_api.batch_command(**kwargs)
    return result
Beispiel #2
0
def batch(
    arguments: List[BatchArgument],
    *,
    include_output: bool = True,
    drive_service: Optional[discovery.Resource] = None,
) -> List[drive_api.ResourceID]:
    """
    Batch copy Google Drive files.

    Returns list of IDs in order of passed names.
    """
    result = []  # callback will append resulting IDs in order

    def batch_response(request_id, response, exception) -> None:
        nonlocal result
        result.append(response.get("id"))

    requests = [
        request(
            origin_file_id=argument.origin_resource_id,
            new_name=argument.new_name,
            target_parent_folder_id=argument.target_folder_id,
            drive_service=drive_service,
        )
        for argument in arguments
    ]
    kwargs = {"requests": requests, "drive_service": drive_service}
    if include_output:
        kwargs["callback"] = batch_response
    drive_api.batch_command(**kwargs)
    return result
Beispiel #3
0
def batch(
    arguments: List[BatchArgument],
    *,
    uniform_email_message: Optional[str] = None,
    uniform_send_email: bool = False,
    uniform_transfer_ownership: bool = False,
    drive_service: Optional[discovery.Resource] = None,
) -> None:
    """
    Batch share resources with provided users.
    """
    # convert uniform parameters
    if uniform_email_message is not None:
        arguments = [
            BatchArgument(
                resource_id=argument.resource_id,
                emails=argument.emails,
                email_message=uniform_email_message,
                send_email=argument.send_email,
                transfer_ownership=argument.transfer_ownership,
            ) for argument in arguments
        ]
    if uniform_send_email is True:
        arguments = [
            BatchArgument(
                resource_id=argument.resource_id,
                emails=argument.emails,
                email_message=argument.email_message,
                send_email=True,
                transfer_ownership=argument.transfer_ownership,
            ) for argument in arguments
        ]
    if uniform_transfer_ownership is True:
        arguments = [
            BatchArgument(
                resource_id=argument.resource_id,
                emails=argument.emails,
                email_message=argument.email_message,
                send_email=True,
                transfer_ownership=True,
            ) for argument in arguments
        ]
    # generate requests
    requests = [[
        request(
            resource_id=argument.resource_id,
            email=email,
            email_message=argument.email_message,
            send_email=argument.send_email,
            transfer_ownership=argument.transfer_ownership,
            drive_service=drive_service,
        ) for email in argument.emails
    ] for argument in arguments]
    requests_flattened = [x for y in requests for x in y]
    # execute
    drive_api.batch_command(requests=requests_flattened,
                            drive_service=drive_service)
Beispiel #4
0
def batch(
    arguments: List[BatchArgument],
    *,
    drive_service: Optional[discovery.Resource] = None,
) -> None:
    """
    Batch remove multiple resources.
    """
    requests = [
        request(resource_id=argument.resource_id, drive_service=drive_service)
        for argument in arguments
    ]
    drive_api.batch_command(requests=requests, drive_service=drive_service)
Beispiel #5
0
def linked_sheet_and_form(
    *,
    origin_sheet_id: drive_api.ResourceID,
    origin_form_id: drive_api.ResourceID,
    origin_parent_folder_id: Optional[drive_api.ResourceID] = None,
    new_sheet_name: str,
    new_form_name: str,
    target_parent_folder_id: drive_api.ResourceID,
    drive_service: Optional[discovery.Resource] = None,
    initial_form_search_delay: int = 10,
    timeout: int = 45,
) -> SheetAndForm:
    """
    Copy both the spreadsheet and its accompanying form.

    This is because when you copy a spreadsheet linked to a form, the form will automatically be copied as well.
    So, this command will find that copied form, rename it, and move to the same parent.

    Because the form is not created instantly, uses a timeout mechanism to search multiple times.
    """
    if drive_service is None:
        drive_service = drive_api.build_service()
    if origin_parent_folder_id is None:
        origin_parent_folder_id = find.parent_folder(origin_form_id)
    original_form_name = find.name(origin_form_id)
    copied_sheet_id = file(
        origin_file_id=origin_sheet_id,
        target_parent_folder_id=target_parent_folder_id,
        new_name=new_sheet_name,
        drive_service=drive_service,
    )

    def find_copied_form(
        time_delay: int, total_time_elapsed: int = 0
    ) -> drive_api.ResourceID:
        time.sleep(time_delay)
        copy_id = find.resource(
            resource_name=f"Copy of {original_form_name}",
            parent_folder_id=origin_parent_folder_id,
            mime_type=mime_types.gform,
            drive_service=drive_service,
        )
        updated_time_elapsed = total_time_elapsed + time_delay
        time_remaining = timeout - updated_time_elapsed
        if copy_id is None:
            if time_remaining > timeout:
                raise OSError(
                    "Cannot find the copied form. Check that it was created in Google Drive."
                )
            print(
                textwrap.dedent(
                    f"""\
                Copy of the form {original_form_name} not found yet.
                Trying again, this time waiting {time_delay*2} seconds.
                Will timeout in {time_remaining} seconds."""
                )
            )
            return find_copied_form(
                time_delay * 2, total_time_elapsed=updated_time_elapsed
            )
        return copy_id

    # use time delay to make sure form created
    copied_form_id = find_copied_form(initial_form_search_delay)

    drive_api.batch_command(
        requests=[
            rename.request(
                resource_id=copied_form_id,
                new_name=new_form_name,
                drive_service=drive_service,
            ),
            move.request(
                origin_resource_id=copied_form_id,
                target_folder_id=target_parent_folder_id,
                drive_service=drive_service,
            ),
        ],
        drive_service=drive_service,
    )
    return SheetAndForm(sheet=copied_sheet_id, form=copied_form_id)