diff options
-rw-r--r-- | app.py | 2 | ||||
-rw-r--r-- | repos/__init__.py | 8 | ||||
-rw-r--r-- | repos/base.py | 4 | ||||
-rw-r--r-- | templates/badge.svg.jinja | 30 |
4 files changed, 16 insertions, 28 deletions
@@ -14,7 +14,7 @@ def hello_world(): @app.route('/badge/<package>.svg') def badge(package: str): versions = repos.get_versions(package) - newest_version = max(v for family in versions.values() for v in family.values()) + newest_version = max(versions.values()) rendered = render_template('badge.svg.jinja', versions=versions, newest_version=newest_version) response = make_response(rendered) response.headers['Content-Type'] = 'image/svg+xml' diff --git a/repos/__init__.py b/repos/__init__.py index fc81260..ab6aa76 100644 --- a/repos/__init__.py +++ b/repos/__init__.py @@ -1,7 +1,7 @@ from typing import Mapping, List from . import alpine_linux, arch_linux -from .base import Repository +from .base import Repository, Version __all__ = [ 'get_versions', @@ -18,12 +18,10 @@ all_repos: List[Repository] = [ *repos_from(arch_linux), ] -def get_versions(package: str) -> Mapping[str, Mapping[str, str]]: +def get_versions(package: str) -> Mapping[str, Version]: result = dict() for repo in all_repos: repo_versions = repo.get_versions() if package in repo_versions: - if repo.family not in result: - result[repo.family] = dict() - result[repo.family][repo.repo] = repo_versions[package] + result[repo.full_name()] = repo_versions[package] return result diff --git a/repos/base.py b/repos/base.py index 1bd4ae3..c7790c1 100644 --- a/repos/base.py +++ b/repos/base.py @@ -60,7 +60,7 @@ class Repository: index_url: str parse: Callable[[Path], Mapping[str, Version]] - def _full_name(self): + def full_name(self): return f'{self.family} {self.repo}' def _cache_dir(self) -> Path: @@ -88,7 +88,7 @@ class Repository: response = requests.get(self.index_url, headers=headers, stream=True) if response.status_code != requests.codes.not_modified: response.raise_for_status() - print('Re-downloading', self._full_name()) + print('Re-downloading', self.full_name()) with downloaded_file.open('wb') as f: for chunk in response.iter_content(chunk_size=256): f.write(chunk) diff --git a/templates/badge.svg.jinja b/templates/badge.svg.jinja index 972c39a..8143cff 100644 --- a/templates/badge.svg.jinja +++ b/templates/badge.svg.jinja @@ -2,20 +2,13 @@ {%- set ROW_MARGIN = 2 -%} {%- set FONT_SIZE_PERCENT = 75 -%} {%- set FONT_SIZE = (ROW_HEIGHT - ROW_MARGIN) * FONT_SIZE_PERCENT / 100 -%} -{%- set ROWS = 1 + versions|length + (versions.values()|map('length')|sum) -%} +{%- set ROWS = 1 + versions|length -%} {%- set OUTER_MARGIN = 5 -%} -{%- set REPO_LEFT_MARGIN = 15 -%} {%- set FONT_WIDENESS = 0.5 -%} -{# ugh. just ugh. #} -{%- set ns = namespace(longest_repo = 0, longest_version = 0) -%} -{%- for family_contents in versions.values() -%} - {%- set this_longest_repo = family_contents.keys()|map('length')|max -%} - {%- set this_longest_version = family_contents.values()|map('string')|map('length')|max -%} - {%- set ns.longest_repo = [ns.longest_repo, this_longest_repo]|max -%} - {%- set ns.longest_version = [ns.longest_version, this_longest_version]|max -%} -{%- endfor -%} -{%- set VERSION_X = OUTER_MARGIN + REPO_LEFT_MARGIN + ns.longest_repo * FONT_WIDENESS * FONT_SIZE -%} -{%- set VERSION_WIDTH = OUTER_MARGIN + ns.longest_version * FONT_WIDENESS * FONT_SIZE -%} +{%- set LONGEST_REPO = versions.keys()|map('length')|max -%} +{%- set LONGEST_VERSION = versions.values()|map('string')|map('length')|max -%} +{%- set VERSION_X = OUTER_MARGIN + LONGEST_REPO * FONT_WIDENESS * FONT_SIZE -%} +{%- set VERSION_WIDTH = OUTER_MARGIN + LONGEST_VERSION * FONT_WIDENESS * FONT_SIZE -%} {%- set WIDTH = VERSION_X + VERSION_WIDTH -%} {%- set ns = namespace(y = 0) -%} {%- macro y(offset=0) -%}{{ ns.y + offset }}{%- endmacro -%} @@ -27,13 +20,10 @@ <text x="5" y="{{ texty() }}" fill="white">Packaging Status</text> <line x1="0" y1="{{ y(ROW_HEIGHT - ROW_MARGIN / 2) }}" x2="{{ WIDTH }}" y2="{{ y(ROW_HEIGHT - ROW_MARGIN / 2) }}" stroke="white" stroke-width="{{ ROW_MARGIN }}"></line>{{ end_row() }} - {% for family, family_contents in versions.items() %} - <text x="{{ OUTER_MARGIN }}" y="{{ texty() }}">{{ family }}</text>{{ end_row() }} - {% for repo, version in family_contents.items() %} - <text x="{{ REPO_LEFT_MARGIN }}" y="{{ texty() }}">{{ repo }}</text> - <rect x="{{ VERSION_X }}" y="{{ y() }}" width="{{ VERSION_WIDTH }}" height="{{ ROW_HEIGHT }}" - fill="{% if version < newest_version %}red{% else %}green{% endif %}"></rect> - <text x="{{ VERSION_X + OUTER_MARGIN }}" y="{{ texty() }}">{{ version }}</text>{{ end_row() }} - {% endfor %} + {% for repo, version in versions.items() %} + <text x="{{ OUTER_MARGIN }}" y="{{ texty() }}">{{ repo }}</text> + <rect x="{{ VERSION_X }}" y="{{ y() }}" width="{{ VERSION_WIDTH }}" height="{{ ROW_HEIGHT }}" + fill="{% if version < newest_version %}red{% else %}green{% endif %}"></rect> + <text x="{{ VERSION_X + OUTER_MARGIN }}" y="{{ texty() }}">{{ version }}</text>{{ end_row() }} {% endfor %} </svg> |