Transformers

This page describe internal utility classes use to parse or help with converting data from Tor to python structures. Most of these classes rely on pydantic to handle the heavy lifting.

class aiostem.utils.transformers.TrAfterAsTimezone[source]

Post-validator that enforces a timezone.

timezone: tzinfo

Timezone to map this date to.

from_value(value: datetime) datetime[source]

Apply the timezone of change the offset.

Parameters:

value (datetime) – The original datetime to change.

Returns:

datetime – A new datetime with the proper timezone applied.

class aiostem.utils.transformers.TrBeforeSetToNone[source]

Pre-validator that sets a value to None.

values: Set[Any]

List of values mapped to None.

from_value(value: Any) Any[source]

Set the return value to None when applicable.

Parameters:

value (Any) – The value to check against values.

Returns:

Any – The same value of None when matching any value in values.

class aiostem.utils.transformers.TrBeforeStringSplit[source]

Deserialize sequences from/to strings.

model_config: ClassVar[ConfigDict | None] = None

Base pydantic configuration to apply when serializing.

maxsplit: int

Maximum number of string split.

separator: str

How to split this string sequence.

dict_keys: Sequence[str] | None

Optional list of keys when converted to a dictionary.

when_used: Literal['always', 'unless-none', 'json', 'json-unless-none']

When serialization is supposed to be used.

class aiostem.utils.transformers.TrBeforeTimedelta[source]

Pre-validator that gets a timedelta from an int or float.

unit: Literal['milliseconds', 'seconds', 'minutes']

Unit used to serialize and deserialize.

is_float: bool

Whether to serialize / deserialize to a float number.

from_number(value: float) timedelta[source]

Parse the input value as an integer or float timedelta.

Parameters:

value (float) – The input value we want to create a timedelta from.

Returns:

timedelta – A simple timedelta built from the provided value.

to_number(delta: timedelta) float | int[source]

Convert the timedelta value to a float or int.

Parameters:

delta (timedelta) – The timedelta value we want to serialize.

Returns:

float | int – A float or integer value.

class aiostem.utils.transformers.TrCast[source]

Pre-validator that converts to the target type.

target: type[Any]

Type we want to cast this to!

mode: Literal['before', 'after']

Whether to apply this transformation before or after the main handler.

class aiostem.utils.transformers.TrEd25519PrivateKey[source]

Transform bytes into an ed25519 private key.

Note

Tor’s implementation of Ed25519 is donna-ed25519 and uses the expanded form as the private key (64 bytes). Unfortunately cryptography does not provide such interface, which means that we are left with this implementation.

expanded: bool

Whether to generate the expanded form while serializing.

Note

This makes parsing impossible…

from_bytes(data: bytes) Ed25519PrivateKey[source]

Build an ed25519 private key out of the provided bytes.

Parameters:

data (bytes) – a 32 bytes seed representing a secret key.

Returns:

Ed25519PrivateKey – An instance of an ed25519 private key.

to_bytes(key: Ed25519PrivateKey) bytes[source]

Serialize the provided private key to bytes.

Returns:

bytes – 32 or 64 bytes corresponding to the private key.

to_expanded_bytes(key: Ed25519PrivateKey) bytes[source]

Serialize to the expanded form.

Returns:

bytes – 64 bytes corresponding to the expanded private key.

to_seed_bytes(key: Ed25519PrivateKey) bytes[source]

Serialize the seed bytes, which is the default behavior.

Returns:

bytes – 32 bytes corresponding to the private key.

class aiostem.utils.transformers.TrEd25519PublicKey[source]

Transform bytes into a ed25519 public key.

from_bytes(data: bytes) Ed25519PublicKey[source]

Build an ed25519 public key out of the provided bytes.

Returns:

Ed25519PublicKey – An instance of an ed25519 public key.

to_bytes(key: Ed25519PublicKey) bytes[source]

Serialize the provided public key to bytes.

Returns:

bytes – 32 bytes corresponding to the public key.

class aiostem.utils.transformers.TrRSAPrivateKey[source]

Transform bytes into a RSA private key.

encoding: Encoding

Encoding format for the public RSA key.

format: PrivateFormat

Key format used while serializing.

from_bytes(data: bytes) RSAPrivateKey[source]

Build a RSA private key out of the provided bytes.

Returns:

RSAPrivateKey – An instance of a RSA private key.

to_bytes(key: RSAPrivateKey) bytes[source]

Serialize the provided private key to bytes.

Return type:

bytes

class aiostem.utils.transformers.TrRSAPublicKey[source]

Transform bytes into a RSA public key.

encoding: Encoding

Encoding format for the public RSA key.

format: PublicFormat

Key format used while serializing.

from_bytes(data: bytes) RSAPublicKey[source]

Build a RSA public key out of the provided bytes.

Returns:

RSAPublicKey – An instance of a RSA public key.

to_bytes(key: RSAPublicKey) bytes[source]

Serialize the provided public key to bytes.

Return type:

bytes

class aiostem.utils.transformers.TrX25519PrivateKey[source]

Transform bytes into a X25519 private key.

from_bytes(data: bytes) X25519PrivateKey[source]

Build a X25519 private key out of the provided bytes.

Returns:

X25519PrivateKey – An instance of a X25519 private key.

to_bytes(key: X25519PrivateKey) bytes[source]

Serialize the provided private key to bytes.

Returns:

bytes – 32 bytes corresponding to the private key.

class aiostem.utils.transformers.TrX25519PublicKey[source]

Transform bytes into a X25519 public key.

from_bytes(data: bytes) X25519PublicKey[source]

Build a X25519 public key out of the provided bytes.

Returns:

X25519PublicKey – An instance of a X25519 public key.

to_bytes(key: X25519PublicKey) bytes[source]

Serialize the provided public key to bytes.

Returns:

bytes – 32 bytes corresponding to the public key.