-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract-list-of-usernames.py
executable file
·99 lines (81 loc) · 2.77 KB
/
extract-list-of-usernames.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
#!/usr/bin/env python
import subprocess
import pathlib
import collections
import re
import shutil
import pprint
import colorsys
import math
import sys
import mistune
USERNAME_ALIASES = {
'kpc21': 'kpc',
'a44ce20e03': 'cytP450',
'Keijo': 'Keij0',
'cytP540': 'cytP450',
'gazowany_smalec': 'kszmigiel',
'Martti25': 'marti'
}
def hsv2rgb(h, s, v):
return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h, s, v))
def optimal_color(i, n):
hue = (1 / n) * i
light = 1.0
magic = 0.618033988749895
magic = 0.3
hue = math.fmod(i * magic, 1.0)
light = math.sqrt(1.0 - math.fmod(i * magic, 0.5))
ret = hsv2rgb(hue, 1, light)
return '"#' + "".join([hex(x).split("x")[1].zfill(2) for x in ret]) + '"'
WIKI_URL = "https://github.com/hakierspejs/wiki.wiki.git"
def process_child(child, title, projects_per_user):
if "text" not in child:
if "children" not in child:
return
for child in child["children"]:
process_child(child, title, projects_per_user)
else:
for user in re.findall("@\\w+", child["text"]):
if str(title) != 'None':
username = user[1:]
username = USERNAME_ALIASES.get(username, username)
projects_per_user[username].add(title)
def process(markdown, projects_per_user, fname):
with open(fname) as f:
parsed = markdown(f.read())
ftitle = "/".join(
[x[:-3] if x.endswith(".md") else x for x in fname.parts[1:]]
)
title = None
for entry in parsed:
if entry["type"] == "heading":
try:
title = f"{ftitle}#{entry['children'][0].get('text', '')}"
except IndexError:
continue
else:
process_child(entry, title, projects_per_user)
def main():
subprocess.check_call(["git", "clone", WIKI_URL])
projects_per_user = collections.defaultdict(set)
markdown = mistune.create_markdown(renderer=mistune.AstRenderer())
for fname in pathlib.Path("wiki.wiki").glob("**/*.md"):
if fname.name.startswith('Spotkania::'):
continue
process(markdown, projects_per_user, fname)
num_users = len(projects_per_user)
colors_per_user = {}
for n, user in enumerate(projects_per_user):
colors_per_user[user] = optimal_color(n, num_users)
dot = "graph { rankdir=LR; splines=ortho; ranksep=2; nodesep=0.1;\n"
for k, v in projects_per_user.items():
for u in v:
dot += f'"{k}"--"{u}" [color={colors_per_user[k]}]\n'
dot += "}"
rendered = subprocess.check_output(["dot", "-Tsvg"], input=dot.encode())
with open("media-w-wiki/kto-co-kontroluje.svg", "wb") as f:
f.write(rendered)
shutil.rmtree("wiki.wiki")
if __name__ == "__main__":
main()