Merge "Clean up badge generation code"
This commit is contained in:
commit
88b3d1092f
|
@ -25,7 +25,9 @@ import projects
|
|||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
NUM_COL = 4
|
||||
PADDING = 8
|
||||
BADGE_SPACING = 4
|
||||
BASE_TAGS_URL = 'https://governance.openstack.org/tc/reference/tags/'
|
||||
COLOR_SCHEME = {
|
||||
"brightgreen": "#4c1",
|
||||
|
@ -42,8 +44,9 @@ COLOR_SCHEME = {
|
|||
SVG_ROOT = """<?xml version="1.0" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
|
||||
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<svg contentScriptType="text/ecmascript" zoomAndPan="magnify" contentStyleType="text/css"
|
||||
height="{height}" width="{width}" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg"
|
||||
<svg contentScriptType="text/ecmascript" zoomAndPan="magnify"
|
||||
contentStyleType="text/css" height="{height}" width="{width}"
|
||||
preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg"
|
||||
version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title id="os:gov:badges:title">
|
||||
This is a container for a set of OpenStack badges indicating the status and
|
||||
|
@ -52,7 +55,8 @@ SVG_ROOT = """<?xml version="1.0" standalone="no"?>
|
|||
{svg}
|
||||
</svg>
|
||||
"""
|
||||
FLAT_BADGE_TEMPLATE = """<svg id="{left_text}:{right_text}" width="{width}" height="20" x="{svg_x}" y="{svg_y}">
|
||||
FLAT_BADGE_TEMPLATE = """<svg id="{left_text}:{right_text}" width="{width}"
|
||||
height="20" x="{svg_x}" y="{svg_y}">
|
||||
<title>{left_text}:{right_text}</title>
|
||||
<a target="_blank" xlink:href="{link}">
|
||||
<linearGradient id="smooth:{left_text}:{right_text}" x2="0" y2="100%">
|
||||
|
@ -67,13 +71,17 @@ FLAT_BADGE_TEMPLATE = """<svg id="{left_text}:{right_text}" width="{width}" heig
|
|||
<g mask="url(#round:{left_text}:{right_text})">
|
||||
<rect width="{left_width}" height="20" fill="#555"/>
|
||||
<rect x="{left_width}" width="{right_width}" height="20" fill="{color}"/>
|
||||
<rect width="{width}" height="20" fill="url(#smooth:{left_text}:{right_text})"/>
|
||||
<rect width="{width}" height="20"
|
||||
fill="url(#smooth:{left_text}:{right_text})"/>
|
||||
</g>
|
||||
|
||||
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
|
||||
<text x="{left_x}" y="15" fill="#010101" fill-opacity=".3">{left_text}</text>
|
||||
<g fill="#fff" text-anchor="middle"
|
||||
font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
|
||||
<text x="{left_x}" y="15" fill="#010101"
|
||||
fill-opacity=".3">{left_text}</text>
|
||||
<text x="{left_x}" y="14">{left_text}</text>
|
||||
<text x="{right_x}" y="15" fill="#010101" fill-opacity=".3">{right_text}</text>
|
||||
<text x="{right_x}" y="15" fill="#010101"
|
||||
fill-opacity=".3">{right_text}</text>
|
||||
<text x="{right_x}" y="14">{right_text}</text>
|
||||
</g>
|
||||
</a>
|
||||
|
@ -81,7 +89,7 @@ FLAT_BADGE_TEMPLATE = """<svg id="{left_text}:{right_text}" width="{width}" heig
|
|||
"""
|
||||
|
||||
|
||||
def _generate_badge(left_text, right_text, link=None, colorscheme='brightgreen'):
|
||||
def _badge(left_text, right_text, link=None, colorscheme='brightgreen'):
|
||||
|
||||
font = ImageFont.truetype('DejaVuSans.ttf', 11)
|
||||
left_width = font.getsize(left_text)[0] + PADDING
|
||||
|
@ -105,10 +113,10 @@ def _generate_badge(left_text, right_text, link=None, colorscheme='brightgreen')
|
|||
return data
|
||||
|
||||
|
||||
def _generate_tag_badges(tags):
|
||||
def _get_badges(tags):
|
||||
badges = []
|
||||
|
||||
badges.append(_generate_badge('project', 'official'))
|
||||
badges.append(_badge('project', 'official'))
|
||||
|
||||
for tag in tags:
|
||||
# NOTE(flaper87): will submit other patches to make these
|
||||
|
@ -119,42 +127,40 @@ def _generate_tag_badges(tags):
|
|||
group, tname = tag.split(':')
|
||||
|
||||
link = BASE_TAGS_URL + '%s.html' % tag.replace(':', '_')
|
||||
badges.append(_generate_badge(group, tname, link, colorscheme='blue'))
|
||||
badges.append(_badge(group, tname, link, colorscheme='blue'))
|
||||
|
||||
return badges
|
||||
|
||||
|
||||
def _organize_badges(badges):
|
||||
sbadges = sorted(badges, key=lambda badge: badge['width'])
|
||||
|
||||
# NOTE(flaper87): 4 is the number of columns
|
||||
ziped = list(zip_longest(*(iter(sbadges),) * 4))
|
||||
# Arrange badges in NUM_COL columns, filling the rest with None
|
||||
ziped = list(zip_longest(*(iter(sbadges),) * NUM_COL))
|
||||
|
||||
result = []
|
||||
for y, group in enumerate(ziped):
|
||||
# Calculate x,y for each badge, leaving BADGE_SPACING between them
|
||||
for line, group in enumerate(ziped):
|
||||
# Start a new line
|
||||
result.append([])
|
||||
col_x = 0
|
||||
for x, badge in enumerate(group):
|
||||
# NOTE(flaper87): zip_longest fills the
|
||||
# empty slots with None. We don't care about
|
||||
# those.
|
||||
x = 0
|
||||
for col, badge in enumerate(group):
|
||||
# Skip None badges
|
||||
if badge is None:
|
||||
break
|
||||
|
||||
width_badge = ziped[-1][x]
|
||||
if width_badge is None:
|
||||
largest_badge = ziped[-1][col]
|
||||
if largest_badge is None:
|
||||
if len(ziped) > 1:
|
||||
width_badge = ziped[-2][x]
|
||||
largest_badge = ziped[-2][col]
|
||||
else:
|
||||
width_badge = badge
|
||||
largest_badge = badge
|
||||
|
||||
badge['height'] = 20
|
||||
badge['svg_y'] = (20 + 4) * y
|
||||
|
||||
# NOTE(flaper87): 3 is just an extra padding in case there are two badges
|
||||
# with the same width in the same row
|
||||
badge['svg_x'] = col_x
|
||||
col_x += width_badge['width'] + 3
|
||||
result[y].append(badge)
|
||||
badge['svg_y'] = (20 + BADGE_SPACING) * line
|
||||
badge['svg_x'] = x
|
||||
x += largest_badge['width'] + BADGE_SPACING
|
||||
result[line].append(badge)
|
||||
return result
|
||||
|
||||
|
||||
|
@ -173,7 +179,7 @@ def _generate_teams_badges(app, exception=None):
|
|||
os.mkdir(badges_dir)
|
||||
|
||||
filename = os.path.join(badges_dir, 'project-unofficial.svg')
|
||||
svg_data = _generate_badge('project', 'unofficial', colorscheme='red')
|
||||
svg_data = _badge('project', 'unofficial', colorscheme='red')
|
||||
svg = FLAT_BADGE_TEMPLATE.format(**svg_data)
|
||||
with open(filename, 'w', encoding='utf-8') as f:
|
||||
f.write(SVG_ROOT.format(height=20, width=106, svg=svg))
|
||||
|
@ -184,18 +190,19 @@ def _generate_teams_badges(app, exception=None):
|
|||
|
||||
for name, deliverable in info['deliverables'].items():
|
||||
tags = info.get('tags', []) + deliverable.get('tags', [])
|
||||
badges = _organize_badges(_generate_tag_badges(tags))
|
||||
badges = _organize_badges(_get_badges(tags))
|
||||
svg = '\n'.join(_to_svg(chain(*badges)))
|
||||
|
||||
root_width = max([bdg_row[-1]['width'] + bdg_row[-1]['svg_x']
|
||||
for bdg_row in badges])
|
||||
root_height = badges[-1][0]['svg_y'] + badges[-1][0]['height']
|
||||
|
||||
for repo in deliverable.get('repos', []):
|
||||
repo_name = repo.split('/')[1]
|
||||
filename = os.path.join(badges_dir, '%s.svg' % projects.slugify(repo_name))
|
||||
filename = os.path.join(badges_dir,
|
||||
'%s.svg' % projects.slugify(repo_name))
|
||||
with open(filename, 'w') as f:
|
||||
f.write(SVG_ROOT.format(height=root_height, width=root_width, svg=svg))
|
||||
f.write(SVG_ROOT.format(height=root_height,
|
||||
width=root_width, svg=svg))
|
||||
files.append(filename)
|
||||
|
||||
return files
|
||||
|
|
Loading…
Reference in New Issue