Coverage for src/lcdoc/mkdocs/lp/plugs/drawio/__init__.py: 68.09%

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

35 statements  

1""" 

2### `drawio` 

3 

4Automatically includes an svg, based on .drawio file changes. 

5""" 

6 

7import json lp|features/lp/plugs/drawio/index.md

8import subprocess as sp lp|features/lp/plugs/drawio/index.md

9 

10from lcdoc import lp lp|features/lp/plugs/drawio/index.md

11from lcdoc.tools import file_hash, app, dirname, exists, os, read_file, write_file, os lp|features/lp/plugs/drawio/index.md

12 

13multi_line_to_list = True lp|features/lp/plugs/drawio/index.md

14req_kw = ['fn', 'src'] lp|features/lp/plugs/drawio/index.md

15 

16 

17def run(cmd, kw): lp|features/lp/plugs/drawio/index.md

18 """ 

19 """ 

20 D = lp.page_dir(kw) lp|features/lp/plugs/drawio/index.md

21 src = kw['abs_src'] lp|features/lp/plugs/drawio/index.md

22 if not exists(src): 22 ↛ 23line 22 didn't jump to line 23, because the condition on line 22 was never truelp|features/lp/plugs/drawio/index.md

23 return 'Not found: %s' % src 

24 fn = kw['fn'] lp|features/lp/plugs/drawio/index.md

25 if not fn or fn[0] == '/': 25 ↛ 26line 25 didn't jump to line 26, because the condition on line 25 was never truelp|features/lp/plugs/drawio/index.md

26 return lp.err('Require relative fn', have=fn) 

27 ffn = D + fn lp|features/lp/plugs/drawio/index.md

28 os.makedirs(dirname(ffn), exist_ok=True) lp|features/lp/plugs/drawio/index.md

29 fn_src_info = ffn + '.src' lp|features/lp/plugs/drawio/index.md

30 if exists(fn_src_info): 30 ↛ 33line 30 didn't jump to line 33, because the condition on line 30 was never falselp|features/lp/plugs/drawio/index.md

31 oldmtime, oldhsh = json.loads(read_file(fn_src_info)) lp|features/lp/plugs/drawio/index.md

32 else: 

33 oldmtime, oldhsh = 0, 0 

34 

35 mtime = os.stat(src).st_mtime lp|features/lp/plugs/drawio/index.md

36 have, hsh = False, None lp|features/lp/plugs/drawio/index.md

37 if mtime == oldmtime: 37 ↛ 38line 37 didn't jump to line 38, because the condition on line 37 was never truelp|features/lp/plugs/drawio/index.md

38 have = True 

39 else: 

40 hsh = file_hash(src) lp|features/lp/plugs/drawio/index.md

41 if hsh == oldhsh: 41 ↛ 43line 41 didn't jump to line 43, because the condition on line 41 was never falselp|features/lp/plugs/drawio/index.md

42 have = True lp|features/lp/plugs/drawio/index.md

43 if not have: 43 ↛ 44line 43 didn't jump to line 44, because the condition on line 43 was never truelp|features/lp/plugs/drawio/index.md

44 create_new_svg(src, ffn, kw) 

45 write_file(fn_src_info, json.dumps([mtime, hsh or file_hash(src)])) 

46 return {'res': '![](%s)' % fn, 'formatted': True} lp|features/lp/plugs/drawio/index.md

47 

48 

49def create_new_svg(fn_src, fn_svg, kw): lp|features/lp/plugs/drawio/index.md

50 app.info('Exporting drawio', src=fn_src, svg=fn_svg) 

51 d = os.environ.get('drawio', 'drawio') 

52 sp.call([d, '--output', fn_svg, '--export', fn_src])