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_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.
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
55# class badges:
56# """We must the eval results statically into the source - otherwise README won't work"""
58# def gh_action(a, kw):
59# # a e.g. 'ci'
60# fn = kw['page'].file.abs_src_path
61# md = read_file(fn)
62# # md = kw['markdown']
63# config = kw['config']
64# ru = config['repo_url']
65# while ru.endswith('/'):
66# ru = ru[:-1]
67# y = '%s/actions/workflows/%s.yml' % (ru, a)
68# r = '[![%s](%s/badge.svg)][%s]' % (a, y, a)
69# if r in md:
70# return ''
71# b = '<!-- badges'
72# l = md.split(b, 1)
73# if not len(l) == 2:
74# app.die('have badges within a "<-- badges" html comment block')
75# k = l[1].split('-->', 1)
76# md = l[0] + r + '\n' + b + k[0] + '-->\n\n[%s]: %s\n\n' % (a, y) + k[1]
77# app.warning('Writing file with static badges links', fn=fn, badge=a, url=y)
78# write_file(fn, md)
79# return r
82config = lambda kw: kw['LP'].config lp|features/lp/plugs/make_badges/index.mdlp|index.md
84no_end_slash = lambda s: s if not s[-1] == '/' else s[:-1] lp|features/lp/plugs/make_badges/index.mdlp|index.md
88 def axblack(spec, kw): lp|index.md
90 lnk='https://pypi.org/project/axblack/',
91 label='code_style',
92 value='axblack',
93 color='#222222',
94 )
96 def gh_action(spec, kw): lp|index.md
97 a = spec.get('action', 'ci') lp|index.md
98 ru = no_end_slash(config(kw)['repo_url']) lp|index.md
99 u = '%s/actions/workflows/%s.yml' % (ru, a) lp|index.md
100 i = '%s/badge.svg' % u lp|index.md
101 return dict(lnk=u, img=i, label='gh-' + a) lp|index.md
103 def pypi(spec, kw): lp|index.md
104 lnk = project.packagehome() lp|index.md
105 value = project.version() lp|index.md
106 label = 'pypi' if value.startswith('https://pypi.org') else 'pkg' lp|index.md
107 color = '#8bd124' lp|index.md
108 return dict(locals()) lp|index.md
110 def docs(spec, kw): lp|index.md
111 lnk = configured_site_url(kw) lp|index.md
112 # lnk = config(kw)['site_url'] # 127.0.0.1 for mkdocs serve
113 value = spec.get('value', 'mkdocs-material') lp|index.md
114 label = 'docs' lp|index.md
115 if value == 'pagecount': 115 ↛ 121line 115 didn't jump to line 121, because the condition on line 115 was never falselp|index.md
116 d = config(kw)['docs_dir'] lp|index.md
117 spec['value'] = (
118 os.popen("cd '%s' && find . | grep 'md$' | wc -l" % d).read().strip()
119 )
120 label = 'docs pages' lp|index.md
121 color = '#331188' lp|index.md
122 return dict(locals()) lp|index.md
124 def generic(spec, kw): lp|index.md
125 spec['label'] = spec.get('label', spec['cmd']) lp|features/lp/plugs/make_badges/index.md
126 return dict(spec) lp|features/lp/plugs/make_badges/index.md
129def configured_site_url(kw, c=[0]): lp|index.md
130 if not c[0]: lp|features/lp/plugs/make_badges/index.mdlp|index.md
131 d = project.root(config(kw)) lp|index.md
132 d = read_file('%s/mkdocs.yml' % d, dflt='').split('\nnav:', 1)[0] lp|index.md
133 c[0] = yaml.safe_load(d)['site_url'] lp|index.md
134 return c[0] lp|features/lp/plugs/make_badges/index.mdlp|index.md
140def make_badge_svg_file(badge_fn, label, value, color='gray', **kw): lp|index.md
141 p = partial(ab.Badge, label=label, value=value, text_color='white') lp|features/lp/plugs/make_badges/index.mdlp|index.md
143 if not isinstance(color, dict): 143 ↛ 146line 143 didn't jump to line 146, because the condition on line 143 was never falselp|features/lp/plugs/make_badges/index.mdlp|index.md
144 badge = p(default_color=color) lp|features/lp/plugs/make_badges/index.mdlp|index.md
145 else:
146 badge = p(thresholds=color)
147 write_file(badge_fn, badge.badge_svg_text, mkdir=True, only_on_change=True) lp|features/lp/plugs/make_badges/index.mdlp|index.md
150def write_readme(page, config, content=None, **kw): lp|index.md
151 fn = project.root(config) + '/README.md'
152 insert_file(fn, content, sep='<!-- badges -->')
153 app.info('Have written README', fn=fn)
156def run(cmd, kw): lp|index.md
157 # prevents mkdocs serve loops, this counts up, changing the svgs all the time:
158 ab.Badge.mask_id = 0 lp|features/lp/plugs/make_badges/index.mdlp|index.md
159 project.root(config(kw)) lp|features/lp/plugs/make_badges/index.mdlp|index.md
160 project.load_config() lp|features/lp/plugs/make_badges/index.mdlp|index.md
161 specs = [] lp|features/lp/plugs/make_badges/index.mdlp|index.md
162 for spec in cmd: lp|features/lp/plugs/make_badges/index.mdlp|index.md
163 func = getattr(badges, spec['cmd'].replace('-', '_'), badges.generic) lp|features/lp/plugs/make_badges/index.mdlp|index.md
164 d = func(spec, kw) lp|features/lp/plugs/make_badges/index.mdlp|index.md
165 [d.pop(k, 0) for k in ['kw', 'spec']] # the locals hack lp|features/lp/plugs/make_badges/index.mdlp|index.md
166 for k in d: lp|features/lp/plugs/make_badges/index.mdlp|index.md
167 spec[k] = spec.get(k) or d[k] lp|features/lp/plugs/make_badges/index.mdlp|index.md
168 if not spec.get('img'): lp|features/lp/plugs/make_badges/index.mdlp|index.md
169 bdg = 'badge_%(cmd)s.svg' % spec lp|features/lp/plugs/make_badges/index.mdlp|index.md
170 fn = dirname(kw['LP'].page.file.abs_src_path) + '/img/' + bdg lp|features/lp/plugs/make_badges/index.mdlp|index.md
171 spec['badge_fn'] = fn lp|features/lp/plugs/make_badges/index.mdlp|index.md
172 # need an absolute path for the readme.md:
173 u = no_end_slash(configured_site_url(kw)) lp|features/lp/plugs/make_badges/index.mdlp|index.md
174 k = '/' + dirname(kw['LP'].page.file.src_path) lp|features/lp/plugs/make_badges/index.mdlp|index.md
175 u = no_end_slash(u + k) + '/img/' + bdg lp|features/lp/plugs/make_badges/index.mdlp|index.md
176 spec['img'] = u # .replace('//', '/') lp|features/lp/plugs/make_badges/index.mdlp|index.md
177 try: lp|features/lp/plugs/make_badges/index.mdlp|index.md
178 make_badge_svg_file(**spec) lp|features/lp/plugs/make_badges/index.mdlp|index.md
179 except Exception as ex:
180 app.error('Badge creation failed', exc=ex)
181 continue
182 specs.append(spec) lp|features/lp/plugs/make_badges/index.mdlp|index.md
183 r = '' lp|features/lp/plugs/make_badges/index.mdlp|index.md
184 l = [] lp|features/lp/plugs/make_badges/index.mdlp|index.md
185 for s in specs: lp|features/lp/plugs/make_badges/index.mdlp|index.md
186 r += '[![%(label)s][%(label)s_img]][%(label)s] ' % s lp|features/lp/plugs/make_badges/index.mdlp|index.md
187 l += ['[%(label)s]: %(lnk)s' % s] lp|features/lp/plugs/make_badges/index.mdlp|index.md
188 l += ['[%(label)s_img]: %(img)s' % s] lp|features/lp/plugs/make_badges/index.mdlp|index.md
189 l = '\n'.join(l) lp|features/lp/plugs/make_badges/index.mdlp|index.md
190 content = '\n'.join(['', r, '', l, '']) lp|features/lp/plugs/make_badges/index.mdlp|index.md
191 if kw.get('write_readme'): lp|features/lp/plugs/make_badges/index.mdlp|index.md
192 add_post_page_func(kw, partial(write_readme, content=content)) lp|index.md
194 return {'res': specs, 'formatted': content} lp|features/lp/plugs/make_badges/index.mdlp|index.md