Skip to content

Storage

Shared by the client and server: MSFormat identifies a file's format, and StorePlan decides where a transferred file lands on disk and whether it is converted (store_as) on the way.

store

Decide where and in what format a transferred file is stored on disk.

MSFormat

Bases: StrEnum

An on-disk MS data format.

Members compare equal to their DownloadFormat string values.

ext property

ext: str

The canonical file extension, used to name converted outputs.

from_path classmethod

from_path(name: str) -> MSFormat | None

Infer the format from a filename extension, or None.

from_url classmethod

from_url(url: str) -> MSFormat | None

Infer the format from the URL filename extension, or None.

from_header classmethod

from_header(value: str) -> MSFormat

Parse an X-Source-Format header value, case-insensitively.

Raises ValueError for an unrecognised value.

Conversion dataclass

Conversion(source: MSFormat, target: MSFormat)

Turning a source-format file into a target-format file.

supported property

supported: bool

Whether a converter exists for this pair of formats.

run async

run(src: Path, dst: Path) -> None

Convert src to dst in a worker thread.

MszxExpansion dataclass

MszxExpansion(
    src: Path, entries: tuple[str, ...], target: MSFormat
)

Writes every member of a multi-member .mszx into one directory.

Each spectra entry of src becomes <entry stem>.<ext> stored as target (MSFormat.MSZ or MSFormat.MZML).

targets cached property

targets: list[tuple[str, str]]

Map each spectra entry to an output file name.

Raises ValueError for unsafe names, or if two entries map to the same output name.

write async

write(archive_dir: Path) -> Path

Store every member in archive_dir.

Members are extracted into a uniquely named staging directory that is renamed to archive_dir only once all of them succeed, so a failure leaves no partial output behind and concurrent downloads never share staging files. Names are validated before anything is written.

StorePlan dataclass

StorePlan(
    dest: Path,
    source: MSFormat | None,
    target: MSFormat | None,
)

How a file downloaded to dest ends up on disk.

source is the format of the downloaded bytes (or None if unknown) and target the requested store_as format (or None to keep the file as-is). Build one with for_download to also validate the combination before any I/O.

conversion property

conversion: Conversion | None

The conversion needed, or None if the file is stored as-is.

final_dest property

final_dest: Path

The output path, with its extension adjusted for any conversion.

part_path property

part_path: Path

Where the download is streamed before it is finalized.

may_expand property

may_expand: bool

Whether a multi-member .mszx would be expanded into a directory.

archive_dir property

archive_dir: Path

Where the members of a multi-member .mszx are written.

for_download classmethod

for_download(
    url: str, dest: Path, store_as: DownloadFormat | None
) -> StorePlan

Plan storage for url, failing fast on unsupported conversions.

for_upload classmethod

for_upload(
    dest_dir: Path,
    filename: str,
    source: MSFormat | None,
    store_as: MSFormat,
) -> StorePlan

Plan storage for an upload of filename into dest_dir.

source is the format the client declared, or None to infer it from filename. mzML is compressed on the wire, so anything but an .mszx archive arrives as MSZ. Archives are always kept as-is, whatever store_as says.

unique_part_path

unique_part_path() -> Path

A part_path variant that concurrent writers never share.

Two uploads can target the same dest (e.g. run.msz and run.mzML both arrive as run.msz), so each needs its own file.

existing

existing() -> Path | None

Return the output already on disk, if any.

finalize async

finalize(part: Path) -> Path

Move or convert the downloaded part file into its final form.

Returns the final destination, or the archive directory for a multi-member .mszx.