Coverage for /home/runner/.local/share/hatch/env/virtual/importnb/KA2AwMZG/test.interactive/lib/python3.9/site-packages/importnb/decoder.py: 88%

1 import json 6 ctx 1atbcud

2 import linecache 6 ctx 1atbcud

3 import textwrap 6 ctx 1atbcud

4 from functools import partial 6 ctx 1atbcud

5

6

7 def quote(object, *, quotes="'''"): 6 ctx 1atbcud

8 if quotes in object: 8 ↛ 9line 8 didn't jump to line 9, because the condition on line 8 was never true 19 ctx 1eabcfdghijklmnopqrs

9 quotes = '"""'

10 return quotes + object + "\n" + quotes 19 ctx 1eabcfdghijklmnopqrs

11

12

13 from ._json_parser import Lark_StandAlone, Transformer, Tree 6 ctx 1atbcud

14

15

16 class Transformer(Transformer): 6 ctx 1atbcud

17 def __init__( 6 ctx 1atbcud

18 self,

19 markdown=quote,

20 code=textwrap.dedent,

21 raw=partial(textwrap.indent, prefix="# "),

22 **kwargs,

23 ):

24 super().__init__(**kwargs) 19 ctx 1eabcfdghijklmnopqrs

25

26 for key in ("markdown", "code", "raw"): 19 ctx 1eabcfdghijklmnopqrs

27 setattr(self, "transform_" + key, locals().get(key)) 19 ctx 1eabcfdghijklmnopqrs

28

29 def string(self, s): 6 ctx 1atbcud

30 return s[0].line, json.loads(s[0]) 19 ctx 1eabcfdghijklmnopqrs

31

32 def item(self, s): 6 ctx 1atbcud

33 key = s[0][-1] 19 ctx 1eabcfdghijklmnopqrs

34 if key == "cells": 19 ctx 1eabcfdghijklmnopqrs

35 if not isinstance(s[-1], Tree): 35 ↛ exitline 35 didn't return from function 'item', because the condition on line 35 was never false 19 ctx 1eabcfdghijklmnopqrs

36 return self.render(list(map(dict, s[-1]))) 19 ctx 1eabcfdghijklmnopqrs

37 elif key in {"source", "text"}: 19 ctx 1eabcfdghijklmnopqrs

38 return key, s[-1] 19 ctx 1eabcfdghijklmnopqrs

39 elif key == "cell_type": 19 ctx 1eabcfdghijklmnopqrs

40 if isinstance(s[-1], tuple): 40 ↛ exitline 40 didn't return from function 'item', because the condition on line 40 was never false 19 ctx 1eabcfdghijklmnopqrs

41 return key, s[-1][-1] 19 ctx 1eabcfdghijklmnopqrs

42

43 def array(self, s): 6 ctx 1atbcud

44 if s: 19 ctx 1eabcfdghijklmnopqrs

45 return s 19 ctx 1eabcfdghijklmnopqrs

46 return [] 19 ctx 1eabcfdghijklmnopqrs

47

48 def object(self, s): 6 ctx 1atbcud

49 return [x for x in s if x is not None] 19 ctx 1eabcfdghijklmnopqrs

50

51 def render_one(self, kind, lines): 6 ctx 1atbcud

52 s = "".join(lines) 19 ctx 1eabcfdghijklmnopqrs

53 if not s.endswith(("\n",)): 53 ↛ 55line 53 didn't jump to line 55, because the condition on line 53 was never false 19 ctx 1eabcfdghijklmnopqrs

54 s += "\n" 19 ctx 1eabcfdghijklmnopqrs

55 return getattr(self, f"transform_{kind}")(s) 19 ctx 1eabcfdghijklmnopqrs

56

57 def render(self, x): 6 ctx 1atbcud

58 body = [] 19 ctx 1eabcfdghijklmnopqrs

59 for token in x: 19 ctx 1eabcfdghijklmnopqrs

60 t = token.get("cell_type") 19 ctx 1eabcfdghijklmnopqrs

61 try: 19 ctx 1eabcfdghijklmnopqrs

62 s = token["source"] 19 ctx 1eabcfdghijklmnopqrs

63 except KeyError:

64 s = token.get("text")

65 if s: 19 ctx 1eabcfdghijklmnopqrs

66 if not isinstance(s, list): 66 ↛ 67line 66 didn't jump to line 67, because the condition on line 66 was never true 19 ctx 1eabcfdghijklmnopqrs

67 s = [s]

68 l, lines = s[0][0], [x[1] for x in s] 19 ctx 1eabcfdghijklmnopqrs

69 body.extend([""] * (l - len(body))) 19 ctx 1eabcfdghijklmnopqrs

70 lines = self.render_one(t, lines) 19 ctx 1eabcfdghijklmnopqrs

71 body.extend(lines.splitlines()) 19 ctx 1eabcfdghijklmnopqrs

72 return "\n".join(body + [""]) 19 ctx 1eabcfdghijklmnopqrs

73

74

75 class LineCacheNotebookDecoder(Transformer): 6 ctx 1atbcud

76 def __init__( 6 ctx 1atbcud

77 self,

78 markdown=quote,

79 code=textwrap.dedent,

80 raw=partial(textwrap.indent, prefix="# "),

81 **kwargs,

82 ):

83 super().__init__(**kwargs) 19 ctx 1eabcfdghijklmnopqrs

84

85 for key in ("markdown", "code", "raw"): 19 ctx 1eabcfdghijklmnopqrs

86 setattr(self, "transform_" + key, locals().get(key)) 19 ctx 1eabcfdghijklmnopqrs

87

88 def source_from_json_grammar(self, object): 6 ctx 1atbcud

89 return Lark_StandAlone(transformer=self).parse(object) 19 ctx 1eabcfdghijklmnopqrs

90

91 def decode(self, object, filename): 6 ctx 1atbcud

92 s = self.source_from_json_grammar(object) 19 ctx 1eabcfdghijklmnopqrs

93 if s: 93 ↛ 104line 93 didn't jump to line 104, because the condition on line 93 was never false 19 ctx 1eabcfdghijklmnopqrs

94 source = s[0] 19 ctx 1eabcfdghijklmnopqrs

95 linecache.updatecache(filename) 19 ctx 1eabcfdghijklmnopqrs

96 if filename in linecache.cache: 96 ↛ 103line 96 didn't jump to line 103, because the condition on line 96 was never false 19 ctx 1eabcfdghijklmnopqrs

97 linecache.cache[filename] = ( 19 ctx 1eabcfdghijklmnopqrs

98 linecache.cache[filename][0],

99 linecache.cache[filename][1],

100 source.splitlines(True),

101 filename,

102 )

103 return source 19 ctx 1eabcfdghijklmnopqrs

104 return ""