maestral.utils

Module contents

Utility modules and functions

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 an iterable into chunks of length n.

Parameters
  • lst (list) – Iterable to partition.

  • n (int) – Chunk size.

  • consume (bool) – If True, the list will be consumed (emptied) during the iteration. This can be used to free memory in case of large lists.

Returns

Iterator over chunks.

Return type

Iterator[list]

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

Clamps a number between a minimum and maximum value.

Parameters
  • n (maestral.utils._N) – Original value.

  • minn (maestral.utils._N) – Minimum allowed value.

  • maxn (maestral.utils._N) – Maximum allowed value.

Returns

Clamped value.

Return type

maestral.utils._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)[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.

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

Tuple[Type[BaseException], BaseException, Optional[types.TracebackType]]