protoserver.utils module#

protoserver.utils.get_int(prompt)[source]#

A function that takes a prompt as input and repeatedly prompts the user for input until a valid integer is entered.

Parameters:

prompt (str) – The prompt to display to the user.

Returns:

The user’s input as an integer.

Return type:

(int)

protoserver.utils.get_user_input_by_type(field_descriptor)[source]#

Gets user input based on provided field descriptor type.

Parameters:

field_descriptor (google.protobuf.descriptor.FieldDescriptor) – The field descriptor of the field to get user input for.

Returns:

The user input based on the field descriptor type.

Return type:

Union[float, int, bool, str, bytes, None]

protoserver.utils.get_enum_input(enum_type)[source]#

Get user input for selecting an enum value.

Parameters:

enum_type (google.protobuf.descriptor.EnumDescriptor) – The enum type descriptor.

Returns:

Enum value corresponding to the user’s choice.

Return type:

Union[int, None]

This function prints the available options for the given enum type, prompts the user to enter the number corresponding to their choice, and returns the selected enum value. The user can press Enter to skip making a selection. If the entered input is invalid, the function will print an error message and prompt the user again.

Example:

from example_pb2 import ExampleEnum
selected_value = get_enum_input(ExampleEnum.DESCRIPTOR)
protoserver.utils.fill_message(message)[source]#

Fill the given protocol buffer message with user input for each field.

protoserver.utils.MessageToDict(message)[source]#

Convert a protocol buffer message into a dictionary representation.

Reference

Directly stolen from Stackoverflow

I am not using google.protobuf.json_format.MessageToDict() because the keynames are not being matched properly.

Parameters:

message (google.protobuf.message.Message) – The protocol buffer message to be converted.

Returns:

A dictionary representation of the protocol buffer message.

Return type:

(dict)

protoserver.utils.MessageToTable(message, show_empty=False, tablefmt='grid')[source]#

Generates a table representation of the given message.

Parameters:
  • message (google.protobuf.message.Message) – The message to convert to a table.

  • show_empty (bool) – Whether to show empty fields. Defaults to False.

  • tablefmt (str) – The format of the table. Defaults to “grid”.

Tip

The method ListFields only returns non-empty values which may cause fields set to 0 to be skipped. For instance the field return_code was being skipped in case of successful return. Using show_empty=True will show empty fields in the table but I recommend to avoid it as it will make the table too large with unnecessary empty fields.

Returns:

A table representation of the message.