Coverage for src/lcdoc/mkdocs/lp/plugs/make_badges/__init__.py: 90.98%
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
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
1"""
2### `make_badges`
4Creates badges. Optionally writes the README.md
7#### Format
9Line separated badge function names with statement level lp parameters.
11Functions:
13 - axblack
14 - docs (with value=[pagecount], default "mkdocs-material")
15 - gh_action (with action parameter, default ci)
16 - pypi
18Params:
20 - value
21 - label
22 - color
23 - lnk
24 - img
26#### Parameters
28- write_readme: Create the readme with static badges. All img links then to remote URLs.
30"""
36from functools import partial lp|index.md
38import anybadge as ab lp|index.md
40from lcdoc.mkdocs.tools import add_post_page_func, srclink lp|index.md
41from lcdoc.tools import ( lp|index.md
42 app,
43 dirname,
44 exists,
45 now,
46 os,
47 project,
48 read_file,
49 write_file,
50 insert_file,
51)
53multi_line_to_list = True lp|index.md
55config = lambda kw: kw['LP'].config lp|features/lp/plugs/make_badges/index.mdlp|index.md
57no_end_slash = lambda s: s if not s[-1] == '/' else s[:-1] lp|index.md
61 def axblack(spec, kw): lp|index.md
63 lnk='https://pypi.org/project/axblack/',
64 label='code_style',
65 value='axblack',
66 color='#222222',
67 )
69 def gl_ci(spec, kw): lp|index.md
70 # https://gitlab.axiros.com/klessinger/lc-python/-/pipelines
71 u = no_end_slash(config(kw)['repo_url']) + '/-/pipelines'
72 return dict(lnk=u, label='gl-ci')
74 def gh_action(spec, kw): lp|index.md
75 a = spec.get('action', 'ci') lp|index.md
76 ru = no_end_slash(config(kw)['repo_url']) lp|index.md
77 u = '%s/actions/workflows/%s.yml' % (ru, a) lp|index.md
78 i = '%s/badge.svg' % u lp|index.md
79 return dict(lnk=u, img=i, label='gh-' + a) lp|index.md
81 def pypi(spec, kw): lp|index.md
82 lnk = project.packagehome() lp|index.md
83 value = project.version() lp|index.md
84 label = 'pypi' if value.startswith('https://pypi.org') else 'pkg' lp|index.md
85 color = '#8bd124' lp|index.md
86 return dict(locals()) lp|index.md
88 def docs(spec, kw): lp|index.md
89 lnk = configured_site_url(kw) lp|index.md
90 # lnk = config(kw)['site_url'] # 127.0.0.1 for mkdocs serve
91 value = spec.get('value', 'mkdocs-material') lp|index.md
93 if value == 'pagecount': 93 ↛ 99line 93 didn't jump to line 99, because the condition on line 93 was never falselp|index.md
94 d = config(kw)['docs_dir'] lp|index.md
95 spec['value'] = (
96 os.popen("cd '%s' && find . | grep 'md$' | wc -l" % d).read().strip()
97 )
98 label = 'docs pages' lp|index.md
99 color = '#331188' lp|index.md
100 return dict(locals()) lp|index.md
102 def generic(spec, kw): lp|index.md
103 spec['label'] = spec.get('label', spec['cmd']) lp|features/lp/plugs/make_badges/index.md
104 return dict(spec) lp|features/lp/plugs/make_badges/index.md
107def configured_site_url(kw, c=[0]): lp|index.md
109 d = project.root(config(kw)) lp|index.md
110 d = read_file('%s/mkdocs.yml' % d, dflt='').split('\nnav:', 1)[0] lp|index.md
111 c[0] = yaml.safe_load(d)['site_url'] lp|index.md
118def make_badge_svg_file(badge_fn, label, value, color='gray', **kw): lp|index.md
119 p = partial(ab.Badge, label=label, value=value, text_color='white') lp|features/lp/plugs/make_badges/index.mdlp|index.md
121 if not isinstance(color, dict): 121 ↛ 124line 121 didn't jump to line 124, because the condition on line 121 was never falselp|features/lp/plugs/make_badges/index.mdlp|index.md
122 badge = p(default_color=color) lp|features/lp/plugs/make_badges/index.mdlp|index.md
123 else:
124 badge = p(thresholds=color)
125 write_file(badge_fn, badge.badge_svg_text, mkdir=True, only_on_change=True) lp|features/lp/plugs/make_badges/index.mdlp|index.md
128def write_readme(page, config, content=None, **kw): lp|index.md
129 # :docs:insert_readme_badges
130 fn = project.root(config) + '/README.md'
131 insert_file(fn, content, sep='<!-- badges -->')
132 app.info('Have inserted badges into README', file=fn)
133 # :docs:insert_readme_badges
136def run(cmd, kw): lp|index.md
137 # prevents mkdocs serve loops, this counts up, changing the svgs all the time:
138 ab.Badge.mask_id = 0 lp|features/lp/plugs/make_badges/index.mdlp|index.md
139 project.root(config(kw)) lp|features/lp/plugs/make_badges/index.mdlp|index.md
140 project.load_config() lp|features/lp/plugs/make_badges/index.mdlp|index.md
141 specs = [] lp|features/lp/plugs/make_badges/index.mdlp|index.md
142 for spec in cmd: lp|features/lp/plugs/make_badges/index.mdlp|index.md
143 func = getattr(badges, spec['cmd'].replace('-', '_'), badges.generic) lp|features/lp/plugs/make_badges/index.mdlp|index.md
144 d = func(spec, kw) lp|features/lp/plugs/make_badges/index.mdlp|index.md
145 [d.pop(k, 0) for k in ['kw', 'spec']] # the locals hack lp|features/lp/plugs/make_badges/index.mdlp|index.md
146 for k in d: lp|features/lp/plugs/make_badges/index.mdlp|index.md
147 spec[k] = spec.get(k) or d[k] lp|features/lp/plugs/make_badges/index.mdlp|index.md
148 if not spec.get('img'): lp|features/lp/plugs/make_badges/index.mdlp|index.md
149 bdg = 'badge_%(cmd)s.svg' % spec lp|features/lp/plugs/make_badges/index.mdlp|index.md
150 fn = dirname(kw['LP'].page.file.abs_src_path) + '/img/' + bdg lp|features/lp/plugs/make_badges/index.mdlp|index.md
151 spec['badge_fn'] = fn lp|features/lp/plugs/make_badges/index.mdlp|index.md
152 # need an absolute path for the readme.md:
153 if kw.get('write_readme'): lp|features/lp/plugs/make_badges/index.mdlp|index.md
154 u = no_end_slash(configured_site_url(kw)) lp|index.md
155 k = '/' + dirname(kw['LP'].page.file.src_path) lp|index.md
156 u = no_end_slash(u + k) + '/img/' + bdg lp|index.md
157 spec['img'] = u # .replace('//', '/') lp|index.md
158 else:
159 # else just normal rel. path:
160 spec['img'] = './img/' + bdg lp|features/lp/plugs/make_badges/index.md
161 try: lp|features/lp/plugs/make_badges/index.mdlp|index.md
162 make_badge_svg_file(**spec) lp|features/lp/plugs/make_badges/index.mdlp|index.md
163 except Exception as ex:
164 app.error('Badge creation failed', exc=ex)
165 continue
166 specs.append(spec) lp|features/lp/plugs/make_badges/index.mdlp|index.md
167 r = '' lp|features/lp/plugs/make_badges/index.mdlp|index.md
168 l = [] lp|features/lp/plugs/make_badges/index.mdlp|index.md
169 for s in specs: lp|features/lp/plugs/make_badges/index.mdlp|index.md
170 r += '[![%(label)s][%(label)s_img]][%(label)s] ' % s lp|features/lp/plugs/make_badges/index.mdlp|index.md
171 l += ['[%(label)s]: %(lnk)s' % s] lp|features/lp/plugs/make_badges/index.mdlp|index.md
172 l += ['[%(label)s_img]: %(img)s' % s] lp|features/lp/plugs/make_badges/index.mdlp|index.md
173 l = '\n'.join(l) lp|features/lp/plugs/make_badges/index.mdlp|index.md
174 content = '\n'.join(['', r, '', l, '']) lp|features/lp/plugs/make_badges/index.mdlp|index.md
175 if kw.get('write_readme'): lp|features/lp/plugs/make_badges/index.mdlp|index.md
176 add_post_page_func(kw, partial(write_readme, content=content)) lp|index.md
178 return {'res': specs, 'formatted': content} lp|features/lp/plugs/make_badges/index.mdlp|index.md