@@ -46,9 +46,43 @@ def ensure_path(dir: Union[str, Path] = None) -> bool:
46
46
return True
47
47
48
48
49
- def msgpack_serialize (
49
+ def msgpack_serialize (_json : dict = None ) -> dict [str , Union [bool , str , bytes , None ]]:
50
+ """Serialize a Python dict to a msgpack string.
51
+
52
+ Return object will be a dict with 2 keys, 'success' and 'detail.'
53
+ Success is a bool indicator of serialize success.
54
+ Detail contains the 'message' key with the bytestring, as well as other
55
+ optional details to be returned.
56
+ """
57
+ if not _json :
58
+ raise ValueError ("Missing Python dict data to serialize" )
59
+
60
+ try :
61
+ packed = msgpack .packb (_json )
62
+
63
+ print (f"Packed message type ({ type (packed )} )" )
64
+
65
+ return_obj = {"success" : True , "detail" : {"message" : packed }}
66
+
67
+ except Exception as exc :
68
+ return_obj = {"success" : False , "detail" : {"message" : f"{ exc } " }}
69
+
70
+ return return_obj
71
+
72
+
73
+ def msgpack_serialize_file (
50
74
_json : dict = None , output_dir : str = default_serialize_dir , filename : str = None
51
75
) -> dict [str , Union [bool , str , dict [str , Union [str , dict ]]]]:
76
+ """Serialize a Python dict to a msgpack file.
77
+
78
+ Accepts a dict input, with an optional output_dir and filename. Serialize the
79
+ msgpack data to file at output_dir/filename.
80
+
81
+ Return object will be a dict with 2 keys, 'success' and 'detail.'
82
+ Success is a bool indicator of serialize success.
83
+ Detail contains the 'message' key with the bytestring, as well as other
84
+ optional details to be returned.
85
+ """
52
86
if not _json :
53
87
raise ValueError ("Missing Python dict data to serialize" )
54
88
@@ -59,6 +93,8 @@ def msgpack_serialize(
59
93
60
94
if filename .endswith (".msgpack" ):
61
95
filename .replace (".msgpack" , "" )
96
+ else :
97
+ filename = f"{ filename } .msgpack"
62
98
63
99
dir_exist = ensure_path (output_dir )
64
100
@@ -76,18 +112,21 @@ def msgpack_serialize(
76
112
}
77
113
78
114
except Exception as exc :
79
- # log.error(
80
- # {"exception": "Unhandled exception writing msgpack."}, exc_info=True
81
- # )
82
-
83
115
return_obj = {"success" : False , "detail" : {"message" : f"{ exc } " }}
84
116
85
117
return return_obj
86
118
87
119
88
- def msgpack_deserialize (
120
+ def msgpack_deserialize_file (
89
121
filename : str = None ,
90
122
) -> dict [str , Union [bool , str , dict [str , Union [str , dict ]]]]:
123
+ """Load serialized msgpack string from a file and return.
124
+
125
+ Return object will be a dict with 2 keys, 'success' and 'detail.'
126
+ Success is a bool indicator of serialize success.
127
+ Detail contains the 'message' key with the bytestring, as well as other
128
+ optional details to be returned.
129
+ """
91
130
if not filename :
92
131
raise ValueError ("Must pass a file name/path to deserialize" )
93
132
@@ -113,3 +152,35 @@ def msgpack_deserialize(
113
152
return_obj = {"success" : False , "detail" : {"message" : f"{ exc } " }}
114
153
115
154
return return_obj
155
+
156
+
157
+ def msgpack_deserialize (
158
+ packed_str : bytes = None ,
159
+ ) -> dict [str , Union [bool , str , dict [str , Union [str , dict ]]]]:
160
+ """Load serialized msgpack string.
161
+
162
+ Returns a dict with"""
163
+ if not packed_str :
164
+ raise ValueError ("Must pass a bytestring to deserialize" )
165
+
166
+ if not isinstance (packed_str , bytes ):
167
+ raise TypeError (
168
+ f"Invalid type for [packed_str]: ({ type (packed_str )} ). Must be of type bytestring"
169
+ )
170
+
171
+ try :
172
+ unpacked = msgpack .unpackb (packed_str )
173
+
174
+ return_obj = {
175
+ "success" : True ,
176
+ "detail" : {
177
+ "message" : unpacked ,
178
+ },
179
+ }
180
+
181
+ except Exception as exc :
182
+ # log.error({"exception": "Unhandled exception reading msgpack."}, exc_info=True)
183
+
184
+ return_obj = {"success" : False , "detail" : {"message" : f"{ exc } " }}
185
+
186
+ return return_obj
0 commit comments