s365b.modules.exchange.restore

Restoring mail, calendar events, contacts and tasks with Exchange’s import API.

Each item’s restore copy (Exchange’s own export, see the module) is imported into a folder of the target mailbox, so it comes back as it was: read state, dates, flags and attachments, and not as a draft. Imported meetings send no invitations.

Folders are found by name along the item’s backed-up path and created when missing, either where they were (“original folders”) or under a new folder named by the person restoring. Mail goes into mail folders, events into calendars, contacts into contact folders and tasks into task folders (To Do lists). Calendars and task lists other than the default ones are looked for at the top of the mailbox and under the default calendar or task folder, and created under it (as Outlook does). A “new folder” restore puts events in a new calendar under the default calendar, tasks in a new list under the default task folder, and contacts in a new folder under the default contacts folder, so they do not clash with the new mail folder of the same name. The import API identifies folders by its own IDs (/admin/exchange/mailboxes/{id}/folders), not by the Outlook API’s.

Attributes

Classes

Folders

A mailbox's folders, read as needed, created when missing.

ImportSession

An import session's pre-authenticated URL, renewed before it expires.

Functions

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

Folder names along a backed-up path ("Inbox/Projects" → two names).

still_in_place(reader, user, item) → bool

Whether an item is still in the mailbox as it was backed up.

default_names(reader, user) → dict[str, collections.abc.Callable[[], collections.abc.Awaitable[str | None]]]

How to learn the names of a mailbox's default calendar, contacts and tasks folders.

restore_mail(ctx, request, mailbox_id) → s365b.modules.sdk.RestoreResult

Import messages', events' and contacts' restore copies into a mailbox.

Module Contents

s365b.modules.exchange.restore.FTS = 'fts'[source]
s365b.modules.exchange.restore.IMMUTABLE[source]
s365b.modules.exchange.restore.FOLDER_TYPE = 'IPF.Note'[source]
s365b.modules.exchange.restore.FOLDER_TYPES[source]
s365b.modules.exchange.restore.IMPORT_PARALLEL = 4[source]
s365b.modules.exchange.restore.CHUNK = 100[source]
s365b.modules.exchange.restore.SESSION_MARGIN[source]
s365b.modules.exchange.restore.SESSION_FALLBACK[source]
s365b.modules.exchange.restore.FALLBACK_FOLDER = 'Inbox'[source]
s365b.modules.exchange.restore.PERMISSION_HINT = 'The Restore app may not write to this mailbox. In Microsoft 365 tenant, choose Update permissions.'[source]
s365b.modules.exchange.restore.split_path(path: str) → tuple[str, ...][source]

Folder names along a backed-up path (“Inbox/Projects” → two names).

class s365b.modules.exchange.restore.Folders(ctx: s365b.modules.sdk.ModuleContext, mailbox: str, folder_type: str = FOLDER_TYPE, default_name: collections.abc.Callable[[], collections.abc.Awaitable[str | None]] | None = None)[source]

A mailbox’s folders, read as needed, created when missing.

Parameters:
  • ctx – Module context (Restore app).

  • mailbox – Mailbox ID for the import API.

  • folder_type – The kind of folder: IPF.Note (mail), IPF.Appointment (calendar), IPF.Contact or IPF.Task. Folders of other kinds are ignored, and new folders are of this kind.

  • default_name – Returns the name of the mailbox’s default folder of this kind (e.g. “Calendar”, or its translation), or None if it cannot be read.

ctx[source]
base[source]
folder_type = 'IPF.Note'[source]
default_name = None[source]
created: list[str] = [][source]
async default() → str[source]

The default folder of this kind, e.g. Calendar.

The folder at the top with the default folder’s name; without that name, the first one at the top.

Returns:

Its import API ID.

Raises:

ModuleError – If the mailbox has none.

async named(name: str) → str[source]

A calendar or task list by name: at the top, else under the default one.

One that is missing is created under the default folder.

Parameters:

name – The calendar’s or list’s name.

Returns:

Its import API ID.

async ensure(parts: tuple[str, ...], under: str = '') → str[source]

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

Parameters:
  • parts – Folder names from the top of the mailbox, or from under.

  • under – The folder to start from (default: the top).

Returns:

The import API’s ID of the last folder.

class s365b.modules.exchange.restore.ImportSession(ctx: s365b.modules.sdk.ModuleContext, mailbox: str)[source]

An import session’s pre-authenticated URL, renewed before it expires.

Parameters:
  • ctx – Module context (Restore app).

  • mailbox – Mailbox ID.

ctx[source]
mailbox[source]
async url(*, renew: bool = False) → str[source]

The current import URL (a new session when needed).

async put(folder_id: str, data: bytes) → str[source]

Import one item into a folder.

Parameters:
  • folder_id – Import API folder ID.

  • data – The item’s export stream.

Returns:

The new item’s ID.

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

Whether an item is still in the mailbox as it was backed up.

A message counts as still there in the folder it was backed up in. An event, contact or task must also be unchanged: restoring last week’s version of an event edited since adds that version back.

An item that was moved (e.g. to Deleted Items) or deleted is restored.

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

  • user – The mailbox’s user ID.

  • item – The backed-up message, event, contact or task.

Returns:

True if it is still there, unchanged.

s365b.modules.exchange.restore.default_names(reader: s365b.graph.client.GraphClient | None, user: str) → dict[str, collections.abc.Callable[[], collections.abc.Awaitable[str | None]]][source]

How to learn the names of a mailbox’s default calendar, contacts and tasks folders.

Read with the Backup app (the import API does not mark them); None if it cannot.

Parameters:
  • reader – Graph client with read access, if any.

  • user – The mailbox’s user ID.

Returns:

A name reader by item type.

async s365b.modules.exchange.restore.restore_mail(ctx: s365b.modules.sdk.ModuleContext, request: s365b.modules.sdk.RestoreRequest, mailbox_id: Any) → s365b.modules.sdk.RestoreResult[source]

Import messages’, events’ and contacts’ restore copies into a mailbox.

Options: destination is "original" (the folders they were in) or "folder" (under a new folder named folder_name, keeping the folder structure; see the module for events and contacts). Restoring to the original folders of the same mailbox skips items that are still where they were.

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

  • request – Messages and where to put them.

  • mailbox_id – Coroutine function returning a user’s mailbox ID.

Returns:

The outcome.

Raises:

ModuleError – If the options are wrong or the Restore app lacks permissions.