-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.py
279 lines (215 loc) · 8.87 KB
/
application.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from __future__ import print_function
from flask import Flask, render_template, request, redirect, url_for
import json
import pygraphviz as pgv
import re
from lxml import etree
#replace StringIO
import io
#from io import BytesIO
#from io import StringIO
import urllib
#import urllib2
import urllib.request
import urllib3
import string
import sys
import os
import os.path
import natsort
from collections import OrderedDict
app = Flask(__name__)
os.environ['PATH'] = os.environ['PATH'] + ':/usr/local/bin'
os.environ['GV_FILE_PATH'] = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static/images/')) + '/'
print('PATH: ' + os.environ['PATH'], file=sys.stderr)
print('GV_FILE_PATH: ' + os.environ['GV_FILE_PATH'], file=sys.stderr)
def get_people():
filepath = os.path.abspath(os.path.join(os.path.dirname(__file__), 'people.json'))
return get_file_contents(filepath)
# thanks to Colin Morris for adding this code originally
def get_skills_list():
skills_list = {}
json_results = get_people()
for supervisor, data in json_results.items():
for section in data:
for item in json_results[str(supervisor)][section]:
if item not in skills_list:
skills_list[item] = 1;
else:
skills_list[item] = skills_list[item] +1;
skills_list_new = OrderedDict(natsort.natsorted(skills_list.items()))
return skills_list_new
def get_file_contents(filename):
data = None
try:
fp = open(filename, 'rb')
try:
contents = fp.read()
data = json.loads(contents)
finally:
fp.close()
except IOError:
print('Could not open JSON file:' + filename, file=sys.stderr)
sys.exit(1)
return data
def get_titles(topic):
url = 'https://en.wikipedia.org/w/api.php'
values = {
'action' : 'query',
'list' : 'search',
'srwhat' : 'text',
'srsearch' : topic.encode('utf8'),
'format' : 'json',
'srlimit' : '40'
}
## Big urllib change for Python 3 below...
#data = urllib.urlencode(values) # Python 2
data = urllib.parse.urlencode(values)
data = data.encode('utf-8') # necessary for python 3
request = urllib.request.Request(url, data)
#response = urllib2.urlopen(request) # Python 3 version below
response = urllib.request.urlopen(request)
json_response = response.read()
json_result = json.loads(json_response)
try:
results = []
for result in json_result['query']['search']:
results.append(result['title'])
except:
results = []
print("ERROR: no title found for topic: " + topic.encode('utf8'))
return results
def get_graph_string(graph):
#output = StringIO.StringIO()
output = io.BytesIO()
graph.draw(output, format = 'svg')
svg = output.getvalue()
output.close()
svg_parser = etree.XMLParser()
svg_obj = etree.fromstring(svg, svg_parser)
svg_obj.attrib['width'] = '100%'
del svg_obj.attrib['height']
images = svg_obj.findall('.//{http://www.w3.org/2000/svg}image')
# Convert all image href links to match server
# For example, anonymous.png becomes static/images/anonymous.png
# Have to do this because graphviz doesn't allow you to specify the
# URL to an image, only the file path.
for image in images:
image_filename = image.attrib['{http://www.w3.org/1999/xlink}href']
image_url = url_for('static', filename = 'images/' + image_filename)
image.attrib['{http://www.w3.org/1999/xlink}href'] = image_url
return etree.tostring(svg_obj, pretty_print = True).decode('utf-8')
def get_image_files():
image_files = []
image_dir = os.environ['GV_FILE_PATH']
for root, sub_folders, files in os.walk(image_dir):
for filename in files:
actual_file_name = os.path.join(root, filename)
if filename.endswith('.png'):
image_files.append(filename)
return image_files
def build_graph(name, results, topics):
graph = pgv.AGraph(overlap = 'false', name = name)
people = get_people()
for person in results:
forename, surname = person.lower().split()
image_file = 'anonymous.png'
image_files = get_image_files()
for filename in image_files:
#newstr = starturlsource[index+len(pattern):index+len(pattern)+17]
if str.find(filename, surname) != -1 and str.find(filename, forename) != -1:
image_file = '%s' % (filename)
# check added for _ in name e.g. Anja Le_Blanc; that is: convert _ to space
myperson= person.replace(' ', '\n')
graph.add_node(person, label = myperson.replace('_' , ' '), fontname = 'Helvetica', fixedsize = True, imagescale = True, width = '1.5', height = '1.5', fontcolor = 'white', shape = 'circle', style = 'filled', color = '#303030', URL = url_for('show_person', name = person), image = image_file)
interests = people[person]['interests']
for interest in interests:
if interest in topics:
color = '#A02020FF'
shape = 'ellipse'
else:
color = '#105060EE'
shape = 'ellipse'
label = re.sub('\(.*\)', '', interest)
graph.add_node(interest, label = label, style = 'filled', fontname = 'Helvetica', shape = shape, color = color, fontcolor = 'white', URL = url_for('show_topic', name = interest))
graph.add_edge(person, interest, color = '#00000050')
if 'technologies' in people[person]:
for technology in people[person]['technologies']:
if technology in topics:
color = '#B01050FF'
shape = 'ellipse'
else:
color = '#701050EE'
shape = 'ellipse'
label = re.sub('\(.*\)', '', technology)
graph.add_node(technology, label = label, style = 'filled', fontname = 'Helvetica', shape = shape, color = color, fontcolor = 'white')
graph.add_edge(person, technology, color = '#00000050')
graph.layout(prog = 'neato')
return graph
@app.route('/')
def index():
graph_name = 'UoM Research Software Engineers and their skills'
results = set()
topics = []
people = get_people()
for person in people:
results.add(person)
graph = build_graph(graph_name, results, topics)
graph_str = get_graph_string(graph)
interests_links = get_interests_links()
return render_template('graph.html', name=graph_name, node_count=len(graph.nodes()), graph=graph_str, interests=interests_links)
def get_interests_links():
interests_skills = get_skills_list()
interests_links = ""
index_link = url_for('index')
for skill, count in interests_skills.items():
interests_links += '<a href="' + index_link + 'topic/' + str(skill) + '" title="' + str(count) + ' records">' + str(skill) + '</a><br>'
return interests_links
@app.route('/person/<name>')
@app.route('/person/')
@app.route('/person')
def show_person(name=None):
if not name:
interests_links = get_interests_links()
return render_template('notfound.html', interests=interests_links)
graph_name = name.replace('_', ' ') + "'s skills"
results = set()
topics = []
results.add(name)
graph = build_graph(graph_name, results, topics)
if graph is False:
pname=name
interests_links = get_interests_links()
return render_template('notfound.html', search_term=pname, interests=interests_links)
graph_str = get_graph_string(graph)
return render_template('graph.html', name=graph_name, node_count=len(graph.nodes()), graph=graph_str)
@app.route('/topic/<name>')
def show_topic(name):
main_topic = name
graph_name = 'UoM RSEs and their skills related to ' + name
results = set()
topics = get_titles(name)
people = get_people()
interests_links = get_interests_links()
for person in people:
for topic in topics:
if topic in people[person]['interests']:
results.add(person)
if 'technologies' in people[person]:
for topic in topics:
if topic in people[person]['technologies']:
results.add(person)
graph = build_graph(graph_name, results, topics)
graph_str = get_graph_string(graph)
return render_template('graph.html', name=graph_name, node_count=len(graph.nodes()), graph=graph_str, interests=interests_links)
# 2019-04-08 | New : Try to handle empty search
@app.route('/topic/')
def show_topic_notfound():
interests_links = get_interests_links()
return render_template('notfound.html', interests=interests_links)
@app.route('/topic-search')
def topic_search():
topic = request.args.get('topic')
return redirect(url_for('show_topic', name = topic))
if __name__ == "__main__":
app.run(debug = True, host='0.0.0.0')