-
Notifications
You must be signed in to change notification settings - Fork 9
/
gca-lint
executable file
·90 lines (67 loc) · 2.95 KB
/
gca-lint
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from collections import defaultdict
from gca.core import Abstract
import argparse
import codecs
import sys
class Issue(object):
def __init__(self, level, message, abstract):
self.level = level
self.message = message
self.abstract = abstract
def __str__(self):
return '[%s] %s {%s}' % (self.level, self.message, self.abstract.uuid)
def __repr__(self):
return str(self)
def check_abstract(abstract, args):
if abstract.title is None or not len(abstract.title):
yield Issue('Error', 'Abstract has no title', abstract)
if not len(abstract.authors):
yield Issue('Error', 'Abstract has no authors', abstract)
if not len(abstract.affiliations):
yield Issue('Error', 'Abstract has no affiliations', abstract)
for author in abstract.authors:
if len(author.affiliations) < 1:
aid = author.last_name if author.last_name else author.uuid
yield Issue('Error', 'Author "%s" has no affiliation' % aid, abstract)
nrefs = len(abstract.references)
if nrefs > 10:
yield Issue('Warning', 'Abstract has %d references' % nrefs, abstract)
nfigures = len(abstract.figures)
if nfigures > 1:
yield Issue('Warning', 'Abstract has %d figures' % nfigures, abstract)
check_log = not args.ignore_log
if check_log and len(abstract.log) < 1:
yield Issue('Warning', 'Abstract has no state log', abstract)
check_owners = not args.ignore_owners
if check_owners and len(abstract.owners) < 1:
yield Issue('Error', 'Abstract has no owner', abstract)
if abstract.sort_id < 1:
yield Issue('Error', 'Abstract has no sort id', abstract)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='GCA Linter')
parser.add_argument('file', type=str, default='-')
parser.add_argument('--ignore-log', action='store_true', default=False)
parser.add_argument('--ignore-owners', action='store_true', default=False)
args = parser.parse_args()
fd = codecs.open(args.file, 'r', encoding='utf-8') if args.file != '-' else sys.stdin
abstracts = Abstract.from_data(fd.read())
abstract_map = {abstract.uuid: abstract for abstract in abstracts}
issues = [(abstract.uuid, list(check_abstract(abstract, args))) for abstract in abstracts]
issues = filter(lambda x: len(x[1]), issues)
d = defaultdict(list)
for i in issues:
d[i[0]] += i[1]
for k, v in d.items():
abstract = abstract_map[k]
author = abstract.authors[0].last_name if len(abstract.authors) else ''
title = abstract.title if abstract.title is not None else ''
print('%s' % k)
print(' %.10s - %.60s...' % (author, title))
for i in v:
print(' [%s] %s' % (i.level, i.message))
print('')
print('%d abstract with issues.' % len(d))