Hot-keys 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
1"""
2### make_file
4Creates a file and displays it as if we used cat on it.
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"""
14import os, json lp|features/lp/xterm.md
15from lcdoc.lp_session import get_cwd lp|features/lp/xterm.md
17fmt_default = 'mk_console' lp|features/lp/xterm.md
20def within_session_dir(session_name, func): lp|features/lp/xterm.md
21 if not session_name: 21 ↛ 23line 21 didn't jump to line 23, because the condition on line 21 was never falselp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md
22 return func() lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md
23 here = os.getcwd()
24 try:
25 os.chdir(get_cwd(session_name))
26 return func()
27 finally:
28 os.chdir(here)
31def show_file(cmd, kw): lp|features/lp/xterm.md
32 """to have it all together we keep this mode here"""
34 def f(kw=kw): lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md
35 fn = kw['fn'] lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md
36 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
37 c = fd.read() lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md
38 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
39 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
40 res['cmd'] = '$ ' + res['cmd'] lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.md
41 return res lp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/xterm.md
43 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
46def run(cmd, kw): lp|features/lp/xterm.md
48 kw['content'] = cmd lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
49 session_name = kw.get('session_name') lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
51 def f(kw=kw): lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
52 fn = kw['fn'] lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
53 c = kw['content'] lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
54 if kw.get('replace'): 54 ↛ 55line 54 didn't jump to line 55, because the condition on line 54 was never truelp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
55 d = dict(os.environ)
56 d.update(kw)
57 if '%(' in c and ')s' in c:
58 c = c % d
59 else:
60 c = c.format(**d)
62 if kw.get('lang') in ('js', 'javascript', 'json'): lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
63 if isinstance(c, (dict, list, tuple)): 63 ↛ 65line 63 didn't jump to line 65, because the condition on line 63 was never falselp|features/lp/plugs/make_file/index.md
64 c = json.dumps(c, indent=4) lp|features/lp/plugs/make_file/index.md
65 with open(fn, 'w') as fd: lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
66 fd.write(str(c)) lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
67 os.system('chmod %s %s' % (kw.get('chmod', 660), fn)) lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
68 return show_file('', kw) lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md
70 return within_session_dir(session_name, f) lp|features/lp/plugs/make_file/index.mdlp|features/lp/xterm.md