-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_images.py
More file actions
201 lines (170 loc) · 7.09 KB
/
Copy pathgenerate_images.py
File metadata and controls
201 lines (170 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python3
import asyncio
import os
import re
import aiohttp
from github_stats import Stats
################################################################################
# Helper Functions
################################################################################
def generate_output_folder() -> None:
"""
Create the output folder if it does not already exist
"""
if not os.path.isdir("generated"):
os.mkdir("generated")
################################################################################
# Individual Image Generation Functions
################################################################################
async def generate_overview(s: Stats) -> None:
"""
Generate an SVG badge with summary statistics
:param s: Represents user's GitHub statistics
"""
try:
print("Starting generation of overview.svg...")
with open("templates/overview.svg", "r") as f:
output = f.read()
print("Fetching statistics data...")
output = re.sub("{{ name }}", await s.name, output)
output = re.sub("{{ stars }}", f"{await s.stargazers:,}", output)
output = re.sub("{{ forks }}", f"{await s.forks:,}", output)
output = re.sub("{{ contributions }}", f"{await s.total_contributions:,}", output)
output = re.sub("{{ views }}", f"{await s.views:,}", output)
output = re.sub("{{ repos }}", f"{len(await s.repos):,}", output)
commits = await s.total_commits
output = re.sub("{{ commits }}", f"{commits:,}", output)
output = re.sub("{{ prs }}", f"{await s.prs:,}", output)
output = re.sub("{{ issues }}", f"{await s.issues:,}", output)
generate_output_folder()
output_path = "generated/overview.svg"
with open(output_path, "w", encoding="utf-8") as f:
f.write(output)
# Verify file was created and has content
if not os.path.exists(output_path):
raise FileNotFoundError(f"Failed to create {output_path}")
file_size = os.path.getsize(output_path)
if file_size == 0:
raise ValueError(f"Generated {output_path} is empty!")
print(f"Successfully generated overview.svg ({file_size} bytes)")
except Exception as e:
print(f"ERROR generating overview.svg: {e}")
import traceback
traceback.print_exc()
raise
async def generate_languages(s: Stats) -> None:
"""
Generate an SVG badge with summary languages used
:param s: Represents user's GitHub statistics
"""
try:
print("Starting generation of languages.svg...")
with open("templates/languages.svg", "r") as f:
output = f.read()
print("Fetching languages data...")
languages = await s.languages
print(f"Found {len(languages)} languages")
if not languages:
print("WARNING: No languages found! Generating empty languages.svg")
progress = ""
lang_list = ""
else:
progress = ""
lang_list = ""
sorted_languages = sorted(
languages.items(), reverse=True, key=lambda t: t[1].get("size")
)
delay_between = 150
for i, (lang, data) in enumerate(sorted_languages):
color = data.get("color")
color = color if color is not None else "#000000"
progress += (
f'<span style="background-color: {color};'
f'width: {data.get("prop", 0):0.3f}%;" '
f'class="progress-item"></span>'
)
lang_list += f"""
<li style="animation-delay: {i * delay_between}ms;">
<svg xmlns="http://www.w3.org/2000/svg" class="octicon" style="fill:{color};"
viewBox="0 0 16 16" version="1.1" width="16" height="16"><path
fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8z"></path></svg>
<span class="lang">{lang}</span>
<span class="percent">{data.get("prop", 0):0.2f}%</span>
</li>
"""
output = re.sub(r"{{ progress }}", progress, output)
output = re.sub(r"{{ lang_list }}", lang_list, output)
generate_output_folder()
output_path = "generated/languages.svg"
with open(output_path, "w", encoding="utf-8") as f:
f.write(output)
# Verify file was created and has content
if not os.path.exists(output_path):
raise FileNotFoundError(f"Failed to create {output_path}")
file_size = os.path.getsize(output_path)
if file_size == 0:
raise ValueError(f"Generated {output_path} is empty!")
print(f"Successfully generated languages.svg ({file_size} bytes)")
except Exception as e:
print(f"ERROR generating languages.svg: {e}")
import traceback
traceback.print_exc()
raise
################################################################################
# Main Function
################################################################################
async def main() -> None:
"""
Generate all badges
"""
access_token = os.getenv("ACCESS_TOKEN")
if not access_token:
# access_token = os.getenv("GITHUB_TOKEN")
raise Exception("A personal access token is required to proceed!")
user = os.getenv("GITHUB_ACTOR")
if user is None:
raise RuntimeError("Environment variable GITHUB_ACTOR must be set.")
exclude_repos = os.getenv("EXCLUDED")
excluded_repos = (
{x.strip() for x in exclude_repos.split(",")} if exclude_repos else None
)
exclude_langs = os.getenv("EXCLUDED_LANGS")
excluded_langs = (
{x.strip() for x in exclude_langs.split(",")} if exclude_langs else None
)
# Convert a truthy value to a Boolean
raw_ignore_forked_repos = os.getenv("EXCLUDE_FORKED_REPOS")
ignore_forked_repos = (
not not raw_ignore_forked_repos
and raw_ignore_forked_repos.strip().lower() != "false"
)
emails = os.getenv("GIT_EMAILS")
email_list = (
list({x.strip() for x in emails.split(",")}) if emails else None
)
async with aiohttp.ClientSession() as session:
s = Stats(
user,
access_token,
session,
exclude_repos=excluded_repos,
exclude_langs=excluded_langs,
ignore_forked_repos=ignore_forked_repos,
emails=email_list,
)
try:
# Pre-fetch stats to ensure data is loaded before generating images
print("Pre-fetching repository statistics...")
await s.get_stats()
print(f"Stats loaded: {len(await s.repos)} repos, {len(await s.languages)} languages")
# Generate both images (stats already loaded, so parallel is safe now)
await generate_overview(s)
await generate_languages(s)
print("All images generated successfully!")
except Exception as e:
print(f"FATAL ERROR during image generation: {e}")
import traceback
traceback.print_exc()
raise
if __name__ == "__main__":
asyncio.run(main())