aiobtclientapi.utils

Utilities

Functions

async aiobtclientapi.utils.download(url, to=None, maxsize=None)[source]

Download URL to file or return bytes

Parameters:
  • url – URL to download

  • to – File path to store bytes from url in

  • maxsize – Maximum allowed Content-Length or None for unlimited download size

Raises:

ReadError – if anything goes wrong

aiobtclientapi.utils.is_infohash(string)[source]

Return True if string looks like a torrent infohash, False otherwise

aiobtclientapi.utils.is_magnet(string)[source]

Return True if string is a magnet URI, False otherwise

aiobtclientapi.utils.is_url(string)[source]

Return True if string looks like an URL, False otherwise

async aiobtclientapi.utils.merge_async_generators(*generators)[source]

Combine multiple asynchronous generators into one

Every generated value is wrapped in a coroutine that returns it or raises an exception raised by the generator.

Example:

>>> async for coro in merge_async_generators(a, b, c):
>>>     try:
>>>         print('Good value:', await coro)
>>>     except ValueError as e:
>>>         print('Bad value:', e)
aiobtclientapi.utils.partial(*args, **kwargs)[source]
aiobtclientapi.utils.read_bytes(path, maxsize=None)[source]

Return bytes from file

Parameters:
  • path – Path to file

  • maxsize – Maximum size of path in bytes

Raises:

ReadError – if reading path fails or size of path exceeds maxsize bytes

aiobtclientapi.utils.without_None_values(dct)[source]

Return copy of dct without the keys that map to None

Classes

class aiobtclientapi.utils.DynamicInterval(min, max, progress_getter, name=None)[source]

Bases: object

Generate intervals from min to max depending on some ongoing operation

Parameters:
  • min – Minimum interval

  • max – Maximum interval

  • progress_getter

    Callable that takes no arguments and returns a number from 0 to 100

    As progress approaches 100, the interval returned by next() gets closer to min.

  • name – Any object with a descriptive string representation (only used for debugging)

property progress

Most recent return value from progress_getter

async sleep()[source]

sleep() if next() returns positive interval

async next()[source]

Return next delay

class aiobtclientapi.utils.Infohash(string)[source]

Bases: str

Case-insensitive string of exactly 40 hexadecimal digits

class aiobtclientapi.utils.Monitor(call, attribute=None, interval=1, timeout=None)[source]

Bases: object

Continuously call awaitable until it returns a predefined value

Parameters:
  • call – Any awaitable that doesn’t take any arguments

  • attribute – Attribute from the return value of call to act on, e.g. if call returns a Response object and you want to monitor its success attribute

  • interval (DynamicInterval instance or int) – Seconds to sleep before calling call again

  • timeout – Maximum number of seconds overall before TimeoutError is raised

async return_value_equals(value, negate=False)[source]

Block until await call() == value

Parameters:

negate – Invert the result of the comparison

async return_value_is(value, negate=False)[source]

Block until await call() is value

Parameters:

negate – Invert the result of the comparison

async return_value_contains(value, negate=False)[source]

Block until value in (await call())

Parameters:

negate – Invert the result of the comparison

async return_value_validates(validator, negate=False)[source]

Block until validator(await call()) is truthy

Parameters:

negate – Invert the result of the comparison