Utility classes and functions

momotor.bundles.utils.ascii

Constants for ASCII characters

momotor.bundles.utils.content

momotor.bundles.utils.content.decode_content(node, encoding)
momotor.bundles.utils.content.encode_content(content)

momotor.bundles.utils.dependencies

momotor.bundles.utils.dependencies.get_complete_step_dependencies(steps)
Return type

Tuple[Dict[str, Set[str]], Dict[str, Set[str]]]

momotor.bundles.utils.encoding

Data encoding and decoding

momotor.bundles.utils.encoding.ENCODINGS

List of all supported encodings

momotor.bundles.utils.encoding.decode_data(data, encoding)

Decode encoded data.

Parameters
  • data (str) – Data to decode

  • encoding (str) – The encoding used on the data: quopri or base64

Return type

bytes

Returns

The decoded data

Raises

ValueError – For unsupported encodings

momotor.bundles.utils.encoding.encode_data(data)

Encode provided data.

Printable data is encoded using Quoted-printable encoding, any other data is encoded using Base64 encoding.

Parameters

data (str) – Data to encode

Return type

tuple

Returns

tuple(encoded, encoding)

  • encoded: The encoded data

  • encoding (str): The encoding used. quopri or base64

momotor.bundles.utils.encoding.encode_posix_path(path)

Encode a path using idna encoding to ensure it will only contain ascii characters

Return type

PurePosixPath

momotor.bundles.utils.encoding.is_printable(s, low=32, allowed=None)

Detect if string is printable; it should not contain any control characters, except those in allowed, and less than 5% of the characters are allowed to be high-ascii

momotor.bundles.utils.encoding.make_pure_ascii(s)
Return type

str

momotor.bundles.utils.encoding.quopri_decode(data)

Decode quoted printable data

momotor.bundles.utils.encoding.quopri_encode(data)

Encode data using quoted printable method Uses a different algorithm than quopri.encodestring():

  • Encodes first space character on a line, but not the rest

  • Encodes newline, return characters and high-ascii

momotor.bundles.utils.keyedlist

class momotor.bundles.utils.keyedlist.KeyedList(items=None, *, key_attr=None)

A list of objects with an indexable attribute that acts as both a sequence and a mapping. Elements can be accessed by their numeric index or the key attribute.

Parameters

If items is another KeyedList, key_attr must either be the same as the key_attr of items, or not provided.

append(item)

append item to the end of the sequence.

Parameters

item (~IT) – the items to add to the sequence, must have a key attribute which does not yet exist in this list

Return type

None

clear()

Remove all items

Return type

None

copy()

Create a shallow copy

Return type

KeyedList[~IT]

count(item)
Return type

int

extend(items)

Extend sequence by appending elements

Parameters

items (Union[KeyedList[~IT], Mapping[str, ~IT], Sequence[~IT]]) – the items to add to the sequence. Can be a KeyedList, sequence or mapping

If items is a KeyedList, it must be compatible, ie. have the same key_attr value

get(key_or_index, default=None)

Get an item from the sequence by index, key or item

Parameters
  • key_or_index (Union[str, int, ~IT]) – index, key or item

  • default – the value to return if item does not exist

Returns

the item or default

index(value[, start[, stop]]) → integer – return first index of value.

Raises ValueError if the value is not present.

Supporting start and stop arguments is optional, but recommended.

insert(key_or_index, value)

insertion is not supported

Return type

None

items()

A set-like object providing a view on the items

Return type

ItemsView[str, ~IT]

keys()

A set-like object providing a view on the keys

Return type

KeysView[str]

pop(key_or_index: Union[str, int, IT]) → IT

Pop an item from the sequence by index, key or item

Parameters
  • key_or_index (Union[str, int, ~IT]) – index, key or item

  • default – the value to return if item does not exist

Return type

~IT

Returns

the item or default. If default is not provided and item does not exist, raises KeyError

popitem(last=True)

Remove and return a (key, value) pair from the dictionary.

Pairs are returned in LIFO order if last is true or FIFO order if false.

Return type

Tuple[str, ~IT]

remove(item)

S.remove(value) – remove first occurrence of value. Raise ValueError if the value is not present.

reverse()

S.reverse() – reverse IN PLACE

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values()

A set-like object providing a view on the values

Return type

ValuesView[~IT]

momotor.bundles.utils.zipwrapper

class momotor.bundles.utils.zipwrapper.ZipWrapper(*, path=None, content=None)

A wrapper around a ZipFile. The zip file can be either located in the filesystem or in memory.

Example usage:

zip_wrapper = ZipWrapper('test.zip')
with zip_wrapper as zip_file, zip_file.open() as f:
    # f is now an open zipfile.ZipFile object
Parameters
close()

Close the wrapped zip file