Coverage for src/lcdoc/mkdocs/lp/plugs/make_file/__init__.py: 71.70%

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

41 statements  

1""" 

2### make_file 

3 

4Creates a file and displays it as if we used cat on it. 

5 

6#### Parameters 

7- fn 

8- replace: optional replacements to apply (dict) 

9- content (the body of the lp block) 

10- chmod: optional chmod params 

11""" 

12 

13 

14import os, json lp|features/lp/xterm.md

15from lcdoc.lp_session import get_cwd lp|features/lp/xterm.md

16from lcdoc.tools import write_file lp|features/lp/xterm.md

17 

18# def write_file(fn, s, log=0, mkdir=0, chmod=None, mode='w', only_on_change=False): 

19fmt_default = 'mk_console' lp|features/lp/xterm.md

20 

21 

22def within_session_dir(session_name, func): lp|features/lp/xterm.md

23 if not session_name: 23 ↛ 25line 23 didn't jump to line 25, because the condition on line 23 was never falselp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

24 return func() lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

25 here = os.getcwd() 

26 try: 

27 os.chdir(get_cwd(session_name)) 

28 return func() 

29 finally: 

30 os.chdir(here) 

31 

32 

33def show_file(cmd, kw): lp|features/lp/xterm.md

34 """to have it all together we keep this mode here""" 

35 

36 def f(kw=kw): lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

37 fn = kw['fn'] lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

38 with open(fn, 'r') as fd: lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

39 c = fd.read() lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

40 res = {'cmd': 'cat %s' % fn, 'res': c} lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

41 if kw.get('lang') not in ['sh', 'bash']: lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

42 res['cmd'] = '$ ' + res['cmd'] lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.md

43 return res lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

44 

45 return within_session_dir(kw.get('session_name'), f) lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md

46 

47 

48def run(cmd, kw): lp|features/lp/xterm.md

49 

50 kw['content'] = cmd lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

51 session_name = kw.get('session_name') lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

52 

53 def f(kw=kw): lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

54 fn = kw['fn'] lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

55 c = kw['content'] lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

56 if kw.get('replace'): 56 ↛ 57line 56 didn't jump to line 57, because the condition on line 56 was never truelp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

57 d = dict(os.environ) 

58 d.update(kw) 

59 if '%(' in c and ')s' in c: 

60 c = c % d 

61 else: 

62 c = c.format(**d) 

63 

64 if kw.get('lang') in ('js', 'javascript', 'json'): lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

65 if isinstance(c, (dict, list, tuple)): 65 ↛ 67line 65 didn't jump to line 67, because the condition on line 65 was never falselp|features/lp/plugs/make_file/index.md

66 c = json.dumps(c, indent=4) lp|features/lp/plugs/make_file/index.md

67 write_file(fn, str(c), mkdir=True) lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

68 os.system('chmod %s %s' % (kw.get('chmod', 660), fn)) lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

69 return show_file('', kw) lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md

70 

71 return within_session_dir(session_name, f) lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md