-
Notifications
You must be signed in to change notification settings - Fork 9
/
gca-sort
executable file
·79 lines (57 loc) · 2.14 KB
/
gca-sort
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from gca.core import Abstract
from gca.util import make_fields, simplify_str
import argparse
import codecs
import sys
from six import string_types
def sanitize_field(x):
if type(x) == bytes:
return x.strip()
elif isinstance(x, string_types):
""" Why only the first character???
if x[0] in s_conv:
x = s_conv[x[0]] + x[1:]
"""
return simplify_str(x)
elif isinstance(x, (tuple, list)):
x = [sanitize_field(xx) for xx in x]
return x
def get_sorter(field):
if field == '=':
return None
reverse = True
if field.startswith('+'):
reverse = False
field = field[1:]
fields = field
def sort_fn(abstract):
val = abstract.select_field(fields, fold=True)
return sanitize_field(val)
return {'fn': sort_fn, 'reverse': reverse}
def set_sort_id(abstract, sort_id, base, use_altid):
old_sort_id = abstract.sort_id
abstract.sort_id = sort_id + base
if use_altid:
abstract.alt_id = old_sort_id
return abstract
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='GCA Sort - sort list of abstract by fields')
parser.add_argument('file', type=str, default='-')
parser.add_argument('fields', type=str, nargs='+', help='fields to sort for')
parser.add_argument('--assign', type=int, default=None, help='assign a sortID')
parser.add_argument('--altid', default=False, dest='altid', action='store_true')
args = parser.parse_args()
sorter = filter(lambda x: x is not None, [get_sorter(f) for f in args.fields])
fd = codecs.open(args.file, 'r', encoding='utf-8') if args.file != '-' else sys.stdin
abstracts = Abstract.from_data(fd.read())
fd.close()
for s in sorter:
abstracts = sorted(abstracts, key=s['fn'], reverse=s['reverse'])
if args.assign is not None:
abstracts = [set_sort_id(a, idx, args.assign, args.altid) for idx, a in enumerate(abstracts)]
data = Abstract.to_json(abstracts)
sys.stdout.write(data)