3434import base64
3535import os
3636import re
37+ from .jpath import jsonpath
38+ import lzma
3739
3840##====================================================================================
3941## global variables
@@ -107,7 +109,7 @@ def encode(d, opt={}, **kwargs):
107109 import lzma
108110 except ImportError :
109111 from backports import lzma
110- except Exception :
112+ except ImportError :
111113 raise Exception (
112114 "JData" ,
113115 'you must install "lzma" module to compress with this format' ,
@@ -298,38 +300,40 @@ def decode(d, opt={}, **kwargs):
298300 elif d ["_ArrayZipType_" ] == "gzip" :
299301 newobj = zlib .decompress (bytes (newobj ), zlib .MAX_WBITS | 32 )
300302 elif d ["_ArrayZipType_" ] == "lzma" :
301- try :
302- import lzma
303- except ImportError :
304- from backports import lzma
305303 buf = bytearray (newobj ) # set length to -1 (unknown) if EOF appears
306304 buf [5 :13 ] = b"\xff \xff \xff \xff \xff \xff \xff \xff "
307305 newobj = lzma .decompress (buf , lzma .FORMAT_ALONE )
308306 elif d ["_ArrayZipType_" ] == "lz4" :
309307 try :
310308 import lz4 .frame
311-
312- newobj = lz4 .frame .decompress (bytes (newobj ))
313- except Exception :
309+ except ImportError :
314310 print (
315311 'Warning: you must install "lz4" module to decompress a data record in this file, ignoring'
316312 )
317313 return copy .deepcopy (d ) if opt ["inplace" ] else d
314+
315+ try :
316+ newobj = lz4 .frame .decompress (bytes (newobj ))
317+ except Exception as e :
318+ raise ValueError (f"lz4 decompression failed: { e } " )
319+
318320 elif d ["_ArrayZipType_" ].startswith ("blosc2" ):
319321 try :
320322 import blosc2
323+ except ImportError :
324+ print ('Warning: you must install "blosc2" module...' )
325+ return copy .deepcopy (d ) if opt ["inplace" ] else d
321326
327+ try :
322328 blosc2nthread = 1
323329 if "nthread" in opt :
324330 blosc2nthread = opt ["nthread" ]
325331 newobj = blosc2 .decompress2 (
326332 bytes (newobj ), as_bytearray = False , nthreads = blosc2nthread
327333 )
328- except Exception :
329- print (
330- 'Warning: you must install "blosc2" module to decompress a data record in this file, ignoring'
331- )
332- return copy .deepcopy (d ) if opt ["inplace" ] else d
334+ except Exception as e :
335+ raise ValueError (f"blosc2 decompression failed: { e } " )
336+
333337 newobj = np .frombuffer (
334338 bytearray (newobj ), dtype = np .dtype (d ["_ArrayType_" ])
335339 ).reshape (d ["_ArrayZipSize_" ])
@@ -380,9 +384,9 @@ def decode(d, opt={}, **kwargs):
380384 "one and only one of _ArrayData_ or _ArrayZipData_ is required" ,
381385 )
382386 elif "_DataLink_" in d :
383- if opt ["maxlinklevel" ] > 0 and "_DataLink_" in data :
384- if isinstance (data ["_DataLink_" ], str ):
385- datalink = data ["_DataLink_" ]
387+ if opt ["maxlinklevel" ] > 0 and "_DataLink_" in d :
388+ if isinstance (d ["_DataLink_" ], str ):
389+ datalink = d ["_DataLink_" ]
386390 if re .search ("\:\$" , datalink ):
387391 ref = re .search (
388392 "^(?P<proto>[a-zA-Z]+://)*(?P<path>.+)(?P<delim>\:)()*(?P<jsonpath>(?<=:)\$\d*\.*.*)*" ,
@@ -399,8 +403,8 @@ def decode(d, opt={}, **kwargs):
399403 if os .path .exists (fname ):
400404 opt ["maxlinklevel" ] = opt ["maxlinklevel" ] - 1
401405 if ref .group ("jsonpath" ):
402- newobj = jsonpath (newdata , ref .group ("jsonpath" ))
403- return nrewobj
406+ newobj = jsonpath (newobj , ref .group ("jsonpath" ))
407+ return newobj
404408 else :
405409 raise Exception (
406410 "JData" ,
@@ -452,7 +456,8 @@ def encodedict(d0, opt={}):
452456
453457
454458def encodelist (d0 , opt = {}):
455- d = copy .deepcopy (d0 ) if opt ["inplace" ] else d0
459+ if opt ["inplace" ]:
460+ d = [copy .deepcopy (x ) if not isinstance (x , np .ndarray ) else x for x in d0 ]
456461 for i , s in enumerate (d ):
457462 d [i ] = encode (s , opt )
458463 return d
@@ -475,7 +480,8 @@ def decodedict(d0, opt={}):
475480
476481
477482def decodelist (d0 , opt = {}):
478- d = copy .deepcopy (d0 ) if opt ["inplace" ] else d0
483+ if opt ["inplace" ]:
484+ d = [copy .deepcopy (x ) if not isinstance (x , np .ndarray ) else x for x in d0 ]
479485 for i , s in enumerate (d ):
480486 d [i ] = decode (s , opt )
481487 return d
@@ -502,16 +508,6 @@ def gzipencode(buf):
502508
503509
504510def lzmaencode (buf ):
505- try :
506- try :
507- import lzma
508- except ImportError :
509- from backports import lzma
510- except Exception :
511- raise Exception (
512- "JData" ,
513- 'you must install "lzma" module to compress with this format' ,
514- )
515511 return lzma .compress (buf , lzma .FORMAT_ALONE )
516512
517513
@@ -554,16 +550,6 @@ def gzipdecode(buf):
554550
555551
556552def lzmadecode (buf ):
557- try :
558- try :
559- import lzma
560- except ImportError :
561- from backports import lzma
562- except Exception :
563- raise Exception (
564- "JData" ,
565- 'you must install "lzma" module to compress with this format' ,
566- )
567553 newbuf = bytearray (buf ) # set length to -1 (unknown) if EOF appears
568554 newbuf [5 :13 ] = b"\xff \xff \xff \xff \xff \xff \xff \xff "
569555 return lzma .decompress (newbuf , lzma .FORMAT_ALONE )
0 commit comments