-
Notifications
You must be signed in to change notification settings - Fork 25
/
script.py
169 lines (139 loc) · 5.78 KB
/
script.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
import os
import sys
import jinja2
from rdflib import ConjunctiveGraph, URIRef
from rdflib.namespace import DCTERMS, RDFS, FOAF
from rdflib.namespace import Namespace
FM = Namespace('https://purl.org/fair-metrics/terms/')
fairGraph = ConjunctiveGraph()
fairGraph.parse('http://purl.org/fair-ontology#', format='trig')
fairTermGraph = ConjunctiveGraph()
fairTermGraph.parse('terms', format='n3')
class FairMetricData():
def __init__(self, id):
self.base = 'https://purl.org/fair-metrics/'
self.id = URIRef(id)
self.assertion = URIRef(id+'#assertion')
# id = id.replace(self.base, '') # HACK -- remove this line before merging commit
self.g = ConjunctiveGraph()
self.g.parse(id, format='trig')
def getID(self):
return self.id
def getShortID(self):
return self.id.replace(self.base, '')
def getAuthors(self):
authors = [o.toPython() for o in self.g.objects(subject=self.assertion, predicate=DCTERMS.author)]
authors.sort()
return ' \\\\ '.join(authors)
def getTitle(self):
return ', '.join([o.toPython() for o in self.g.objects(subject=self.assertion, predicate=RDFS.comment)])
def getShortTitle(self):
return ', '.join([o.toPython() for o in self.g.objects(subject=self.assertion, predicate=DCTERMS.title)])
def getTopicDescription(self):
descs = []
for o in self.g.objects(subject=self.id, predicate=FOAF.primaryTopic):
# o should be fair:A1.1
for o2 in fairGraph.objects(subject=o, predicate=DCTERMS.description):
descs.append(o2.toPython())
return ' '.join(descs)
def getTopicTitle(self):
descs = []
for o in self.g.objects(subject=self.id, predicate=FOAF.primaryTopic):
# o should be fair:A1.1
for o2 in fairGraph.objects(subject=o, predicate=DCTERMS.title):
descs.append(o2.toPython())
return ' '.join(descs)
def getMeasuring(self):
# return fm:measuring
return self.getFMPropertyValue(FM.measuring)
def getRationale(self):
# return fm:rationale
return self.getFMPropertyValue(FM.rationale)
def getRequirements(self):
# return fm:requirements
return self.getFMPropertyValue(FM.requirements)
def getProcedure(self):
# return fm:procedure
return self.getFMPropertyValue(FM.procedure)
def getValidation(self):
# return fm:validation
return self.getFMPropertyValue(FM.validation)
def getRelevance(self):
# return fm:relevance
return self.getFMPropertyValue(FM.relevance)
def getExamples(self):
# return fm:examples
return self.getFMPropertyValue(FM.examples)
def getComments(self):
# return fm:comments
return self.getFMPropertyValue(FM.comments)
def getFMPropertyLabel(self, property):
return ', '.join([ o.toPython() for o in fairTermGraph.objects(subject=FM[property], predicate=RDFS['label'])])
def getFMPropertyValue(self, property):
return ', '.join([o.toPython() for o in self.g.objects(subject=self.assertion, predicate=property)])
if __name__=='__main__':
args = sys.argv
if len(args)!=2:
raise Exception('Expected metric IRI as input')
# The idea is that we could fill the table http://fairmetrics.org/fairmetricform.html
# from a given metric IRI
# id = 'https://purl.org/fair-metrics/FM_A1.1'
metricFile = args[1]
id = 'https://purl.org/fair-metrics/' + metricFile
fm = FairMetricData(id)
latex_jinja_env = jinja2.Environment(
variable_start_string = '\VAR{',
variable_end_string = '}',
trim_blocks = True,
autoescape = False,
loader = jinja2.FileSystemLoader(os.path.abspath('.'))
)
template = latex_jinja_env.get_template('template.tex')
title=fm.getTitle()
authors=fm.getAuthors()
metricId=fm.getShortID().replace('_','-') # Avoid _ in latex template
metricIdVerb=fm.getID()
shortTitle=fm.getShortTitle()
topicTitle=fm.getTopicTitle()
topicDesription=fm.getTopicDescription().replace('\n','\\newline\n')
measuring=fm.getMeasuring().replace('\n','\\newline\n')
rationale=fm.getRationale().replace('\n','\\newline\n')
requirements=fm.getRequirements().replace('\n','\\newline\n')
procedure=fm.getProcedure().replace('\n','\\newline\n')
validation=fm.getValidation().replace('\n','\\newline\n')
relevance=fm.getRelevance().replace('\n','\\newline\n')
examples=fm.getExamples().replace('\n','\\newline\n')
comments=fm.getComments().replace('\n','\\newline\n')
measuringLabel=fm.getFMPropertyLabel('measuring')
rationaleLabel=fm.getFMPropertyLabel('rationale')
requirementsLabel=fm.getFMPropertyLabel('requirements')
procedureLabel=fm.getFMPropertyLabel('procedure')
validationLabel=fm.getFMPropertyLabel('validation')
relevanceLabel=fm.getFMPropertyLabel('relevance')
examplesLabel=fm.getFMPropertyLabel('examples')
commentsLabel=fm.getFMPropertyLabel('comments')
print(template.render(
title=title,
authors=authors,
metricId=metricId,
metricIdVerb=metricIdVerb,
shortTitle=shortTitle,
topicTitle=topicTitle,
topicDesription=topicDesription,
measuring=measuring,
rationale=rationale,
requirements=requirements,
procedure=procedure,
validation=validation,
relevance=relevance,
examples=examples,
comments=comments,
measuringLabel=measuringLabel,
rationaleLabel=rationaleLabel,
requirementsLabel=requirementsLabel,
procedureLabel=procedureLabel,
validationLabel=validationLabel,
relevanceLabel=relevanceLabel,
examplesLabel=examplesLabel,
commentsLabel=commentsLabel,
))