|
| 1 | +"""Functions to convert between different data formats.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from abc import ABC, abstractmethod |
| 7 | +from typing import Any, Generic, Type, TypeVar |
| 8 | + |
| 9 | +from google.protobuf import any_pb2, wrappers_pb2 |
| 10 | +from google.protobuf.message import Message |
| 11 | + |
| 12 | +_TPythonType = TypeVar("_TPythonType") |
| 13 | +_TProtobufType = TypeVar("_TProtobufType", bound=Message) |
| 14 | + |
| 15 | +_logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class Converter(Generic[_TPythonType, _TProtobufType], ABC): |
| 19 | + """A class that defines how to convert between Python objects and protobuf Any messages.""" |
| 20 | + |
| 21 | + @property |
| 22 | + @abstractmethod |
| 23 | + def python_type(self) -> Type[_TPythonType]: |
| 24 | + """The Python type that this converter handles.""" |
| 25 | + |
| 26 | + @property |
| 27 | + @abstractmethod |
| 28 | + def protobuf_message(self) -> Type[_TProtobufType]: |
| 29 | + """The type-specific protobuf message for the Python type.""" |
| 30 | + |
| 31 | + @property |
| 32 | + def protobuf_typename(self) -> str: |
| 33 | + """The protobuf name for the type.""" |
| 34 | + return self.protobuf_message.DESCRIPTOR.full_name # type: ignore[no-any-return] |
| 35 | + |
| 36 | + def to_protobuf_any(self, python_value: _TPythonType) -> any_pb2.Any: |
| 37 | + """Convert the Python object to its type-specific message and pack it as any_pb2.Any.""" |
| 38 | + message = self.to_protobuf_message(python_value) |
| 39 | + as_any = any_pb2.Any() |
| 40 | + as_any.Pack(message) |
| 41 | + return as_any |
| 42 | + |
| 43 | + @abstractmethod |
| 44 | + def to_protobuf_message(self, python_value: _TPythonType) -> _TProtobufType: |
| 45 | + """Convert the Python object to its type-specific message.""" |
| 46 | + |
| 47 | + def to_python(self, protobuf_value: any_pb2.Any) -> _TPythonType: |
| 48 | + """Convert the protobuf Any message to its matching Python type.""" |
| 49 | + protobuf_message = self.protobuf_message() |
| 50 | + did_unpack = protobuf_value.Unpack(protobuf_message) |
| 51 | + if not did_unpack: |
| 52 | + raise ValueError(f"Failed to unpack Any with type '{protobuf_value.TypeName()}'") |
| 53 | + return self.to_python_value(protobuf_message) |
| 54 | + |
| 55 | + @abstractmethod |
| 56 | + def to_python_value(self, protobuf_message: _TProtobufType) -> _TPythonType: |
| 57 | + """Convert the protobuf wrapper message to its matching Python type.""" |
| 58 | + |
| 59 | + |
| 60 | +class BoolConverter(Converter[bool, wrappers_pb2.BoolValue]): |
| 61 | + """A converter for boolean types.""" |
| 62 | + |
| 63 | + @property |
| 64 | + def python_type(self) -> Type[bool]: |
| 65 | + """The Python type that this converter handles.""" |
| 66 | + return bool |
| 67 | + |
| 68 | + @property |
| 69 | + def protobuf_message(self) -> Type[wrappers_pb2.BoolValue]: |
| 70 | + """The type-specific protobuf message for the Python type.""" |
| 71 | + return wrappers_pb2.BoolValue |
| 72 | + |
| 73 | + def to_protobuf_message(self, python_value: bool) -> wrappers_pb2.BoolValue: |
| 74 | + """Convert the Python bool to a protobuf wrappers_pb2.BoolValue.""" |
| 75 | + return self.protobuf_message(value=python_value) |
| 76 | + |
| 77 | + def to_python_value(self, protobuf_value: wrappers_pb2.BoolValue) -> bool: |
| 78 | + """Convert the protobuf message to a Python bool.""" |
| 79 | + return protobuf_value.value |
| 80 | + |
| 81 | + |
| 82 | +class BytesConverter(Converter[bytes, wrappers_pb2.BytesValue]): |
| 83 | + """A converter for byte string types.""" |
| 84 | + |
| 85 | + @property |
| 86 | + def python_type(self) -> Type[bytes]: |
| 87 | + """The Python type that this converter handles.""" |
| 88 | + return bytes |
| 89 | + |
| 90 | + @property |
| 91 | + def protobuf_message(self) -> Type[wrappers_pb2.BytesValue]: |
| 92 | + """The type-specific protobuf message for the Python type.""" |
| 93 | + return wrappers_pb2.BytesValue |
| 94 | + |
| 95 | + def to_protobuf_message(self, python_value: bytes) -> wrappers_pb2.BytesValue: |
| 96 | + """Convert the Python bytes string to a protobuf wrappers_pb2.BytesValue.""" |
| 97 | + return self.protobuf_message(value=python_value) |
| 98 | + |
| 99 | + def to_python_value(self, protobuf_value: wrappers_pb2.BytesValue) -> bytes: |
| 100 | + """Convert the protobuf message to a Python bytes string.""" |
| 101 | + return protobuf_value.value |
| 102 | + |
| 103 | + |
| 104 | +class FloatConverter(Converter[float, wrappers_pb2.DoubleValue]): |
| 105 | + """A converter for floating point types.""" |
| 106 | + |
| 107 | + @property |
| 108 | + def python_type(self) -> Type[float]: |
| 109 | + """The Python type that this converter handles.""" |
| 110 | + return float |
| 111 | + |
| 112 | + @property |
| 113 | + def protobuf_message(self) -> Type[wrappers_pb2.DoubleValue]: |
| 114 | + """The type-specific protobuf message for the Python type.""" |
| 115 | + return wrappers_pb2.DoubleValue |
| 116 | + |
| 117 | + def to_protobuf_message(self, python_value: float) -> wrappers_pb2.DoubleValue: |
| 118 | + """Convert the Python float to a protobuf wrappers_pb2.DoubleValue.""" |
| 119 | + return self.protobuf_message(value=python_value) |
| 120 | + |
| 121 | + def to_python_value(self, protobuf_value: wrappers_pb2.DoubleValue) -> float: |
| 122 | + """Convert the protobuf message to a Python float.""" |
| 123 | + return protobuf_value.value |
| 124 | + |
| 125 | + |
| 126 | +class IntConverter(Converter[int, wrappers_pb2.Int64Value]): |
| 127 | + """A converter for integer types.""" |
| 128 | + |
| 129 | + @property |
| 130 | + def python_type(self) -> Type[int]: |
| 131 | + """The Python type that this converter handles.""" |
| 132 | + return int |
| 133 | + |
| 134 | + @property |
| 135 | + def protobuf_message(self) -> Type[wrappers_pb2.Int64Value]: |
| 136 | + """The type-specific protobuf message for the Python type.""" |
| 137 | + return wrappers_pb2.Int64Value |
| 138 | + |
| 139 | + def to_protobuf_message(self, python_value: int) -> wrappers_pb2.Int64Value: |
| 140 | + """Convert the Python int to a protobuf wrappers_pb2.Int64Value.""" |
| 141 | + return self.protobuf_message(value=python_value) |
| 142 | + |
| 143 | + def to_python_value(self, protobuf_value: wrappers_pb2.Int64Value) -> int: |
| 144 | + """Convert the protobuf message to a Python int.""" |
| 145 | + return protobuf_value.value |
| 146 | + |
| 147 | + |
| 148 | +class StrConverter(Converter[str, wrappers_pb2.StringValue]): |
| 149 | + """A converter for text string types.""" |
| 150 | + |
| 151 | + @property |
| 152 | + def python_type(self) -> Type[str]: |
| 153 | + """The Python type that this converter handles.""" |
| 154 | + return str |
| 155 | + |
| 156 | + @property |
| 157 | + def protobuf_message(self) -> Type[wrappers_pb2.StringValue]: |
| 158 | + """The type-specific protobuf message for the Python type.""" |
| 159 | + return wrappers_pb2.StringValue |
| 160 | + |
| 161 | + def to_protobuf_message(self, python_value: str) -> wrappers_pb2.StringValue: |
| 162 | + """Convert the Python str to a protobuf wrappers_pb2.StringValue.""" |
| 163 | + return self.protobuf_message(value=python_value) |
| 164 | + |
| 165 | + def to_python_value(self, protobuf_value: wrappers_pb2.StringValue) -> str: |
| 166 | + """Convert the protobuf message to a Python string.""" |
| 167 | + return protobuf_value.value |
| 168 | + |
| 169 | + |
| 170 | +# FFV -- consider adding a RegisterConverter mechanism |
| 171 | +_CONVERTIBLE_TYPES: list[Converter[Any, Any]] = [ |
| 172 | + BoolConverter(), |
| 173 | + BytesConverter(), |
| 174 | + FloatConverter(), |
| 175 | + IntConverter(), |
| 176 | + StrConverter(), |
| 177 | +] |
| 178 | + |
| 179 | +_CONVERTER_FOR_PYTHON_TYPE = {entry.python_type: entry for entry in _CONVERTIBLE_TYPES} |
| 180 | +_CONVERTER_FOR_GRPC_TYPE = {entry.protobuf_typename: entry for entry in _CONVERTIBLE_TYPES} |
| 181 | +_SUPPORTED_PYTHON_TYPES = _CONVERTER_FOR_PYTHON_TYPE.keys() |
| 182 | + |
| 183 | + |
| 184 | +def to_any(python_value: object) -> any_pb2.Any: |
| 185 | + """Convert a Python object to a protobuf Any.""" |
| 186 | + underlying_parents = type(python_value).mro() # This covers enum.IntEnum and similar |
| 187 | + |
| 188 | + best_matching_type = next( |
| 189 | + (parent for parent in underlying_parents if parent in _SUPPORTED_PYTHON_TYPES), None |
| 190 | + ) |
| 191 | + if not best_matching_type: |
| 192 | + raise TypeError( |
| 193 | + f"Unsupported type: {type(python_value)} with parents {underlying_parents}. Supported types are: {_SUPPORTED_PYTHON_TYPES}" |
| 194 | + ) |
| 195 | + _logger.debug(f"Best matching type for '{repr(python_value)}' resolved to {best_matching_type}") |
| 196 | + |
| 197 | + converter = _CONVERTER_FOR_PYTHON_TYPE[best_matching_type] |
| 198 | + return converter.to_protobuf_any(python_value) |
| 199 | + |
| 200 | + |
| 201 | +def from_any(protobuf_any: any_pb2.Any) -> object: |
| 202 | + """Convert a protobuf Any to a Python object.""" |
| 203 | + if not isinstance(protobuf_any, any_pb2.Any): |
| 204 | + raise ValueError(f"Unexpected type: {type(protobuf_any)}") |
| 205 | + |
| 206 | + underlying_typename = protobuf_any.TypeName() |
| 207 | + _logger.debug(f"Unpacking type '{underlying_typename}'") |
| 208 | + |
| 209 | + converter = _CONVERTER_FOR_GRPC_TYPE[underlying_typename] |
| 210 | + return converter.to_python(protobuf_any) |
0 commit comments