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

1 import json 5 ctx 1asbct

2 import linecache 5 ctx 1asbct

3 import textwrap 5 ctx 1asbct

4 from functools import partial 5 ctx 1asbct

5

6

7 def quote(object, *, quotes="'''"): 5 ctx 1asbct

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

9 quotes = '"""'

10 return quotes + object + "\n" + quotes 18 ctx 1dabcefghijklmnopqr

11

12

13 from ._json_parser import Lark_StandAlone, Transformer, Tree 5 ctx 1asbct

14

15

16 class Transformer(Transformer): 5 ctx 1asbct

17 def __init__( 5 ctx 1asbct

18 self,

19 markdown=quote,

20 code=textwrap.dedent,

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

22 **kwargs,

23 ):

24 super().__init__(**kwargs) 18 ctx 1dabcefghijklmnopqr

25

26 for key in ("markdown", "code", "raw"): 18 ctx 1dabcefghijklmnopqr

27 setattr(self, "transform_" + key, locals().get(key)) 18 ctx 1dabcefghijklmnopqr

28

29 def string(self, s): 5 ctx 1asbct

30 return s[0].line, json.loads(s[0]) 18 ctx 1dabcefghijklmnopqr

31

32 def item(self, s): 5 ctx 1asbct

33 key = s[0][-1] 18 ctx 1dabcefghijklmnopqr

34 if key == "cells": 18 ctx 1dabcefghijklmnopqr

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 18 ctx 1dabcefghijklmnopqr

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

37 elif key in {"source", "text"}: 18 ctx 1dabcefghijklmnopqr

38 return key, s[-1] 18 ctx 1dabcefghijklmnopqr

39 elif key == "cell_type": 18 ctx 1dabcefghijklmnopqr

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

41 return key, s[-1][-1] 18 ctx 1dabcefghijklmnopqr

42

43 def array(self, s): 5 ctx 1asbct

44 if s: 18 ctx 1dabcefghijklmnopqr

45 return s 18 ctx 1dabcefghijklmnopqr

46 return [] 18 ctx 1dabcefghijklmnopqr

47

48 def object(self, s): 5 ctx 1asbct

49 return [x for x in s if x is not None] 18 ctx 1dabcefghijklmnopqr

50

51 def render_one(self, kind, lines): 5 ctx 1asbct

52 s = "".join(lines) 18 ctx 1dabcefghijklmnopqr

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

54 s += "\n" 18 ctx 1dabcefghijklmnopqr

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

56

57 def render(self, x): 5 ctx 1asbct

58 body = [] 18 ctx 1dabcefghijklmnopqr

59 for token in x: 18 ctx 1dabcefghijklmnopqr

60 t = token.get("cell_type") 18 ctx 1dabcefghijklmnopqr

61 try: 18 ctx 1dabcefghijklmnopqr

62 s = token["source"] 18 ctx 1dabcefghijklmnopqr

63 except KeyError:

64 s = token.get("text")

65 if s: 18 ctx 1dabcefghijklmnopqr

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

67 s = [s]

68 l, lines = s[0][0], [x[1] for x in s] 18 ctx 1dabcefghijklmnopqr

69 body.extend([""] * (l - len(body))) 18 ctx 1dabcefghijklmnopqr

70 lines = self.render_one(t, lines) 18 ctx 1dabcefghijklmnopqr

71 body.extend(lines.splitlines()) 18 ctx 1dabcefghijklmnopqr

72 return "\n".join(body + [""]) 18 ctx 1dabcefghijklmnopqr

73

74

75 class LineCacheNotebookDecoder(Transformer): 5 ctx 1asbct

76 def __init__( 5 ctx 1asbct

77 self,

78 markdown=quote,

79 code=textwrap.dedent,

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

81 **kwargs,

82 ):

83 super().__init__(**kwargs) 18 ctx 1dabcefghijklmnopqr

84

85 for key in ("markdown", "code", "raw"): 18 ctx 1dabcefghijklmnopqr

86 setattr(self, "transform_" + key, locals().get(key)) 18 ctx 1dabcefghijklmnopqr

87

88 def source_from_json_grammar(self, object): 5 ctx 1asbct

89 return Lark_StandAlone(transformer=self).parse(object) 18 ctx 1dabcefghijklmnopqr

90

91 def decode(self, object, filename): 5 ctx 1asbct

92 s = self.source_from_json_grammar(object) 18 ctx 1dabcefghijklmnopqr

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

94 source = s[0] 18 ctx 1dabcefghijklmnopqr

95 linecache.updatecache(filename) 18 ctx 1dabcefghijklmnopqr

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

97 linecache.cache[filename] = ( 18 ctx 1dabcefghijklmnopqr

98 linecache.cache[filename][0],

99 linecache.cache[filename][1],

100 source.splitlines(True),

101 filename,

102 )

103 return source 18 ctx 1dabcefghijklmnopqr

104 return ""