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"""
2Common tools for all modules
4"""
5import collections lppytest
6import hashlib lppytest
7import json lppytest
8import os lppytest
9import socket lppytest
10import sys lppytest
11import time lppytest
13import toml lppytest
15from lcdoc.log import app, now lppytest
17exists = os.path.exists lppytest
19# fastest algo:
20def file_hash(fn, algo='blake2b'): lppytest
21 return getattr(hashlib, algo)(open(fn, 'rb').read()).hexdigest() lp|features/lp/plugs/drawio/index.md
24def dirname(fn, create=False): lppytest
25 d = os.path.dirname(fn) lplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest
26 if create: 26 ↛ 27line 26 didn't jump to line 27, because the condition on line 26 was never truelplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest
27 os.makedirs(d, exist_ok=True)
28 return d lplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest
31def require(cmd, name=None, die=True, _have=set()): lppytest
32 '''cmd e.g. "rg --version"'''
33 name = name or cmd.split(' ', 1)[0] lplp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
34 if cmd in _have: lplp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
35 return True lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
36 if os.system(cmd + ' >/dev/null') == 0: 36 ↛ 40line 36 didn't jump to line 40, because the condition on line 36 was never falselplp|features/lp/eval.md
37 _have.add(cmd) lplp|features/lp/eval.md
38 return True lplp|features/lp/eval.md
40 msg = 'Missing required command: %s' % name
41 if die:
42 app.die(msg, tried=cmd)
43 else:
44 app.warning(msg, tried=cmd)
45 return False
48def hostname(c=[0]): lppytest
49 i = c[0]
50 if i:
51 return i
52 c[0] = socket.gethostname()
53 return c[0]
56def into(d, k, v): lppytest
57 """the return is important for e.g. rx"""
58 d[k] = v
59 return d
62def to_list(o): lppytest
63 o = [] if o is None else o lp|features/lp/python/call_flow_logging/index.mdpytest|tests.test_cfl.test_one
64 t = type(o) lp|features/lp/python/call_flow_logging/index.mdpytest|tests.test_cfl.test_one
65 return o if t == list else list(o) if t == tuple else [o] lp|features/lp/python/call_flow_logging/index.mdpytest|tests.test_cfl.test_one
68# ----------------------------------------------------------------------------- file ops
69def walk_dir(directory, crit=None): lppytest
70 crit = (lambda *a: True) if crit is None else crit
71 files = []
72 j = os.path.join
73 for (dirpath, dirnames, filenames) in os.walk(directory):
74 files += [j(dirpath, file) for file in filenames if crit(dirpath, file)]
75 return files
78def read_file(fn, dflt=None, bytes=-1, strip_comments=False): lppytest
79 """
80 API function.
81 read a file - return a default if it does not exist"""
82 if not exists(fn): lplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.md
83 if dflt is not None: 83 ↛ 85line 83 didn't jump to line 85, because the condition on line 83 was never falselplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.md
84 return dflt lplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.md
85 raise Exception(fn, 'does not exist')
86 with open(fn) as fd: lplp|about/credits.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_src/index.mdlp|index.mdlp|index.mdlp|index.md
87 # no idea why but __etc__hostname always contains a linesep at end
88 # not present in source => rstrip(), hope this does not break templs
89 res = fd.read(bytes) lplp|about/credits.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_src/index.mdlp|index.mdlp|index.mdlp|index.md
90 res = res if not res.endswith('\n') else res[:-1] lplp|about/credits.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_src/index.mdlp|index.mdlp|index.mdlp|index.md
91 if strip_comments: 91 ↛ 92line 91 didn't jump to line 92, because the condition on line 91 was never truelplp|about/credits.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_src/index.mdlp|index.mdlp|index.mdlp|index.md
92 lines = res.splitlines()
93 res = '\n'.join([l for l in lines if not l.startswith('#')])
94 return res lplp|about/credits.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_src/index.mdlp|index.mdlp|index.mdlp|index.md
97def insert_file(fn, content, sep): lppytest
98 s = read_file(fn).split(sep) lp|index.md
99 if not len(s) == 3: 99 ↛ 100line 99 didn't jump to line 100, because the condition on line 99 was never truelp|index.md
100 raise Exception(f'No 2 times occurrance of sep in file. sep: {sep}, fn: {fn}')
101 write_file(fn, s[0] + sep + content + sep + s[2]) lp|index.md
104def write_file(fn, s, log=0, mkdir=0, chmod=None, mode='w', only_on_change=False): lppytest
105 'API: Write a file. chmod e.g. 0o755 (as octal integer)'
107 fn = os.path.abspath(fn) lp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
109 if isinstance(s, (list, tuple)): 109 ↛ 110line 109 didn't jump to line 110, because the condition on line 109 was never truelp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
110 s = '\n'.join(s)
111 if only_on_change: lp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
112 so = read_file(fn, dflt='xxx') lp|features/lp/parameters.mdlp|features/lp/plugs/make_badges/index.mdlp|index.md
113 if s == so: lp|features/lp/parameters.mdlp|features/lp/plugs/make_badges/index.mdlp|index.md
116 if log > 0: 116 ↛ 117line 116 didn't jump to line 117, because the condition on line 116 was never truelp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
117 app.info('Writing file', fn=fn)
118 if log > 1: 118 ↛ 119line 118 didn't jump to line 119, because the condition on line 118 was never truelp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
119 sep = '\n----------------------\n'
120 ps = (
121 s
122 if not 'key' in fn and not 'assw' in fn and not 'ecret' in fn
123 else '<hidden>'
124 )
125 app.debug('Content', content=sep + ps + sep[:-1])
126 e = None lp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
127 while True: lp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
128 try: lp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
129 with open(fn, mode) as fd: lp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
130 fd.write(str(s)) lp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
131 if chmod: 131 ↛ 132line 131 didn't jump to line 132, because the condition on line 131 was never truelp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
132 if not isinstance(chmod, (list, tuple)):
133 chmod = [int(chmod)]
134 for s in chmod:
135 os.chmod(fn, s)
136 return fn lp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytest|tests.test_cfl.test_one
137 except IOError as ex: 137 ↛ 144line 137 didn't jump to line 144lp|features/lp/python/call_flow_logging/index.md
138 if mkdir: 138 ↛ 143line 138 didn't jump to line 143, because the condition on line 138 was never falselp|features/lp/python/call_flow_logging/index.md
139 mkdir = 0 lp|features/lp/python/call_flow_logging/index.md
140 d = os.path.dirname(fn) lp|features/lp/python/call_flow_logging/index.md
141 os.makedirs(d) lp|features/lp/python/call_flow_logging/index.md
142 continue lp|features/lp/python/call_flow_logging/index.md
143 e = ex
144 except Exception as ex:
145 e = ex
146 app.die('Could not write file', filename=fn, exc=e)
149have_tty = lambda: sys.stdin.isatty() and sys.stdout.isatty() 149 ↛ exitline 149 didn't run the lambda on line 149lppytest
152def flatten(d, sep='_', tpljoin=None): lppytest
153 """when tpljoin is given we detect them as keys and join
154 (enables json serialization)"""
156 obj = collections.OrderedDict() lp
158 def recurse(t, parent_key=''): lp
160 if isinstance(t, list): lp
161 for i in range(len(t)): lp
162 recurse(t[i], parent_key + sep + str(i) if parent_key else str(i)) lp
163 elif isinstance(t, dict): lp
164 for k, v in t.items(): lp
165 if isinstance(k, tuple): 165 ↛ 166line 165 didn't jump to line 166, because the condition on line 165 was never truelp
166 k = tpljoin.join(k)
167 recurse(v, parent_key + sep + k if parent_key else k) lp
168 else:
169 obj[parent_key] = t lp
171 recurse(d) lp
173 return obj lp
176class project: lppytest
177 """loads the project config
179 Main app config put into project.cfg['app']
180 An optional tool.lc.app section will be merged into that
182 TODO: other formats than pyproject.toml
184 """
186 config, dir_home, fn_cfg = {}, None, None lppytest
188 def root(config=None, root=None, c=[0]): lppytest
189 # config maybe given. Understood currently: mkdocs config
190 if root is not None: lplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytestpytest|tests.test_cfl.test_one
191 c[0] = root # for tests, to force it pytest
192 if c[0]: lplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytestpytest|tests.test_cfl.test_one
193 return c[0] lplp|about/coverage.mdlp|about/credits.mdlp|features/lp/bash/index.mdlp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/examples.mdlp|features/lp/parameters.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/chart/index.mdlp|features/lp/plugs/chartist/index.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/drawio/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/flowchart/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/make_badges/index.mdlp|features/lp/plugs/make_file/index.mdlp|features/lp/plugs/markmap/index.mdlp|features/lp/plugs/mermaid/index.mdlp|features/lp/plugs/show_file/index.mdlp|features/lp/plugs/show_src/index.mdlp|features/lp/python/_tech.mdlp|features/lp/python/_tech.mdlp|features/lp/python/call_flow_logging/index.mdlp|features/lp/sessions.mdlp|features/lp/syntax.mdlp|features/lp/syntax.mdlp|features/lp/tips.mdlp|features/lp/xterm.mdlp|index.mdlp|index.mdlp|index.mdpytestpytest|tests.test_cfl.test_one
194 ret = 0 lppytest
195 try: lppytest
196 if config and hasattr(config, '__getitem__'): 196 ↛ 202line 196 didn't jump to line 202, because the condition on line 196 was never falselppytest
197 dd = config.get('docs_dir') lppytest
198 if dd: 198 ↛ 202line 198 didn't jump to line 202, because the condition on line 198 was never falselppytest
199 ret = dirname(dd) lppytest
200 return ret lppytest
201 # todo: look at git here
202 raise Exception('Cannot derive project root')
203 finally:
204 c[0] = ret lppytest
206 fn_config = lambda: project.root() + '/pyproject.toml' lplp|features/lp/plugs/make_badges/index.mdlp|index.mdpytest
207 d_autodocs = lambda: project.root() + '/build/autodocs' 207 ↛ exitline 207 didn't run the lambda on line 207lppytest
209 def abs_path(fn, config=None, mkdirs=False): lppytest
210 r = project.root(config)
211 if not fn:
212 return r
213 if fn[0] != '/':
214 fn = r + '/' + fn
215 if mkdirs:
216 os.makedirs(dirname(fn), exist_ok=True)
217 return fn
219 # TODO: understand also poetry and piptools:
220 def load_config(): lppytest
222 fn = project.fn_config() lp|features/lp/plugs/make_badges/index.mdlp|index.md
223 if not exists(fn): 223 ↛ 224line 223 didn't jump to line 224, because the condition on line 223 was never truelp|features/lp/plugs/make_badges/index.mdlp|index.md
224 app.die('no config found', fn=fn)
226 cfg = toml.load(fn) lp|features/lp/plugs/make_badges/index.mdlp|index.md
227 app.info('loaded config', filename=fn) lp|features/lp/plugs/make_badges/index.mdlp|index.md
228 c = project.config lp|features/lp/plugs/make_badges/index.mdlp|index.md
229 c.update(cfg) lp|features/lp/plugs/make_badges/index.mdlp|index.md
230 if not 'project' in c: 230 ↛ 231line 230 didn't jump to line 231, because the condition on line 230 was never truelp|features/lp/plugs/make_badges/index.mdlp|index.md
231 c['project'] = {'urls': {}}
232 if 'tool' in c and 'poetry' in c['tool']: 232 ↛ 237line 232 didn't jump to line 237, because the condition on line 232 was never falselp|features/lp/plugs/make_badges/index.mdlp|index.md
233 c['project'].update(c['tool']['poetry']) lp|features/lp/plugs/make_badges/index.mdlp|index.md
234 c['project']['urls']['homepage'] = c['project']['homepage'] lp|features/lp/plugs/make_badges/index.mdlp|index.md
235 c['project']['urls']['repository'] = c['project']['repository'] lp|features/lp/plugs/make_badges/index.mdlp|index.md
237 t = cfg['tool'] lp|features/lp/plugs/make_badges/index.mdlp|index.md
238 # breakpoint() # FIXME BREAKPOINT
239 # c['app'] = t['poetry']
240 # c['app'].update(t.get('lc', {}).get('app', {}))
241 project.fn_cfg = fn lp|features/lp/plugs/make_badges/index.mdlp|index.md
242 # app.die('Did not find a pyproject.toml file with badges declared')
243 return project.config lp|features/lp/plugs/make_badges/index.mdlp|index.md
245 def conf(): lppytest
246 return project.config or project.load_config() lp|about/credits.mdlp|index.md
248 def name(): lppytest
249 p = project.conf() lp|about/credits.mdlp|index.md
250 return p['project']['name'] lp|about/credits.mdlp|index.md
252 def urls(): lppytest
253 p = project.conf()['project'] lp|index.md
254 if 'urls' in p: 254 ↛ 256line 254 didn't jump to line 256, because the condition on line 254 was never falselp|index.md
255 return p['urls'] lp|index.md
256 urls = 'packagehome', 'discusshome', 'homepage', 'repository'
257 return {k: p.get(k, '') for k in urls}
259 def homepage(): lppytest
260 return project.urls().get('homepage', 'n.a.')
262 def repository(): lppytest
263 return project.urls().get('repository', 'n.a.')
265 def packagehome(): lppytest
266 return (
267 project.urls().get('packagehome')
268 or 'https://pypi.org/project/%s/_VERSION_/' % project.name()
269 ).replace('_VERSION_', project.version())
271 def version(): lppytest
272 p = project.conf()['project'] lp|index.md
273 v = p['version'] lp|index.md
274 if isinstance(v, dict) and v.get('use_scm'): 274 ↛ 275line 274 didn't jump to line 275, because the condition on line 274 was never truelp|index.md
275 raise NotImplemented('currently only poetry')
276 # from pdm.pep517.scm import get_version_from_scm
277 # v = get_version_from_scm(project.root())
280 def dependencies(): lppytest
281 d = project.conf()['project']['dependencies'] lp|about/credits.md
282 if isinstance(d, dict): 282 ↛ 286line 282 didn't jump to line 286, because the condition on line 282 was never falselp|about/credits.md
283 # poetry:
284 return d lp|about/credits.md
285 # pdm (pep)
286 return parse_deps(d)
288 def dev_dependencies(): lppytest
289 p = project.conf()['project'] lp|about/credits.md
290 dd = p.get('dev-dependencies') lp|about/credits.md
291 if dd: 291 ↛ 297line 291 didn't jump to line 297lp|about/credits.md
292 if isinstance(dd, dict): 292 ↛ 295line 292 didn't jump to line 295, because the condition on line 292 was never falselp|about/credits.md
293 # poetry - already dict:
294 return dd lp|about/credits.md
295 return parse_deps(dd)
297 r = [
298 l
299 for k in project.conf()['tool']['pdm']['dev-dependencies'].values()
300 for l in k
301 ]
302 return parse_deps(r)
304 def lock_data(): lppytest
305 fn = [] lp|about/credits.md
306 for k in 'pdm', 'poetry': 306 ↛ 310line 306 didn't jump to line 310, because the loop on line 306 didn't completelp|about/credits.md
307 fn.insert(0, project.root() + '/%s.lock' % k) lp|about/credits.md
308 if exists(fn[0]): lp|about/credits.md
309 return toml.load(fn[0]) lp|about/credits.md
310 app.die('No lock file in root', fn)
313def parse_deps(deplist, seps='~<>!= '): lppytest
314 m = {}
315 for dep in deplist:
316 h = False
317 for s in seps:
318 if s in dep:
319 l = dep.split(s, 1)
320 m[l[0]] = (s + l[1]).strip()
321 h = True
322 break
323 if not h:
324 m[dep] = ''
325 return m