Arguments

These classes are used internally by Command and CommandSerializer to serialize its parameters to the format expected by Tor.

Base class

class aiostem.utils.argument.BaseArgument[source]

Bases: ABC

Base class for any command argument.

abstractmethod __str__() str[source]

Serialize the argument to string.

Return type:

str

aiostem.utils.argument.KeyTypes

All types allowed as a key in a keyword argument.

alias of str | None

aiostem.utils.argument.ValueTypes

All types allowed as a value, either for a keyword or a simple string.

alias of IPv4Address | IPv6Address | IntEnum | StrEnum | int | str

Argument classes

class aiostem.utils.argument.ArgumentKeyword[source]

Bases: BaseArgument

Describe a keyword argument.

__init__(key: KeyTypes, value: ValueTypes | None, *, quotes: QuoteStyle = QuoteStyle.AUTO) None[source]

Create a new keyword argument.

Important

Both key and value cannot be None at the same time.

Note

When value is None, quotes is enforced to QuoteStyle.NEVER.

Parameters:
  • key (KeyTypes) – Key part of the keyword, if any.

  • value (ValueTypes | None) – Value part of the keyword, if any.

Raises:

CommandError – when key and value are both None.

Keyword Arguments:

quotes – Tell how to quote the value part when serialized.

__str__() str[source]

Serialize the argument to a string.

Return type:

str

property key: str | None

Get the key part of the keyword argument.

property value: str | None

Get the value of the keyword argument as a string.

property quotes: QuoteStyle

Get the applied quoting style.

class aiostem.utils.argument.ArgumentString[source]

Bases: BaseArgument

Describe a string argument.

__init__(value: ValueTypes, *, safe: bool = False) None[source]

Create a new string argument.

Parameters:

value (ValueTypes) – Raw string value.

Keyword Arguments:

safe – Whether the caller ensures that the value contains no space.

Note

This value cannot contain spaces.

__str__() str[source]

Serialize the argument to a string.

Return type:

str

property value: str

Get the value of the string argument.

Helpers

class aiostem.utils.argument.QuoteStyle[source]

Bases: IntEnum

Set the type of quote to use.

NEVER = 0

No quote are added around the value, no checks are being performed.

NEVER_ENSURE = 1

No quote are added around the value, check input to ensure that.

ALWAYS = 2

Value is always enclosed with quotes.

AUTO = 3

Automatically determine the quoting style.

static should_have_quotes(text: str) bool[source]

Tell whether the provided text should have quotes.

Parameters:

text (str) – Input text to check for quotes.

Returns:

bool – Whether the input text should be enclosed with quotes.

escape(text: str) str[source]

Escape the provided text, if needed.

Parameters:

text (str) – string value to quote according to the current style

Returns:

str – The input value quoted according to the current style.