s365b.modules.onedrive.restore

Restore files into a OneDrive.

Files go back into their folders (created where missing), in the drive they came from or another user’s, or under a new folder at the top of the drive. A name already taken is not overwritten: OneDrive gives the restored file a new name (“report 1.pdf”). Restoring into the original folders of the same drive skips files that are still there unchanged.

Content is uploaded in pieces through an upload session, so a file is never held in memory whole; its modification time is restored too. A file counts as restored only when OneDrive confirms it complete at its size. A stop request is honoured between files and between pieces of one; what was restored by then is reported.

Attributes

Exceptions

Stopped

The run was asked to stop during an upload.

Classes

Folders

Folders of a drive by path, created when missing.

Functions

pieces(source[, size]) → collections.abc.AsyncIterator[bytes]

Regroup a stream into pieces of exactly size bytes (the last shorter).

onedrive_names(path) → tuple[str, ...]

A stored folder path as OneDrive folder names.

still_in_place(reader, drive, item) → bool

Whether a file is still where it was backed up, with the same content.

upload(graph, drive, folder, item[, cancel]) → None

Upload one file into a folder, renamed if the name is taken.

restore_files(ctx, request) → s365b.modules.sdk.RestoreResult

Upload backed-up files into a drive.

Module Contents

s365b.modules.onedrive.restore.PIECE = 10485760[source]
s365b.modules.onedrive.restore.RENAME[source]
s365b.modules.onedrive.restore.PERMISSION_HINT = 'The Restore app may not write to OneDrive. In Microsoft 365 tenant, choose Update permissions.'[source]
async s365b.modules.onedrive.restore.pieces(source: collections.abc.AsyncIterator[bytes], size: int = PIECE) → collections.abc.AsyncIterator[bytes][source]

Regroup a stream into pieces of exactly size bytes (the last shorter).

Parameters:
  • source – The content, in pieces of any size.

  • size – Piece size.

Yields:

The pieces.

exception s365b.modules.onedrive.restore.Stopped[source]

Bases: Exception

The run was asked to stop during an upload.

s365b.modules.onedrive.restore.onedrive_names(path: str) → tuple[str, ...][source]

A stored folder path as OneDrive folder names.

OneDrive names cannot hold /, so a / after decoding stood for a real division slash (U+2215) in the name, which goes back as it was.

Parameters:

path – The stored path.

Returns:

The folder names.

class s365b.modules.onedrive.restore.Folders(graph: s365b.graph.GraphClient, drive: str, created: list[str])[source]

Folders of a drive by path, created when missing.

ids: dict[tuple[str, ...], str][source]
async ensure(path: tuple[str, ...]) → str[source]

Return the ID of the folder at path, creating what is missing.

Parameters:

path – Folder names from the drive’s root; empty for the root.

Returns:

The folder’s item ID.

async s365b.modules.onedrive.restore.still_in_place(reader: s365b.graph.GraphClient, drive: str, item: s365b.modules.sdk.StoredItem) → bool[source]

Whether a file is still where it was backed up, with the same content.

Looked up by its backed-up path, so a file whose folder (or a folder above it) was renamed or moved since is not there, and is restored. The file found there counts if it is the same item with the same content tag, or (restored before, so with a new ID) has the same size and content hash.

Parameters:
  • reader – Client with read access (the Backup app).

  • drive – The drive’s ID.

  • item – The backed-up file.

Returns:

True if restoring it would only double it.

async s365b.modules.onedrive.restore.upload(graph: s365b.graph.GraphClient, drive: str, folder: str, item: s365b.modules.sdk.StoredItem, cancel: asyncio.Event | None = None) → None[source]

Upload one file into a folder, renamed if the name is taken.

Parameters:
  • graph – Client that may write to the drive.

  • drive – Drive ID.

  • folder – The folder’s item ID.

  • item – The backed-up file.

  • cancel – Set when the run is asked to stop.

Raises:
  • Stopped – If the run was asked to stop; the upload is abandoned.

  • ModuleError – If OneDrive did not confirm the file complete.

async s365b.modules.onedrive.restore.restore_files(ctx: s365b.modules.sdk.ModuleContext, request: s365b.modules.sdk.RestoreRequest) → s365b.modules.sdk.RestoreResult[source]

Upload backed-up files into a drive.

Options: destination is "original" (the folders they were in) or "folder" (under a new folder named folder_name at the top of the drive, keeping the folder structure).

Parameters:
  • ctx – Module context with the Restore app’s client (and reader).

  • request – Files and where to put them.

Returns:

The outcome.

Raises:

ModuleError – If the options are wrong, the target has no OneDrive or the Restore app may not write to it.