Pack a python object to binary given a string defining its type
(uatype, val)
| 263 | |
| 264 | |
| 265 | def to_binary(uatype, val): |
| 266 | """ |
| 267 | Pack a python object to binary given a string defining its type |
| 268 | """ |
| 269 | if uatype.startswith("ListOf"): |
| 270 | #if isinstance(val, (list, tuple)): |
| 271 | return list_to_binary(uatype[6:], val) |
| 272 | elif isinstance(uatype, (str, unicode)) and hasattr(ua.VariantType, uatype): |
| 273 | vtype = getattr(ua.VariantType, uatype) |
| 274 | return pack_uatype(vtype, val) |
| 275 | elif isinstance(uatype, (str, unicode)) and hasattr(Primitives, uatype): |
| 276 | return getattr(Primitives, uatype).pack(val) |
| 277 | elif isinstance(val, (IntEnum, Enum)): |
| 278 | return Primitives.UInt32.pack(val.value) |
| 279 | elif isinstance(val, ua.NodeId): |
| 280 | return nodeid_to_binary(val) |
| 281 | elif isinstance(val, ua.Variant): |
| 282 | return variant_to_binary(val) |
| 283 | elif hasattr(val, "ua_types"): |
| 284 | return struct_to_binary(val) |
| 285 | else: |
| 286 | raise UaError("No known way to pack {} of type {} to ua binary".format(val, uatype)) |
| 287 | |
| 288 | |
| 289 | def list_to_binary(uatype, val): |
no test coverage detected