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