-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
101 lines (90 loc) · 3.81 KB
/
generate.py
File metadata and controls
101 lines (90 loc) · 3.81 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
import os
from dataclasses import dataclass
from functools import reduce
from github import Github, Auth
from dotenv import load_dotenv
from jinja2 import Environment, FileSystemLoader
@dataclass
class Project:
name: str
url: str | None
logo: str | None
description: str | None
class Generate:
def __init__(self) -> None:
load_dotenv()
self.__AUTH_TOKEN: str = Auth.Token(os.getenv('GITHUB_TOKEN'))
self.__LOGO_FILE: str = os.getenv('LOGO_FILE')
self.__ORGANISATION_NAME: str = os.getenv('ORGANISATION_NAME')
self.__MAX_DESCRIPTION_LINE_LENGTH: str = 20
self.__HTML_BREAK: str = '<br />'
self.__COLOURS: list[str] = ['1a535c', '4ecdc4', 'f7fff7', 'ff6b6b', 'ffe66d']
def getProjects(self) -> list[Project]:
github: Github = Github(auth=self.__AUTH_TOKEN)
projects: list[Project] = []
for repo in sorted(
github.get_organization(self.__ORGANISATION_NAME).get_repos(),
key=lambda repo: repo.name.lower(),
):
try:
logo: str = None
if 'project' in repo.get_topics():
if any(
file.path == self.__LOGO_FILE for file in repo.get_contents('')
):
with open(
os.path.join(os.getenv('LOGO_DIR'), f"{repo.name}.png"),
'wb',
) as file:
file.write(
repo.get_contents(self.__LOGO_FILE).decoded_content
)
logo = f"{repo.name}.png"
projects.append(
Project(
repo.name.replace('-', ' '),
repo.html_url if not repo.private else None,
logo,
' '.join(
reduce(
lambda acc, word: (
acc.extend([self.__HTML_BREAK, word])
if len(
acc[
(
len(acc)
- acc[::-1].index(self.__HTML_BREAK)
- 1
if self.__HTML_BREAK in acc
else 0
) : len(acc)
]
)
+ len(word)
+ 1
> self.__MAX_DESCRIPTION_LINE_LENGTH
else acc.append(word)
)
or acc,
repo.description.split(),
[],
)
),
)
)
except Exception as e:
print(f'An error occurred for repository {repo.name}: {e}')
github.close()
return projects
def index(self):
env: Environment = Environment(
loader=FileSystemLoader('.'), trim_blocks=True, lstrip_blocks=True
)
with open('index.html', 'w') as index:
index.write(
env.get_template('index.jinja').render(
projects=self.getProjects(), colours=self.__COLOURS
)
)
if __name__ == '__main__':
Generate().index()