b2sdk.utils – Utils

class b2sdk.utils.B2TraceMeta[source]

Bases: logfury.v0_1.meta.DefaultTraceMeta

Traces all public method calls, except for ones with names that begin with get_

class b2sdk.utils.B2TraceMetaAbstract[source]

Bases: logfury.v0_1.meta.DefaultTraceAbstractMeta

Default class for tracers, to be set as a metaclass for abstract base classes

class b2sdk.utils.BytesIoContextManager(byte_data)[source]

Bases: object

A simple wrapper for a BytesIO that makes it look like a file-like object that can be a context manager.

__init__(byte_data)[source]
Parameters

bytes_data – a byte stream

class b2sdk.utils.TempDir[source]

Bases: object

Context manager that creates and destroys a temporary directory.

b2sdk.utils.b2_url_decode(s)[source]

Decodes a Unicode string returned from B2 in an HTTP header.

Parameters

s (str) – a unicode string to decode

Returns

a Python unicode string.

Return type

str

b2sdk.utils.b2_url_encode(s)[source]

URL-encodes a unicode string to be sent to B2 in an HTTP header.

Parameters

s (str) – a unicode string to encode

Returns

URL-encoded string

Return type

str

b2sdk.utils.camelcase_to_underscore(input_)[source]

Convert camel cased string to string with underscores

Parameters

input (str) – an input string

Returns

string with underscores

Return type

str

b2sdk.utils.choose_part_ranges(content_length, minimum_part_size)[source]

Returns a list of (offset, length) for the parts of a large file.

Parameters
  • content_length (int) – content length value

  • minimum_part_size (int) – a minimum file part size

Return type

list

b2sdk.utils.fix_windows_path_limit(path)[source]

Prefix paths when running on Windows to overcome 260 character path length limit See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath

Parameters

path (str) – a path to prefix

Returns

a prefixed path

Return type

str

b2sdk.utils.format_and_scale_fraction(numerator, denominator, unit)[source]

Picks a good scale for representing a fraction, and formats it.

Parameters
  • numerator (int) – a numerator of a fraction

  • denominator (int) – a denominator of a fraction

  • unit (str) – an arbitrary unit name

Returns

scaled and formatted fraction

Return type

str

b2sdk.utils.format_and_scale_number(x, unit)[source]

Picks a good scale for representing a number and formats it.

Parameters
  • x (int) – a number

  • unit (str) – an arbitrary unit name

Returns

scaled and formatted number

Return type

str

b2sdk.utils.hex_sha1_of_bytes(data)[source]

Returns the 40-character hex SHA1 checksum of the data

Parameters

data (bytes) – an array of bytes

Return type

str

b2sdk.utils.hex_sha1_of_stream(input_stream, content_length)[source]

Returns the 40-character hex SHA1 checksum of the first content_length bytes in the input stream.

Parameters
  • input_stream – stream object, which exposes read() method

  • content_length (int) – expected length of the stream

Return type

str

b2sdk.utils.interruptible_get_result(future)[source]

Waits for the result of a future in a way that can be interrupted by a KeyboardInterrupt.

This is not necessary in Python 3, but is needed for Python 2.

Parameters

future (Future) – a future to get result of

b2sdk.utils.is_file_readable(local_path, reporter=None)[source]

Check if the local file has read permissions

Parameters
  • local_path (str) – a file path

  • reporter – reporter object to put errors on

Return type

bool

b2sdk.utils.validate_b2_file_name(name)[source]

Raises a ValueError if the name is not a valid B2 file name.

Parameters

name (str) – a string to check