-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.py
415 lines (392 loc) · 16.8 KB
/
edit.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"""
Edit the authorlist json file.
"""
from __future__ import print_function
import json
import random
import webbrowser
from pprint import pprint
from datetime import datetime
import tornado.ioloop
import tornado.web
from tornado.escape import json_encode, json_decode
from authorlist import collabs
from authorlist.util import author_ordering
def check(data):
for a in data['authors']:
if 'instnames' in a:
for inst in a['instnames']:
if inst not in data['institutions']:
print(a['authname'],inst)
raise Exception('bad instname')
if 'thanks' in a:
for t in a['thanks']:
if t not in data['thanks']:
print(a['authname'],t)
raise Exception('bad thanks')
if 'instnames' not in a and 'thanks' not in a:
print(a['authname'])
raise Exception('no instname or thanks')
def save(outfile, data):
check(data)
data['authors'].sort(key=author_ordering)
if outfile:
with open(outfile, 'w') as f:
json.dump(data, f, indent=2, sort_keys=True)
else:
pprint(data)
class MainHandler(tornado.web.RequestHandler):
def initialize(self, outfile, data):
self.outfile = outfile
self.data = data
def get(self):
collaborations = set()
institutions = set()
for a in self.data['authors']:
if a['to'] == '':
collaborations.add(a['collab'])
if 'instnames' in a:
for i in a['instnames']:
institutions.add(i)
collaborations = {c:collabs[c] for c in collaborations}
institutions = {inst:self.data['institutions'][inst]['cite'] for inst in institutions}
self.write("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Author List Editor</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.default.min.css" />
<style>
footer {
margin-top: 1em;
}
section.action article {
margin: .5em;
}
button#id, button#update {
margin: 1em;
}
article div.text, article div.select {
margin: .5em 0;
display: flex;
align-items: center;
}
article span.label {
margin-right: 0.5em;
}
article div.select div.selectize-control, article select {
width: 100%;
}
section.results section.author {
display: none;
margin: 1em 0;
border-top: 1px solid black;
width: 100%;
}
section.results section.author.active {
display: block;
}
article section.results div.hidden {
display: none;
}
</style>
</head>
<body>
<header><h1>Author List Editor</h1></header>
<main>
<article>
<section class="action">
<button id="add">Add Author</button>
<button id="edit">Edit Author</button>
</section>
<br>
</article>
<footer>
<button id="submit">Reload</button>
</footer>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.min.js"></script>
<script type="text/javascript">
var data = """+json_encode(self.data)+""";
var collaborations = """+json_encode(collaborations)+"""
var institutions = """+json_encode(institutions)+"""
function text_format(id, text, value=''){
return '<div class="text"><span class="label">'+text+':</span><input autocomplete="off" class="'+id+'" type="text" value="'+value+'"></div>';
}
function checkbox_format(id, text, value=false){
let ret = '<div class="text"><span class="label">'+text+':</span><input autocomplete="off" class="'+id+'" type="checkbox" value="true" ';
if (value) {
ret += 'checked ';
}
ret += '></div>';
return ret
}
function date_format(id, text, value=''){
return '<div class="text date"><span class="label">'+text+':</span><input autocomplete="off" class="'+id+'" type="date" value="'+value+'"></div>';
}
function select_format(id, text, options){
var html = '<div class="select"><span class="label">'+text+':</span><select class="'+id+'" multiple=1>';
for(var name in options){
html += '<option value="'+name+'">'+options[name]+'</option>';
}
html += '</select></div>';
return html;
}
function disabled_text_format(id, text, value){
return '<div class="text"><span class="label">'+text+':</span><input autocomplete="off" disabled class="'+id+'" type="text" value="'+value+'"></div>';
}
function disabled_options_format(id, text, options){
var html = '<div class="select"><span class="label">'+text+':</span><select disabled class="'+id+'" multiple=1>';
for(var name in options){
html += '<option selected value="'+name+'">'+options[name]+'</option>';
}
html += '</select></div>';
return html;
}
function hidden_options_format(id, text, options){
var html = '<div class="select hidden"><span class="label">'+text+':</span><select disabled class="'+id+'" multiple=1>';
for(var name in options){
html += '<option selected value="'+name+'">'+options[name]+'</option>';
}
html += '</select></div>';
return html;
}
Array.prototype.equals = function( array ) {
return this.length == array.length &&
this.every( function(this_i,i) { return this_i == array[i] } )
};
$('#add').on('click', function(e) {
var html = '<section class="author">';
html += text_format('name', 'Name');
html += text_format('first', 'First Name');
html += text_format('last', 'Last Name');
html += text_format('email', 'Email');
html += text_format('orcid', 'ORCID');
html += date_format('from', 'From');
html += date_format('to', 'To');
html += checkbox_format('legacy', 'Legacy Author');
html += select_format('collaboration', 'Collaboration', collaborations);
html += select_format('institution', 'Institution', institutions);
html += select_format('thanks', 'Thanks', data['thanks']);
html += '<button id="update">Update</button>';
html += '</section>';
$('article').html(html);
$('select.collaboration').selectize({
plugins: ['remove_button']
});
$('select.institution').selectize({
plugins: ['remove_button']
});
$('select.thanks').selectize({
plugins: ['remove_button']
});
$("#update").on('click', function(e) {
// get data
var collabs = $('select.collaboration').val();
if (collabs == null)
collabs = [];
var insts = $('select.institution').val();
var thanks = $('select.thanks').val();
var author;
var email = $('input.email').val();
var legacy = $('input.legacy:checked').val();
for (var i=0;i<collabs.length;i++){
author = {
'authname': $('input.name').val(),
'first': $('input.first').val(),
'last': $('input.last').val(),
'collab': collabs[i],
'from': $('input.from').val(),
'to': $('input.to').val(),
'orcid': $('input.orcid').val()
}
if (email != null && email != "undefined")
author['email'] = email;
if (insts != null)
author['instnames'] = insts;
if (thanks != null)
author['thanks'] = thanks;
if (legacy != null)
author['legacy'] = true;
data['authors'].push(author);
}
$('#submit').click()
});
$('#submit').hide();
});
$('#edit').on('click', function(e) {
var html = '<section class="search">';
var names = {};
for (var i=0;i<data['authors'].length;i++) {
names[data['authors'][i]['authname']] = data['authors'][i]['authname']
}
html += select_format('names', 'Filter by name', names);
html += date_format('date', 'Filter by date');
html += '</section>';
html += '<section class="results"></section>';
$('article').html(html);
$('select.names').selectize({
plugins: ['remove_button'],
closeAfterSelect: true
});
var filter = function(e){
var html = '';
var names = $('select.names').val();
var date = $('input.date').val();
if (names == null && date == '')
return;
var author = null;
var add_author = function(){
html += '<section class="author';
if ((names == null || names.some(n => n == author['authname'])) &&
(date == '' || (author['from'] <= date && (author['to'] == '' || author['to'] >= date)))) {
html += ' active';
}
html += '">';
html += disabled_text_format('name', 'Name', author['authname']);
html += disabled_text_format('first', 'First Name', author['first']);
html += disabled_text_format('last', 'Last Name', author['last']);
html += disabled_text_format('email', 'Email', author['email']);
html += text_format('orcid', 'ORCID', author['orcid']);
html += date_format('from', 'From', author['from']);
html += date_format('to', 'To', author['to']);
let legacy = false;
if ('legacy' in author && author['legacy']) {
legacy = true;
}
html += checkbox_format('legacy', 'Legacy Author', legacy);
var collabs = {};
for (var j=0;j<author['collab'].length;j++) {
collabs[author['collab'][j]] = collaborations[author['collab'][j]];
}
var insts = {};
if ('instnames' in author) {
for (var j=0;j<author['instnames'].length;j++) {
insts[author['instnames'][j]] = institutions[author['instnames'][j]];
}
}
var thanks = {};
if ('thanks' in author) {
for (var j=0;j<author['thanks'].length;j++) {
thanks[author['thanks'][j]] = thanks[author['thanks'][j]];
}
}
html += disabled_options_format('collaboration', 'Collaboration', collabs);
html += disabled_options_format('institution', 'Institution', insts);
html += hidden_options_format('thanks', 'Thanks', thanks);
html += '</section>';
};
for(var i=0;i<data['authors'].length;i++) {
var a = data['authors'][i];
if (author != null) {
if (a['authname'] == author['authname']
&& (!('instnames' in a ^ 'instnames' in author) || ('instnames' in a && a['instnames'].equals(author['instnames'])))
&& (!('thanks' in a ^ 'thanks' in author) || ('thanks' in a && a['thanks'].equals(author['thanks'])))
&& a['from'] == author['from']
&& a['to'] == author['to']) {
author['collab'].push(a['collab']);
continue;
}
add_author();
}
author = $.extend({}, a);
author['collab'] = [a['collab']]
}
if (author != null) {
add_author();
}
html += '<button id="update">Update</button>';
$('section.results').html(html);
$("#update").on('click', function(e) {
// get data
var authors = [];
$('section.author').each(function(index, el){
var collabs = $(el).find('select.collaboration').val();
if (collabs == null)
collabs = [];
var insts = $(el).find('select.institution').val();
var thanks = $(el).find('select.thanks').val();
var author;
var email = $(el).find('input.email').val();
let legacy = false;
if ($(el).find('input.legacy:checked').val() == ['true']) {
legacy = true;
}
for (var i=0;i<collabs.length;i++){
author = {
'authname': $(el).find('input.name').val(),
'first': $(el).find('input.first').val(),
'last': $(el).find('input.last').val(),
'collab': collabs[i],
'from': $(el).find('input.from').val(),
'to': $(el).find('input.to').val(),
'orcid': $(el).find('input.orcid').val()
}
if (email != null && email != "undefined")
author['email'] = email;
if (insts != null)
author['instnames'] = insts;
if (thanks != null)
author['thanks'] = thanks;
if (legacy === true)
author['legacy'] = legacy;
authors.push(author);
}
});
data['authors'] = authors;
$('#submit').click()
});
};
$('select.names').on('change', filter);
$('input.date').on('change', filter);
$('#submit').hide();
});
$("#submit").on('click', function(e) {
$.ajax({
type: "POST",
url: "/",
data: JSON.stringify({'data':data}),
success: function(){ location.reload(true); },
error: function(xhr, status, e){ alert(status); },
dataType: "json",
contentType : "application/json"
});
});
</script>
</body>
</html>
""")
def post(self):
args = json_decode(self.request.body)
if args['data']:
self.data.update(args['data'])
save(self.outfile, self.data)
self.write({})
def make_app(outfile, data):
kwargs = {'outfile': outfile, 'data': data}
return tornado.web.Application([
(r'/', MainHandler, kwargs),
], debug=True, autoescape=None)
def main():
import argparse
parser = argparse.ArgumentParser(description='Authorlist editor')
parser.add_argument('-i','--input',help='input json file')
parser.add_argument('-o','--output',default=None,help='output json file')
args = parser.parse_args()
with open(args.input) as f:
data = json.load(f)
data['authors'].sort(key=author_ordering)
app = make_app(args.output, data)
while True: # find an unused port we can bind to
port = random.randint(8888,64000)
try:
app.listen(port)
except Exception:
continue
break
webbrowser.open('http://localhost:{}/'.format(port))
tornado.ioloop.IOLoop.current().start()
if __name__ == '__main__':
main()