Skip to content

Commit

Permalink
Implemented a simple method to attach() provenance to the BaseFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperVanDenBosch committed Feb 4, 2015
1 parent 4f9485d commit ca63ad4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
16 changes: 15 additions & 1 deletion niprov/basefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
from niprov.dependencies import Dependencies
from niprov.filesystem import Filesystem
from niprov.hashing import Hasher
from niprov.jsonserializing import JsonSerializer


class BaseFile(object):

def __init__(self, fpath, listener=Commandline(),
filesystem=Filesystem(), hasher=Hasher()):
filesystem=Filesystem(), hasher=Hasher(),
serializer=JsonSerializer()):
self.path = fpath
self.listener = listener
self.filesystem = filesystem
self.hasher = hasher
self.serializer = serializer

def inspect(self):
provenance = {}
Expand All @@ -20,3 +23,14 @@ def inspect(self):
provenance['created'] = self.filesystem.getctime(self.path)
provenance['hash'] = self.hasher.digest(self.path)
return provenance

def attach(self):
"""
Attach the current provenance to the file by saving it encoded
in a small textfile alongside it.
The resulting file's name is like the file it describes,
but with the .provenance extension.
"""
provstr = self.serializer.serialize(self.provenance)
self.filesystem.write(self.path+'.provenance', provstr)
22 changes: 21 additions & 1 deletion tests/basefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ def setUp(self):
self.log = Mock()
self.hasher = Mock()
self.filesys = Mock()
self.json = Mock()
self.path = 'example.abc'
from niprov.basefile import BaseFile
self.file = BaseFile(self.path, listener=self.log,
filesystem=self.filesys, hasher=self.hasher)
filesystem=self.filesys, hasher=self.hasher, serializer=self.json)

def test_Saves_file_path_along_with_provenance(self):
out = self.file.inspect()
Expand All @@ -28,3 +29,22 @@ def test_Saves_file_creation_time_along_with_provenance(self):
def test_Asks_hasher_for_digest_of_file(self):
out = self.file.inspect()
self.assertEqual(out['hash'], self.hasher.digest(self.path))

def test_Attach_writes_provenance_to_a_file(self):
self.file.provenance = {'foo':'bar'}
self.file.attach()
self.json.serialize.assert_called_with(self.file.provenance)
self.filesys.write.assert_called_with(
self.path+'.provenance', self.json.serialize(self.file.provenance))


#ATTACH
# check if we have provenance to attach
# call method for type-specific attachment behavior (file or insert)



#READATTACHED
# call method for type-specific attachment behavior (file or insert)
# return provenance
# save provenance to instance
3 changes: 2 additions & 1 deletion tests/dicomfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def setUp(self):
self.setupPydicom()
from niprov.dcm import DicomFile
self.file = DicomFile(self.path, listener=self.log,
filesystem=self.filesys, hasher=self.hasher, dependencies=self.libs)
filesystem=self.filesys, hasher=self.hasher, dependencies=self.libs,
serializer=self.json)

def test_Gets_basic_info_from_pydicom_and_returns_it(self):
out = self.file.inspect()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_fif.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def setUp(self):
self.setupMne()
from niprov.fif import FifFile
self.file = FifFile(self.path, listener=self.log,
filesystem=self.filesys, hasher=self.hasher, dependencies=self.libs)
filesystem=self.filesys, hasher=self.hasher, dependencies=self.libs,
serializer=self.json)

def test_If_error_during_inspection_tells_listener_and_returns_None(self):
self.libs.mne.io.Raw.side_effect = ValueError
Expand Down
3 changes: 2 additions & 1 deletion tests/test_parrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def setUp(self):
self.setupNibabel()
from niprov.parrec import ParrecFile
self.file = ParrecFile(self.path, listener=self.log,
filesystem=self.filesys, hasher=self.hasher, dependencies=self.libs)
filesystem=self.filesys, hasher=self.hasher, dependencies=self.libs,
serializer=self.json)

def test_If_error_during_inspection_tells_listener_and_returns_None(self):
self.libs.nibabel.load.side_effect = ValueError
Expand Down

0 comments on commit ca63ad4

Please sign in to comment.