Skip to content

Commit 54c8e53

Browse files
nx10pre-commit-ci[bot]reindervdw-cmi
authored
Setup update license badge fix #57 (#79)
* Setup update license badge * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Setup no license -> remove badge * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Escape - in badge; Refactoring * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ruff --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Reinder Vos de Wael <reinder.vosdewael@childmind.org>
1 parent cf6e892 commit 54c8e53

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

setup/licenses.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
import json
5+
import re
56
from datetime import datetime
67
from typing import Optional
78
from urllib import request
@@ -81,6 +82,33 @@ def request_license() -> Optional[dict[str, str]]:
8182
return final_license
8283

8384

85+
def replace_license_badge(content: str, repo_license: Optional[dict[str, str]]) -> str:
86+
"""Replaces the license badge with the specified license.
87+
88+
Args:
89+
content: Content of any file that might contain a license badge.
90+
repo_license: The license to replace the current license with. If None,
91+
the current license badge will be deleted.
92+
93+
Returns:
94+
The content with the license badge replaced.
95+
"""
96+
if repo_license is None:
97+
# remove line containing license badge
98+
return re.sub(
99+
r"\[!\[.*License\]\(https://img\.shields\.io/badge/license.*\)\]\(.*\)\n",
100+
"",
101+
content,
102+
)
103+
# shield.io uses -- as an escape character, so we need to replace - with --
104+
license_name_upper = repo_license["key"].upper().replace("-", "--")
105+
return content.replace(
106+
"![MIT License]" "(https://img.shields.io/badge/license-MIT-blue.svg)]",
107+
f"![{license_name_upper} License]"
108+
f"(https://img.shields.io/badge/license-{license_name_upper}-blue.svg)]",
109+
)
110+
111+
84112
def replace_license(repo_license: Optional[dict[str, str]]) -> None:
85113
"""Replaces the license file in the repository with the specified license.
86114

setup_template.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def main() -> None:
8686
content = content.replace("ENTER_YOUR_EMAIL_ADDRESS", email)
8787
content = content.replace("Reinder Vos de Wael", username)
8888

89+
content = licenses.replace_license_badge(content, repo_license)
90+
8991
if content != content_before:
9092
print(f"Updating {file.relative_to(DIR_REPO)}")
9193
with open(file, "w", encoding="utf-8") as f:

0 commit comments

Comments
 (0)