@@ -71,7 +71,7 @@ def __init__(
7171 self .identifier = self .fqn ()
7272
7373 def value_from (
74- self , value : Optional [Union [int , float , bool , str ]]
74+ self , value : Optional [Union [int , float , bool , str , bytes ]]
7575 ) -> Optional [IngestWithConfigDataChannelValue ]:
7676 """
7777 Like `try_value_from` except will return `None` there is a failure to produce a channel value due to a type mismatch.
@@ -82,7 +82,7 @@ def value_from(
8282 return None
8383
8484 def try_value_from (
85- self , value : Optional [Union [int , float , bool , str ]]
85+ self , value : Optional [Union [int , float , bool , str , bytes ]]
8686 ) -> IngestWithConfigDataChannelValue :
8787 """
8888 Generate a channel value for this particular channel configuration. This will raise an exception
@@ -112,7 +112,8 @@ def try_value_from(
112112 return enum_value (int (value ))
113113 elif isinstance (value , str ) and self .data_type == ChannelDataType .STRING :
114114 return string_value (value )
115-
115+ elif isinstance (value , bytes ) and self .data_type == ChannelDataType .BYTES :
116+ return bytes_value (value )
116117 raise ValueError (f"Failed to cast value of type { type (value )} to { self .data_type } " )
117118
118119 def as_pb (self , klass : Type [ChannelConfigPb ]) -> ChannelConfigPb :
@@ -209,6 +210,7 @@ class ChannelDataTypeStrRep(Enum):
209210 INT_64 = "int64"
210211 UINT_32 = "uint32"
211212 UINT_64 = "uint64"
213+ BYTES = "bytes"
212214
213215 @staticmethod
214216 def from_api_format (val : str ) -> Optional ["ChannelDataTypeStrRep" ]:
@@ -224,6 +226,7 @@ def from_api_format(val: str) -> Optional["ChannelDataTypeStrRep"]:
224226 "CHANNEL_DATA_TYPE_INT_64" : ChannelDataTypeStrRep .INT_64 ,
225227 "CHANNEL_DATA_TYPE_UINT_32" : ChannelDataTypeStrRep .UINT_32 ,
226228 "CHANNEL_DATA_TYPE_UINT_64" : ChannelDataTypeStrRep .UINT_64 ,
229+ "CHANNEL_DATA_TYPE_BYTES" : ChannelDataTypeStrRep .BYTES ,
227230 }[val ]
228231 except KeyError :
229232 return None
@@ -244,6 +247,7 @@ class ChannelDataType(Enum):
244247 INT_64 = channel_pb .CHANNEL_DATA_TYPE_INT_64
245248 UINT_32 = channel_pb .CHANNEL_DATA_TYPE_UINT_32
246249 UINT_64 = channel_pb .CHANNEL_DATA_TYPE_UINT_64
250+ BYTES = channel_pb .CHANNEL_DATA_TYPE_BYTES
247251
248252 @classmethod
249253 def from_pb (cls , val : channel_pb .ChannelDataType .ValueType ) -> "ChannelDataType" :
@@ -267,6 +271,8 @@ def from_pb(cls, val: channel_pb.ChannelDataType.ValueType) -> "ChannelDataType"
267271 return cls .UINT_32
268272 elif val == cls .UINT_64 .value :
269273 return cls .UINT_64
274+ elif val == cls .BYTES .value :
275+ return cls .BYTES
270276 else :
271277 raise ValueError (f"Unknown channel data type '{ val } '." )
272278
@@ -302,6 +308,8 @@ def from_str(cls, raw: str) -> Optional["ChannelDataType"]:
302308 return cls .UINT_32
303309 elif val == ChannelDataTypeStrRep .UINT_64 :
304310 return cls .UINT_64
311+ elif val == ChannelDataTypeStrRep .BYTES :
312+ return cls .BYTES
305313 else :
306314 raise Exception ("Unreachable" )
307315
@@ -334,6 +342,8 @@ def as_human_str(self, api_format: bool = False) -> str:
334342 return (
335343 "CHANNEL_DATA_TYPE_UINT_64" if api_format else ChannelDataTypeStrRep .UINT_64 .value
336344 )
345+ elif self == ChannelDataType .BYTES :
346+ return "CHANNEL_DATA_TYPE_BYTES" if api_format else ChannelDataTypeStrRep .BYTES .value
337347 else :
338348 raise Exception ("Unreachable." )
339349
@@ -421,6 +431,10 @@ def empty_value() -> IngestWithConfigDataChannelValue:
421431 return IngestWithConfigDataChannelValue (empty = Empty ())
422432
423433
434+ def bytes_value (val : bytes ) -> IngestWithConfigDataChannelValue :
435+ return IngestWithConfigDataChannelValue (bytes = val )
436+
437+
424438def is_data_type (val : IngestWithConfigDataChannelValue , target_type : ChannelDataType ) -> bool :
425439 if target_type == ChannelDataType .DOUBLE :
426440 return val .HasField ("double" )
@@ -442,3 +456,6 @@ def is_data_type(val: IngestWithConfigDataChannelValue, target_type: ChannelData
442456 return val .HasField ("uint32" )
443457 elif target_type == ChannelDataType .UINT_64 :
444458 return val .HasField ("uint64" )
459+ elif target_type == ChannelDataType .BYTES :
460+ return val .HasField ("bytes" )
461+ raise ValueError (f"Unknown channel data type '{ target_type } '." )
0 commit comments