@@ -71,7 +71,7 @@ def __init__(
71
71
self .identifier = self .fqn ()
72
72
73
73
def value_from (
74
- self , value : Optional [Union [int , float , bool , str ]]
74
+ self , value : Optional [Union [int , float , bool , str , bytes ]]
75
75
) -> Optional [IngestWithConfigDataChannelValue ]:
76
76
"""
77
77
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(
82
82
return None
83
83
84
84
def try_value_from (
85
- self , value : Optional [Union [int , float , bool , str ]]
85
+ self , value : Optional [Union [int , float , bool , str , bytes ]]
86
86
) -> IngestWithConfigDataChannelValue :
87
87
"""
88
88
Generate a channel value for this particular channel configuration. This will raise an exception
@@ -112,7 +112,8 @@ def try_value_from(
112
112
return enum_value (int (value ))
113
113
elif isinstance (value , str ) and self .data_type == ChannelDataType .STRING :
114
114
return string_value (value )
115
-
115
+ elif isinstance (value , bytes ) and self .data_type == ChannelDataType .BYTES :
116
+ return bytes_value (value )
116
117
raise ValueError (f"Failed to cast value of type { type (value )} to { self .data_type } " )
117
118
118
119
def as_pb (self , klass : Type [ChannelConfigPb ]) -> ChannelConfigPb :
@@ -209,6 +210,7 @@ class ChannelDataTypeStrRep(Enum):
209
210
INT_64 = "int64"
210
211
UINT_32 = "uint32"
211
212
UINT_64 = "uint64"
213
+ BYTES = "bytes"
212
214
213
215
@staticmethod
214
216
def from_api_format (val : str ) -> Optional ["ChannelDataTypeStrRep" ]:
@@ -224,6 +226,7 @@ def from_api_format(val: str) -> Optional["ChannelDataTypeStrRep"]:
224
226
"CHANNEL_DATA_TYPE_INT_64" : ChannelDataTypeStrRep .INT_64 ,
225
227
"CHANNEL_DATA_TYPE_UINT_32" : ChannelDataTypeStrRep .UINT_32 ,
226
228
"CHANNEL_DATA_TYPE_UINT_64" : ChannelDataTypeStrRep .UINT_64 ,
229
+ "CHANNEL_DATA_TYPE_BYTES" : ChannelDataTypeStrRep .BYTES ,
227
230
}[val ]
228
231
except KeyError :
229
232
return None
@@ -244,6 +247,7 @@ class ChannelDataType(Enum):
244
247
INT_64 = channel_pb .CHANNEL_DATA_TYPE_INT_64
245
248
UINT_32 = channel_pb .CHANNEL_DATA_TYPE_UINT_32
246
249
UINT_64 = channel_pb .CHANNEL_DATA_TYPE_UINT_64
250
+ BYTES = channel_pb .CHANNEL_DATA_TYPE_BYTES
247
251
248
252
@classmethod
249
253
def from_pb (cls , val : channel_pb .ChannelDataType .ValueType ) -> "ChannelDataType" :
@@ -267,6 +271,8 @@ def from_pb(cls, val: channel_pb.ChannelDataType.ValueType) -> "ChannelDataType"
267
271
return cls .UINT_32
268
272
elif val == cls .UINT_64 .value :
269
273
return cls .UINT_64
274
+ elif val == cls .BYTES .value :
275
+ return cls .BYTES
270
276
else :
271
277
raise ValueError (f"Unknown channel data type '{ val } '." )
272
278
@@ -302,6 +308,8 @@ def from_str(cls, raw: str) -> Optional["ChannelDataType"]:
302
308
return cls .UINT_32
303
309
elif val == ChannelDataTypeStrRep .UINT_64 :
304
310
return cls .UINT_64
311
+ elif val == ChannelDataTypeStrRep .BYTES :
312
+ return cls .BYTES
305
313
else :
306
314
raise Exception ("Unreachable" )
307
315
@@ -334,6 +342,8 @@ def as_human_str(self, api_format: bool = False) -> str:
334
342
return (
335
343
"CHANNEL_DATA_TYPE_UINT_64" if api_format else ChannelDataTypeStrRep .UINT_64 .value
336
344
)
345
+ elif self == ChannelDataType .BYTES :
346
+ return "CHANNEL_DATA_TYPE_BYTES" if api_format else ChannelDataTypeStrRep .BYTES .value
337
347
else :
338
348
raise Exception ("Unreachable." )
339
349
@@ -421,6 +431,10 @@ def empty_value() -> IngestWithConfigDataChannelValue:
421
431
return IngestWithConfigDataChannelValue (empty = Empty ())
422
432
423
433
434
+ def bytes_value (val : bytes ) -> IngestWithConfigDataChannelValue :
435
+ return IngestWithConfigDataChannelValue (bytes = val )
436
+
437
+
424
438
def is_data_type (val : IngestWithConfigDataChannelValue , target_type : ChannelDataType ) -> bool :
425
439
if target_type == ChannelDataType .DOUBLE :
426
440
return val .HasField ("double" )
@@ -442,3 +456,6 @@ def is_data_type(val: IngestWithConfigDataChannelValue, target_type: ChannelData
442
456
return val .HasField ("uint32" )
443
457
elif target_type == ChannelDataType .UINT_64 :
444
458
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