Coverage for lib/lottie/parsers/tgs.py: 19%

28 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-20 16:17 +0100

1import io 

2import json 

3import gzip 

4from ..objects import Animation 

5 

6 

7def parse_tgs_json(file): 

8 """! 

9 Reads both tgs and lottie files, returns the json structure 

10 """ 

11 return open_maybe_gzipped(file, json.load) 

12 

13 

14def open_maybe_gzipped(file, on_open): 

15 if isinstance(file, str): 

16 with open(file, "r") as fileobj: 

17 return open_maybe_gzipped(fileobj, on_open) 

18 

19 if isinstance(file, io.TextIOBase) and hasattr(file, "buffer"): 

20 binfile = file.buffer 

21 else: 

22 binfile = file 

23 

24 try: 

25 binfile.seek(binfile.tell()) # Throws when not seekable 

26 mn = binfile.read(2) 

27 binfile.seek(0) 

28 except (io.UnsupportedOperation, OSError): 

29 mn = b'' 

30 

31 if mn == b'\x1f\x8b': # gzip magic number 

32 final_file = gzip.open(binfile, "rb") 

33 elif isinstance(file, io.TextIOBase): 

34 final_file = file 

35 else: 

36 final_file = io.TextIOWrapper(file) 

37 

38 return on_open(final_file) 

39 

40 

41def parse_tgs(filename): 

42 """! 

43 Reads both tgs and lottie files 

44 """ 

45 lottie = parse_tgs_json(filename) 

46 return Animation.load(lottie)