27
27
from socket import getfqdn
28
28
from getpass import getuser
29
29
from typing import (Any , Callable , Dict , IO , List , Optional , MutableMapping ,
30
- Set , Tuple , cast )
30
+ Set , Tuple , Union , cast )
31
31
from typing_extensions import Text , TYPE_CHECKING # pylint: disable=unused-import
32
32
# move to a regular typing import when Python 3.3-3.6 is no longer supported
33
33
import six
@@ -519,6 +519,7 @@ def declare_file(self, value):
519
519
if 'checksum' in value :
520
520
csum = value ['checksum' ]
521
521
(method , checksum ) = csum .split ("$" , 1 )
522
+ assert checksum
522
523
if method == SHA1 and \
523
524
self .research_object .has_data_file (checksum ):
524
525
entity = self .document .entity ("data:" + checksum )
@@ -540,8 +541,7 @@ def declare_file(self, value):
540
541
541
542
if not entity and 'content' in value :
542
543
# Anonymous file, add content as string
543
- entity = self .declare_artefact (value ["content" ])
544
- checksum = None # TODO
544
+ entity ,checksum = self .declare_string (value ["content" ])
545
545
546
546
# By here one of them should have worked!
547
547
if not entity :
@@ -579,6 +579,7 @@ def declare_file(self, value):
579
579
other_attributes = {PROV ["type" ]: CWLPROV ["SecondaryFile" ]})
580
580
581
581
assert entity
582
+ assert checksum
582
583
return file_entity , entity , checksum
583
584
584
585
def declare_directory (self , value ):
@@ -688,6 +689,20 @@ def declare_directory(self, value):
688
689
self .research_object .add_uri (coll .identifier .uri )
689
690
return coll
690
691
692
+ def declare_string (self , value ):
693
+ # type: (Union[Text, str]) -> Tuple[ProvEntity,str]
694
+
695
+ # Save as string in UTF-8
696
+ byte_s = io .BytesIO (str (value ).encode (ENCODING ))
697
+ data_file = self .research_object .add_data_file (byte_s , content_type = TEXT_PLAIN )
698
+ checksum = posixpath .basename (data_file )
699
+ # FIXME: Don't naively assume add_data_file uses hash in filename!
700
+ data_id = "data:%s" % posixpath .split (data_file )[1 ]
701
+ entity = self .document .entity (data_id ,
702
+ {provM .PROV_TYPE : WFPROV ["Artifact" ],
703
+ provM .PROV_VALUE : str (value )})
704
+ return entity , checksum
705
+
691
706
def declare_artefact (self , value ):
692
707
# type: (Any) -> ProvEntity
693
708
'''
@@ -711,14 +726,8 @@ def declare_artefact(self, value):
711
726
return e
712
727
713
728
elif isinstance (value , (Text , str )):
714
- # Save as string in UTF-8
715
- byte_s = io .BytesIO (str (value ).encode (ENCODING ))
716
- data_file = self .research_object .add_data_file (byte_s , content_type = TEXT_PLAIN )
717
- # FIXME: Don't naively assume add_data_file uses hash in filename!
718
- data_id = "data:%s" % posixpath .split (data_file )[1 ]
719
- return self .document .entity (data_id ,
720
- {provM .PROV_TYPE : WFPROV ["Artifact" ],
721
- provM .PROV_VALUE : str (value )})
729
+ (entity ,_ ) = self .declare_string (value )
730
+ return entity
722
731
723
732
elif isinstance (value , bytes ):
724
733
# If we got here then we must be in Python 3
@@ -840,7 +849,7 @@ def used_artefacts(self,
840
849
datetime .datetime .now (), None , {"prov:role" : prov_role })
841
850
842
851
def generate_output_prov (self ,
843
- final_output , # type: Optional[ Dict[Text, Any] ]
852
+ final_output , # type: Dict[Text, Any]
844
853
process_run_id , # type: Optional[str]
845
854
name # type: Optional[Text]
846
855
): # type: (...) -> None
0 commit comments