Skip to content

Commit

Permalink
File size now in provenance
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperVanDenBosch committed Jan 23, 2015
1 parent b2339c2 commit 7014f88
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
8 changes: 7 additions & 1 deletion niprov/filesystem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os


class Filesystem():
class Filesystem(object):
"""Wrapper of filesystem access functionality such as that implemented by
the os package in the standard library.
"""

def __init__(self):
self.open = open
Expand Down Expand Up @@ -30,3 +33,6 @@ def read(self, path):
contents = fhandle.read()
return contents

def getsize(self, path):
return os.path.getsize(path)

2 changes: 1 addition & 1 deletion niprov/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class HtmlExporter(object):
"""
_footer = '</html>'
_expectedFields = ['acquired','subject','protocol']
_allfields = ['path','ancestor','acquired','subject','protocol','transformation','code','logtext']
_allfields = ['path','ancestor','acquired','subject','protocol','transformation','code','logtext','size']

def __init__(self, filesys, listener, externals):
self.filesys = filesys
Expand Down
5 changes: 4 additions & 1 deletion niprov/inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
from datetime import datetime
from niprov.commandline import Commandline
from niprov.dependencies import Dependencies
from niprov.filesystem import Filesystem

formats = {'.PAR':'nibabel',
'.dcm':'dicom'}


def inspect(fpath, listener=Commandline(), libs=Dependencies()):
def inspect(fpath, listener=Commandline(), libs=Dependencies(),
filesystem=Filesystem()):
provenance = {}
extension = os.path.splitext(fpath)[1]
if not extension in formats:
Expand All @@ -19,6 +21,7 @@ def inspect(fpath, listener=Commandline(), libs=Dependencies()):
listener.missingDependencyForImage(formats[extension], fpath)
return None
provenance['path'] = fpath
provenance['size'] = filesystem.getsize(fpath)
if formats[extension] == 'nibabel':
try:
img = libs.nibabel.load(fpath)
Expand Down
2 changes: 2 additions & 0 deletions tests/htmlexporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ def test_Exporting_a_single_item(self):
item1['acquired'] = datetime(2014, 8, 5, 12, 23, 46)
item1['code'] = 'private static void'
item1['logtext'] = 'Hello World!'
item1['size'] = 45678
html.export(item1)
filehandle.write.assert_any_call('<dt>code</dt><dd>private static void</dd>\n')
filehandle.write.assert_any_call('<dt>logtext</dt><dd>Hello World!</dd>\n')
filehandle.write.assert_any_call('<dt>size</dt><dd>45678</dd>\n')

def setupFilesys(self):
filesys = Mock()
Expand Down
36 changes: 27 additions & 9 deletions tests/inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ def test_Loads_file_with_nibabel(self):
log = Mock()
libs = self.setupNibabel()
libs.hasDependency.return_value = True
niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs)
filesys = Mock()
niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs, filesystem=filesys)
libs.nibabel.load.assert_any_call('/p/f1.PAR')

def test_Doesnt_use_nibabel_if_not_installed(self):
import niprov.inspection
log = Mock()
libs = self.setupNibabel()
libs.hasDependency.return_value = False
provenance = niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs)
filesys = Mock()
provenance = niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs, filesystem=filesys)
self.assertRaises(AssertionError,
libs.nibabel.load.assert_any_call, '/p/f1.PAR')

Expand All @@ -26,7 +28,8 @@ def test_If_dcm_passed_uses_pydicom_to_open(self):
log = Mock()
libs = self.setupPydicom()
libs.hasDependency.return_value = True
provenance = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs)
filesys = Mock()
provenance = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs, filesystem=filesys)
# doesnt complain about missing pydicom
self.assertRaises(AssertionError,
log.missingDependencyForImage.assert_any_call, 'dicom','/p/f1.dcm')
Expand All @@ -38,7 +41,8 @@ def test_If_dcm_passed_but_pydicom_not_installed_tells_listener(self):
log = Mock()
libs = self.setupPydicom()
libs.hasDependency.return_value = False
provenance = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs)
filesys = Mock()
provenance = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs, filesystem=filesys)
self.assertRaises(AssertionError,
libs.dicom.read_file.assert_any_call, '/p/f1.dcm')
log.missingDependencyForImage.assert_any_call('dicom','/p/f1.dcm')
Expand All @@ -48,15 +52,17 @@ def test_If_nothing_inspected_returns_None(self):
log = Mock()
libs = self.setupNibabel()
libs.hasDependency.return_value = False
provenance = niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs)
filesys = Mock()
provenance = niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs, filesystem=filesys)
self.assertIsNone(provenance)

def test_Gets_basic_info_from_nibabel_and_returns_it(self):
import niprov.inspection
log = Mock()
libs = self.setupNibabel()
libs.hasDependency.return_value = True
out = niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs)
filesys = Mock()
out = niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs, filesystem=filesys)
self.assertEqual(out['subject'], 'John Doeish')
self.assertEqual(out['protocol'], 'T1 SENSE')
self.assertEqual(out['acquired'], datetime(2014, 8, 5, 11, 27, 34))
Expand All @@ -66,7 +72,8 @@ def test_Gets_basic_info_from_pydicom_and_returns_it(self):
log = Mock()
libs = self.setupPydicom()
libs.hasDependency.return_value = True
out = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs)
filesys = Mock()
out = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs, filesystem=filesys)
self.assertEqual(out['subject'], 'John Doeish')
self.assertEqual(out['protocol'], 'T1 SENSE')
self.assertEqual(out['acquired'], datetime(2014, 8, 5, 12, 19, 14))
Expand All @@ -76,16 +83,27 @@ def test_Saves_file_path_along_with_provenance(self):
log = Mock()
libs = self.setupPydicom()
libs.hasDependency.return_value = True
out = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs)
filesys = Mock()
out = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs, filesystem=filesys)
self.assertEqual(out['path'], '/p/f1.dcm')

def test_Saves_filesize_along_with_provenance(self):
import niprov.inspection
log = Mock()
libs = self.setupPydicom()
libs.hasDependency.return_value = True
filesys = Mock()
out = niprov.inspection.inspect('/p/f1.dcm', listener=log, libs=libs, filesystem=filesys)
self.assertEqual(out['size'], filesys.getsize('/p/f1.dcm'))

def test_If_error_during_inspection_tells_listener_and_returns_None(self):
import niprov.inspection
log = Mock()
libs = Mock()
libs.hasDependency.return_value = True
libs.nibabel.load.side_effect = ValueError
out = niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs)
filesys = Mock()
out = niprov.inspection.inspect('/p/f1.PAR', listener=log, libs=libs, filesystem=filesys)
self.assertIsNone(out)
log.fileError.assert_any_call('/p/f1.PAR')

Expand Down

0 comments on commit 7014f88

Please sign in to comment.