Coverage for src/lcdoc/mkdocs/lp/plugs/python/pyplugs/diag_diagram/__init__.py: 31.58%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

47 statements  

1import os lp|index.md

2from lcdoc.mkdocs.lp.plugs import python lp|index.md

3from lcdoc.tools import read_file lp|index.md

4from lcdoc.mkdocs.tools import app lp|index.md

5import shutil lp|index.md

6 

7config, page, Session = (python.config, python.page, python.Session) lp|index.md

8 

9try: lp|index.md

10 from diagrams import Node lp|index.md

11 

12 have_diagrams = True lp|index.md

13except: 

14 have_diagrams = None 

15 

16 

17def copying_icon_loader(self, c={}): lp|index.md

18 exists = os.path.exists 

19 if not c: 

20 bd = python.config()['docs_dir'] + '/icons_diagrams' 

21 os.makedirs(bd, exist_ok=True) 

22 c.update({'build_dir': bd}) 

23 

24 bd = c['build_dir'] 

25 orig = self._orig_load_icon() 

26 pth = orig.split('/resources/', 1)[1].replace('/', '_') 

27 dest = bd + '/' + pth 

28 if not exists(dest): 

29 shutil.copyfile(orig, dest) 

30 return dest 

31 

32 

33if have_diagrams: 33 ↛ 38line 33 didn't jump to line 38, because the condition on line 33 was never falselp|index.md

34 Node._orig_load_icon = Node._load_icon lp|index.md

35 Node._load_icon = copying_icon_loader lp|index.md

36 

37 

38def register(fmts): lp|index.md

39 """registering us as renderer for show(<pyplot module>) within lp python""" 

40 fmts['diagrams.Diagram'] = make_diag lp|index.md

41 

42 

43# from ast import literal_eval as le 

44 

45 

46# def copy_and_repl_imgs(fn, diag): 

47# b = str(diag.dot.body) 

48# diag.dot.body = le(b.replace('/home/gk/repos/docutools', '../../..')) 

49 

50T = ''' 

51<div class="diagrams_container" id="%(id)s"> 

52<svg%(body)s 

53</div> 

54''' 

55 

56 

57def make_diag(diag, fn=None, **kw): lp|index.md

58 if not have_diagrams: 

59 app.die('Please pip install diagrams') 

60 

61 diag.render() 

62 if diag.outformat == 'svg': 

63 dd = python.config()['docs_dir'] + '/' 

64 pth = python.page().file.src_path.split('/') 

65 pth.pop() if pth[-1] == 'index.md' else 0 

66 drel = '../' * (len(pth) - 0) 

67 f = diag.filename + '.' + diag.outformat 

68 s = read_file(f) 

69 s = s.split('<svg', 1)[1] 

70 s = s.replace(dd, drel).replace('>\n', '>') 

71 # s = s.replace('"black"', '"#cccccc"') 

72 # s = s.replace('<polygon ', '<polygon fill-opacity="0.1" ') 

73 s = T % {'body': s, 'id': python.Session.kw['id']} 

74 os.unlink(f) 

75 return s 

76 else: 

77 # create pngs normally otherwise: 

78 app.die('diagrams plugin only supports svg')