forked from astropy/astropy.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getteam.py
122 lines (98 loc) · 3.99 KB
/
getteam.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
115
116
117
118
119
120
121
122
"""
A command line script that updates "The team" in the ``about.html`` file to
reflect the current ``credits.rst`` file from the astropy repository.
Note that this first looks for the ``ASTROPY_REPO_PATH`` environment
variable to try to find a local copy of the astropy repo.
"""
from __future__ import print_function
def get_astropy_credits(warner=print):
"""
Looks for the ``credits.rst`` file in the astropy repo and returns it, or
returns False if the repo can't be found.
"""
import os
from urllib2 import urlopen
creditspath = os.environ.get('ASTROPY_REPO_PATH', 'http://raw.github.com/astropy/astropy/main/docs/credits.rst')
if creditspath.startswith('http'):
#url - download page from web
u = None
try:
u = urlopen(creditspath)
return u.read()
except Exception as e:
warner('Could not download credits.rst from requested path: "{0}" Using placeholder for "The Team" page.'.format(e))
return False
finally:
if u is not None:
u.close()
else:
if not os.path.isfile(creditspath):
warner('Credits.rst file at "{0}" is not a file! Using placeholder for "The Team" page.'.format(creditspath))
return False
with open(creditspath) as f:
return f.read()
def extract_names_list(docs, sectionname, warner=print):
from docutils import nodes
from docutils.core import publish_doctree
if not isinstance(docs, nodes.document):
docs = publish_doctree(docs)
assert isinstance(docs, nodes.document)
foundsections = []
for c in docs.children:
titleidx = c.first_child_matching_class(nodes.title)
if titleidx is not None:
title = str(c.children[titleidx].children[0])
if title == sectionname:
section = c
break
else:
foundsections.append(title)
else:
warner("No section found with name {0}. Sections are:{1!s}".format(sectionname, foundsections))
return None
listidx = section.first_child_matching_class(nodes.bullet_list)
litems = section.children[listidx].children
names = []
for litem in litems:
names.append(''.join(litem.traverse(lambda n: isinstance(n, nodes.Text))))
return names
def process_html(fn, newcoordinators, newcontributors, indent='\t\t\t'):
"""
Returns a string of html mean to look like the input, but with content from
the credits file.
"""
lines = []
incoord = incontrib = False
with open(fn) as fr:
for l in fr:
if l.endswith('\n'):
l = l[:-1] # strip newline
if incoord:
if '</ul>' in l:
lines.extend([(indent + '<li>' + c + '</li>') for c in newcoordinators])
lines.append(l)
incoord = False
#skip otherwise
elif incontrib:
if '</ul>' in l:
lines.extend([(indent + '<li>' + c + '</li>') for c in newcontributors])
lines.append(l)
incontrib = False
#skip otherwise
else:
if '<ul class="coordinators">' in l:
incoord = True
elif '<ul class="team">' in l:
incontrib = True
lines.append(l)
return '\n'.join(lines)
if __name__ == '__main__':
from docutils.core import publish_doctree
dt = publish_doctree(get_astropy_credits())
coordinators = extract_names_list(dt, 'Astropy Project Coordinators')
contributors = extract_names_list(dt, 'Core Package Contributors')
newhtml = process_html('about.html', coordinators, contributors)
print('Replacing "about.html" with updated version. Be sure to "git diff '
'about.html" before committing to ensure no funny business happened.')
with open('about.html', 'w') as f:
f.write(newhtml.encode('UTF-8'))