mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2025-07-10 16:12:29 +03:00
Add docstrings to the update_contributors.py file
This commit is contained in:
parent
5c77042b59
commit
333da12f51
|
@ -8,6 +8,7 @@ CURRENT_FILE = Path(__file__)
|
||||||
ROOT = CURRENT_FILE.parents[1]
|
ROOT = CURRENT_FILE.parents[1]
|
||||||
BOT_LOGINS = ["pyup-bot"]
|
BOT_LOGINS = ["pyup-bot"]
|
||||||
|
|
||||||
|
# Jinja template for CONTRIBUTORS.md
|
||||||
CONTRIBUTORS_TEMPLATE = """
|
CONTRIBUTORS_TEMPLATE = """
|
||||||
# Contributors
|
# Contributors
|
||||||
|
|
||||||
|
@ -69,9 +70,19 @@ guidance and advice.
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
"""
|
||||||
|
Script entry point.
|
||||||
|
|
||||||
|
1. Fetch recent contribtors from the Github API
|
||||||
|
2. Add missing ones to the JSON file
|
||||||
|
3. Generate Markdown from JSON file
|
||||||
|
"""
|
||||||
|
# Use Github API to fetch recent authors rather than
|
||||||
|
# git CLI because we need to know their GH username
|
||||||
gh = GitHub()
|
gh = GitHub()
|
||||||
recent_authors = set(gh.iter_recent_authors())
|
recent_authors = set(gh.iter_recent_authors())
|
||||||
|
|
||||||
|
# Add missing users to the JSON file
|
||||||
contrib_file = ContributorsJSONFile()
|
contrib_file = ContributorsJSONFile()
|
||||||
for username in recent_authors:
|
for username in recent_authors:
|
||||||
print(f"Checking if {username} should be added")
|
print(f"Checking if {username} should be added")
|
||||||
|
@ -81,10 +92,13 @@ def main() -> None:
|
||||||
print(f"Added {username} to contributors")
|
print(f"Added {username} to contributors")
|
||||||
contrib_file.save()
|
contrib_file.save()
|
||||||
|
|
||||||
|
# Generate MD file from JSON file
|
||||||
write_md_file(contrib_file.content)
|
write_md_file(contrib_file.content)
|
||||||
|
|
||||||
|
|
||||||
class GitHub:
|
class GitHub:
|
||||||
|
"""Small wrapper around Github REST API."""
|
||||||
|
|
||||||
base_url = "https://api.github.com"
|
base_url = "https://api.github.com"
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
@ -107,29 +121,36 @@ class GitHub:
|
||||||
|
|
||||||
|
|
||||||
class ContributorsJSONFile:
|
class ContributorsJSONFile:
|
||||||
|
"""Helper to interact with the JSON file."""
|
||||||
|
|
||||||
file_path = ROOT / ".github" / "contributors.json"
|
file_path = ROOT / ".github" / "contributors.json"
|
||||||
content = None
|
content = None
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
"""Read initial content."""
|
||||||
self.content = json.loads(self.file_path.read_text())
|
self.content = json.loads(self.file_path.read_text())
|
||||||
|
|
||||||
def __contains__(self, github_login: str):
|
def __contains__(self, github_login: str):
|
||||||
|
"""Provide a nice API to do: `username in file`."""
|
||||||
return any(github_login == contrib["github_login"] for contrib in self.content)
|
return any(github_login == contrib["github_login"] for contrib in self.content)
|
||||||
|
|
||||||
def add_contributor(self, user_data):
|
def add_contributor(self, user_data):
|
||||||
|
"""Append the contributor data we care about at the end."""
|
||||||
contributor_data = {
|
contributor_data = {
|
||||||
"name": user_data["name"],
|
"name": user_data["name"],
|
||||||
"github_login": user_data["login"],
|
"github_login": user_data["login"],
|
||||||
"twitter_username": user_data["twitter_username"],
|
"twitter_username": user_data["twitter_username"],
|
||||||
}
|
}
|
||||||
self.content.extend(contributor_data)
|
self.content.append(contributor_data)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
"""Write the file to disk with indentation."""
|
||||||
text_content = json.dumps(self.content, indent=2, ensure_ascii=False)
|
text_content = json.dumps(self.content, indent=2, ensure_ascii=False)
|
||||||
self.file_path.write_text(text_content)
|
self.file_path.write_text(text_content)
|
||||||
|
|
||||||
|
|
||||||
def write_md_file(contributors):
|
def write_md_file(contributors):
|
||||||
|
"""Generate markdown file from Jinja template."""
|
||||||
template = Template(CONTRIBUTORS_TEMPLATE, autoescape=True)
|
template = Template(CONTRIBUTORS_TEMPLATE, autoescape=True)
|
||||||
core_contributors = [c for c in contributors if c.get("is_core", False)]
|
core_contributors = [c for c in contributors if c.get("is_core", False)]
|
||||||
other_contributors = (c for c in contributors if not c.get("is_core", False))
|
other_contributors = (c for c in contributors if not c.get("is_core", False))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user