1919cwlprov Provenance
2020
2121"""
22- __author__ = "Stian Soiland-Reyes <https://orcid.org/0000-0001-9842-9718>"
23- __copyright__ = "© 2018 Software Freedom Conservancy (SFC)"
24- __license__ = "Apache License, version 2.0 (https://www.apache.org/licenses/LICENSE-2.0)"
22+ __author__ = "Stian Soiland-Reyes <https://orcid.org/0000-0001-9842-9718>"
23+ __copyright__ = "© 2018 Software Freedom Conservancy (SFC)"
24+ __license__ = (
25+ "Apache License, version 2.0 (https://www.apache.org/licenses/LICENSE-2.0)"
26+ )
2527
2628import logging
29+
2730from prov .identifier import Identifier , Namespace
2831from prov .model import *
32+
2933from .utils import *
3034
3135CWLPROV = Namespace ("cwlprov" , "https://w3id.org/cwl/prov#" )
3438
3539MEDIA_TYPES = {
3640 "ttl" : 'text/turtle; charset="UTF-8"' ,
37- "rdf" : ' application/rdf+xml' ,
38- "json" : ' application/json' ,
39- "jsonld" : ' application/ld+json' ,
40- "xml" : ' application/xml' ,
41+ "rdf" : " application/rdf+xml" ,
42+ "json" : " application/json" ,
43+ "jsonld" : " application/ld+json" ,
44+ "xml" : " application/xml" ,
4145 "provn" : 'text/provenance-notation; charset="UTF-8"' ,
42- "nt" : ' application/n-triples' ,
46+ "nt" : " application/n-triples" ,
4347}
44- EXTENSIONS = dict ((v ,k ) for (k ,v ) in MEDIA_TYPES .items ())
48+ EXTENSIONS = dict ((v , k ) for (k , v ) in MEDIA_TYPES .items ())
49+
4550
4651def _as_identifier (uri_or_identifier ):
4752 if not uri_or_identifier :
@@ -51,9 +56,11 @@ def _as_identifier(uri_or_identifier):
5156 else :
5257 return Identifier (str (uri_or_identifier ))
5358
59+
5460def _prov_attr (attr , elem ):
5561 return first (elem .get_attribute (attr ))
5662
63+
5764class Provenance :
5865 def __init__ (self , ro , run = None ):
5966 self .ro = ro
@@ -66,7 +73,6 @@ def __init__(self, ro, run=None):
6673 def __repr__ (self ):
6774 return "Provenance<%s from %s>" % (self .uri , self ._path )
6875
69-
7076 @property
7177 def uri (self ):
7278 return self .run_id .uri
@@ -89,7 +95,7 @@ def activity(self, uri=None):
8995 return Activity (self , activity )
9096
9197 def _prov_format (self , media_type ):
92- for prov in ( self .ro .provenance (self .uri ) or () ):
98+ for prov in self .ro .provenance (self .uri ) or ():
9399 if media_type == self .ro .mediatype (prov ):
94100 return self .ro .resolve_path (prov )
95101
@@ -103,7 +109,9 @@ def _load_prov_document(self):
103109 if prov :
104110 _logger .info ("Loading %s" , prov )
105111 if c in rdf_candidates :
106- doc = ProvDocument .deserialize (source = prov , format = "rdf" , rdf_format = c )
112+ doc = ProvDocument .deserialize (
113+ source = prov , format = "rdf" , rdf_format = c
114+ )
107115 else :
108116 doc = ProvDocument .deserialize (source = prov , format = c )
109117 return doc .unified (), prov
@@ -114,7 +122,7 @@ def record_with_attr(self, prov_type, attrib_value, with_attrib=PROV_ATTR_ACTIVI
114122 for elem in self .prov_doc .get_records (prov_type ):
115123 if (with_attrib , attrib_value ) in elem .attributes :
116124 yield elem
117-
125+
118126
119127class _Prov :
120128 def __init__ (self , provenance , record ):
@@ -145,10 +153,10 @@ def types(self):
145153 def uri (self ):
146154 i = self .id
147155 return i and i .uri
148-
156+
149157 def __repr__ (self ):
150158 return "<%s %s>" % (self .__class__ .__name__ , self .uri )
151-
159+
152160 def __str__ (self ):
153161 return self .record .get_provn ()
154162
@@ -160,7 +168,6 @@ def _prov_attrs(self, attr):
160168
161169
162170class Activity (_Prov ):
163-
164171 def usage (self ):
165172 return self ._records (ProvUsage , Usage , PROV_ATTR_ACTIVITY )
166173
@@ -172,7 +179,7 @@ def association(self):
172179
173180 def plan (self ):
174181 return first (a .plan_id for a in self .association () if a .plan_id )
175-
182+
176183 def steps (self ):
177184 starts = self ._records (ProvStart , Start , PROV_ATTR_STARTER )
178185 for s in starts :
@@ -192,34 +199,41 @@ def duration(self):
192199 s = start and start .time
193200 end = self .end ()
194201 e = end and end .time
195- return s and e and e - s
202+ return s and e and e - s
203+
196204
197205class _Time (_Prov ):
198206 @property
199207 def time (self ):
200208 return self ._prov_attr (PROV_ATTR_TIME )
201209
210+
202211class _Start_or_End (_Time ):
203212 @property
204213 def activity_id (self ):
205214 return self ._prov_attr (PROV_ATTR_ACTIVITY )
215+
206216 def activity (self ):
207217 a = self .activity_id
208218 return a and self .provenance .activity (a )
209219
210220 @property
211221 def starter_id (self ):
212222 return self ._prov_attr (PROV_ATTR_STARTER )
223+
213224 def starter_activity (self ):
214225 a = self .starter_id
215226 return a and self .provenance .activity (a )
216-
227+
217228
218229class Start (_Start_or_End ):
219230 pass
231+
232+
220233class End (_Start_or_End ):
221234 pass
222235
236+
223237class Association (_Prov ):
224238 @property
225239 def agent_id (self ):
@@ -237,6 +251,7 @@ def activity(self):
237251 def plan_id (self ):
238252 return self ._prov_attr (PROV_ATTR_PLAN )
239253
254+
240255class Specialization (_Prov ):
241256 @property
242257 def general_entity_id (self ):
@@ -254,14 +269,18 @@ def specific_entity(self):
254269 s = self .specific_entity_id
255270 return s and self .provenance .entity (s )
256271
257- class Entity (_Prov ):
258272
273+ class Entity (_Prov ):
259274 def specializationOf (self ):
260- specializations = self ._records (ProvSpecialization , Specialization , PROV_ATTR_SPECIFIC_ENTITY )
275+ specializations = self ._records (
276+ ProvSpecialization , Specialization , PROV_ATTR_SPECIFIC_ENTITY
277+ )
261278 return (s .general_entity () for s in specializations )
262279
263280 def generalizationOf (self ):
264- specializations = self ._records (ProvSpecialization , Specialization , PROV_ATTR_GENERAL_ENTITY )
281+ specializations = self ._records (
282+ ProvSpecialization , Specialization , PROV_ATTR_GENERAL_ENTITY
283+ )
265284 return (s .specific_entity () for s in specializations )
266285
267286 @property
@@ -271,9 +290,11 @@ def value(self):
271290 @property
272291 def basename (self ):
273292 return self ._prov_attr (CWLPROV ["basename" ])
293+
274294 @property
275295 def nameroot (self ):
276296 return self ._prov_attr (CWLPROV ["nameroot" ])
297+
277298 @property
278299 def nameext (self ):
279300 return self ._prov_attr (CWLPROV ["nameext" ])
@@ -282,8 +303,12 @@ def derivations(self):
282303 return self ._records (ProvDerivation , Derivation , PROV_ATTR_USED_ENTITY )
283304
284305 def secondary_files (self ):
285- return [d .generated_entity () for d in self .derivations ()
286- if CWLPROV ["SecondaryFile" ] in d .types ()]
306+ return [
307+ d .generated_entity ()
308+ for d in self .derivations ()
309+ if CWLPROV ["SecondaryFile" ] in d .types ()
310+ ]
311+
287312
288313class Derivation (_Prov ):
289314 @property
@@ -305,12 +330,13 @@ def used_entity(self):
305330 @property
306331 def generation_id (self ):
307332 return self ._prov_attr (PROV_ATTR_GENERATION )
333+
308334 @property
309335 def usage_id (self ):
310336 return self ._prov_attr (PROV_ATTR_USAGE )
311337
312- class _Usage_Or_Generation (_Time ):
313338
339+ class _Usage_Or_Generation (_Time ):
314340 @property
315341 def entity_id (self ):
316342 return self ._prov_attr (PROV_ATTR_ENTITY )
@@ -322,11 +348,11 @@ def entity(self):
322348 @property
323349 def role (self ):
324350 return self ._prov_attr (PROV_ROLE )
325-
351+
352+
326353class Generation (_Usage_Or_Generation ):
327354 pass
328355
356+
329357class Usage (_Usage_Or_Generation ):
330358 pass
331-
332-
0 commit comments