Coverage for /home/runner/.local/share/hatch/env/virtual/importnb/KA2AwMZG/test.interactive/lib/python3.9/site-packages/importnb/decoder.py: 88%
69 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-02 04:03 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-02 04:03 +0000
1import json 1atbcud
2import linecache 1atbcud
3import textwrap 1atbcud
4from functools import partial 1atbcud
7def quote(object, *, quotes="'''"): 1atbcud
8 if quotes in object: 8 ↛ 9line 8 didn't jump to line 9, because the condition on line 8 was never true1eabcfdghijklmnopqrs
9 quotes = '"""'
10 return quotes + object + "\n" + quotes 1eabcfdghijklmnopqrs
13from ._json_parser import Lark_StandAlone, Transformer, Tree 1atbcud
16class Transformer(Transformer): 1atbcud
17 def __init__( 1atbcud
18 self,
19 markdown=quote,
20 code=textwrap.dedent,
21 raw=partial(textwrap.indent, prefix="# "),
22 **kwargs,
23 ):
24 super().__init__(**kwargs) 1eabcfdghijklmnopqrs
26 for key in ("markdown", "code", "raw"): 1eabcfdghijklmnopqrs
27 setattr(self, "transform_" + key, locals().get(key)) 1eabcfdghijklmnopqrs
29 def string(self, s): 1atbcud
30 return s[0].line, json.loads(s[0]) 1eabcfdghijklmnopqrs
32 def item(self, s): 1atbcud
33 key = s[0][-1] 1eabcfdghijklmnopqrs
34 if key == "cells": 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 false1eabcfdghijklmnopqrs
36 return self.render(list(map(dict, s[-1]))) 1eabcfdghijklmnopqrs
37 elif key in {"source", "text"}: 1eabcfdghijklmnopqrs
38 return key, s[-1] 1eabcfdghijklmnopqrs
39 elif key == "cell_type": 1eabcfdghijklmnopqrs
40 if isinstance(s[-1], tuple): 40 ↛ exitline 40 didn't return from function 'item', because the condition on line 40 was never false1eabcfdghijklmnopqrs
41 return key, s[-1][-1] 1eabcfdghijklmnopqrs
43 def array(self, s): 1atbcud
44 if s: 1eabcfdghijklmnopqrs
45 return s 1eabcfdghijklmnopqrs
46 return [] 1eabcfdghijklmnopqrs
48 def object(self, s): 1atbcud
49 return [x for x in s if x is not None] 1eabcfdghijklmnopqrs
51 def render_one(self, kind, lines): 1atbcud
52 s = "".join(lines) 1eabcfdghijklmnopqrs
53 if not s.endswith(("\n",)): 53 ↛ 55line 53 didn't jump to line 55, because the condition on line 53 was never false1eabcfdghijklmnopqrs
54 s += "\n" 1eabcfdghijklmnopqrs
55 return getattr(self, f"transform_{kind}")(s) 1eabcfdghijklmnopqrs
57 def render(self, x): 1atbcud
58 body = [] 1eabcfdghijklmnopqrs
59 for token in x: 1eabcfdghijklmnopqrs
60 t = token.get("cell_type") 1eabcfdghijklmnopqrs
61 try: 1eabcfdghijklmnopqrs
62 s = token["source"] 1eabcfdghijklmnopqrs
63 except KeyError:
64 s = token.get("text")
65 if s: 1eabcfdghijklmnopqrs
66 if not isinstance(s, list): 66 ↛ 67line 66 didn't jump to line 67, because the condition on line 66 was never true1eabcfdghijklmnopqrs
67 s = [s]
68 l, lines = s[0][0], [x[1] for x in s] 1eabcfdghijklmnopqrs
69 body.extend([""] * (l - len(body))) 1eabcfdghijklmnopqrs
70 lines = self.render_one(t, lines) 1eabcfdghijklmnopqrs
71 body.extend(lines.splitlines()) 1eabcfdghijklmnopqrs
72 return "\n".join(body + [""]) 1eabcfdghijklmnopqrs
75class LineCacheNotebookDecoder(Transformer): 1atbcud
76 def __init__( 1atbcud
77 self,
78 markdown=quote,
79 code=textwrap.dedent,
80 raw=partial(textwrap.indent, prefix="# "),
81 **kwargs,
82 ):
83 super().__init__(**kwargs) 1eabcfdghijklmnopqrs
85 for key in ("markdown", "code", "raw"): 1eabcfdghijklmnopqrs
86 setattr(self, "transform_" + key, locals().get(key)) 1eabcfdghijklmnopqrs
88 def source_from_json_grammar(self, object): 1atbcud
89 return Lark_StandAlone(transformer=self).parse(object) 1eabcfdghijklmnopqrs
91 def decode(self, object, filename): 1atbcud
92 s = self.source_from_json_grammar(object) 1eabcfdghijklmnopqrs
93 if s: 93 ↛ 104line 93 didn't jump to line 104, because the condition on line 93 was never false1eabcfdghijklmnopqrs
94 source = s[0] 1eabcfdghijklmnopqrs
95 linecache.updatecache(filename) 1eabcfdghijklmnopqrs
96 if filename in linecache.cache: 96 ↛ 103line 96 didn't jump to line 103, because the condition on line 96 was never false1eabcfdghijklmnopqrs
97 linecache.cache[filename] = ( 1eabcfdghijklmnopqrs
98 linecache.cache[filename][0],
99 linecache.cache[filename][1],
100 source.splitlines(True),
101 filename,
102 )
103 return source 1eabcfdghijklmnopqrs
104 return ""