-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_contributors.py
114 lines (83 loc) · 2.69 KB
/
parse_contributors.py
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
## Copyrights : Sushan Shakya
## Use this script to generate snippet for contributors
import json
filename = "contributors.json"
output_filename = "README.md"
readme_filename = "README.md"
title_template = '> {}'
base_template = '''
<table>
{}
</table>
'''
table_row = '''
<tr>
{}
</tr>
'''
user_template = '''
<td align="center">
<a href="https://github.com/{github_username}">
<img src="https://avatars.githubusercontent.com/{github_username}?s=150&v=1" width="100px;" alt="{github_username}"/>
</a>
<br/>
<a href="https://github.com/{github_username}">{name}</a>
</td>
'''
output = ''
def first_capital(title):
return f"{title[0].upper()}{title[1:]}"
def get_rowed_data(data):
result = [[]]
item_in_a_row = 5
cur = 0
filled = False
for i in range(len(data)):
result[cur].append(data[i])
filled = False
if(((i + 1) % item_in_a_row) == 0):
filled = True
if(filled) :
result.append([])
cur= cur + 1
return result
# * Generator
with open(filename) as fp:
data = json.load(fp)
for key in data.keys():
title = first_capital(key)
output += f'{title_template.format(title)}\n'
row_data = ''
tech_stacks = data[key]
rowed_data = get_rowed_data(tech_stacks)
for tech_stacks in rowed_data:
tmp = ''
tmp = f"<tr>{tmp}"
for value in tech_stacks:
tmp += f"{user_template.format(github_username=value['github_username'], name=value['name'])}\n"
tmp = f"{tmp}</tr>"
row_data = f"{row_data}{tmp}"
output += f'{base_template.format(row_data)}\n'
# with open(output_filename, 'w') as fp:
# output = f"<br>\n<!--- Use [parse_contributors.py] to generate this file -->\n\n## Contributors\n<br>\n\n{output}"
# fp.write(output)
# ** Truncate Contributors from README.md
start_template = "<!--- Contributors Start -->"
end_template = "<!--- Contributors End -->"
with open(readme_filename) as fp:
lines = fp.readlines()
start_index = 0
end_index = 0
for i in range(len(lines)):
data = lines[i].strip()
if(data == start_template):
start_index = i
if(data == end_template):
end_index = i
output_lines = output.split("\n")
output_lines = map(lambda x: f"{x}\n", output_lines)
output_lines = ["\n",f"<br>\n<!--- Use [parse_contributors.py] to generate this file -->\n\n## Contributors\n<br>\n\n", * output_lines, "\n"]
new_lines = [*(lines[0:(start_index + 1)]), *output_lines ,*(lines[end_index: ])]
output = "".join(new_lines)
with open(output_filename, 'w') as fp:
fp.write(output)