maestral.utils

Utility modules and functions

Submodules

Package Contents

maestral.utils.natural_size(num, unit='B', sep=True)[source]

Convert number to a human-readable string with decimal prefix.

Parameters:
  • num (float) – Value in given unit.

  • unit (str) – Unit suffix.

  • sep (bool) – Whether to separate unit and value with a space.

Returns:

Human-readable string with decimal prefixes.

Return type:

str

maestral.utils.chunks(lst, n, consume=False)[source]

Partitions a list into chunks of length n.

Parameters:
  • lst (list[_T]) – Iterable to partition.

  • n (int) – Chunk size.

  • consume (bool) – If True, the list will be consumed (emptied) during the iteration.

Returns:

Iterator over chunks.

Return type:

Iterator[list[_T]]

maestral.utils.clamp(n, minn, maxn)[source]

Clamps a number between a minimum and maximum value.

Parameters:
  • n (_N) – Original value.

  • minn (_N) – Minimum allowed value.

  • maxn (_N) – Maximum allowed value.

Returns:

Clamped value.

Return type:

_N

maestral.utils.get_newer_version(version, releases)[source]

Checks a given release version against a version list of releases to see if an update is available. Only offers newer versions if they are not a prerelease.

Parameters:
  • version (str) – The current version.

  • releases (Iterable[str]) – A list of valid cleaned releases.

Returns:

The version string of the latest release if a newer release is available.

Return type:

Optional[str]

maestral.utils.removeprefix(string, prefix, case_sensitive=True)[source]

Removes the given prefix from a string. Only the first instance of the prefix is removed. The original string is returned if it does not start with the given prefix.

This follows the Python 3.9 implementation of str.removeprefix.

Parameters:
  • string (str) – Original string.

  • prefix (str) – Prefix to remove.

  • case_sensitive (bool) – Whether to do case-sensitive prefix removal.

Returns:

String without prefix.

Return type:

str

maestral.utils.sanitize_string(string)[source]

Converts a string provided by file system APIs, which may contain surrogate escapes for bytes with unknown encoding, to a string which can always be displayed or printed. This is done by replacing invalid characters with “�”.

Parameters:

string (str) – Original string.

Returns:

Sanitised path where all surrogate escapes have been replaced with “�”.

Return type:

str

maestral.utils.exc_info_tuple(exc)[source]

Creates an exc-info tuple from an exception.

Parameters:

exc (BaseException)

Return type:

_ExecInfoType