|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- coding: UTF-8 -*- |
|
3
|
|
|
from __future__ import print_function |
|
4
|
|
|
from niprov.exceptions import UnknownFileError |
|
5
|
|
|
from niprov.dependencies import Dependencies |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class Commandline(object): |
|
9
|
|
|
|
|
10
|
|
|
vlevels = ['debug','info','warning','error'] |
|
11
|
|
|
|
|
12
|
|
|
def __init__(self, dependencies=Dependencies()): |
|
13
|
|
|
self.config = dependencies.config |
|
14
|
|
|
self.verbosity = dependencies.config.verbosity |
|
15
|
|
|
assert self.verbosity in self.vlevels, "Unknown verbosity value" |
|
16
|
|
|
|
|
17
|
|
|
def fileFound(self, image): |
|
18
|
|
|
self.log('info', 'New file: {0}'.format(image.path)) |
|
19
|
|
|
|
|
20
|
|
|
def fileFoundInSeries(self, img, series): |
|
21
|
|
|
template = 'Adding {0} file to series: {1}' |
|
22
|
|
|
nfiles = len(series.provenance['filesInSeries']) |
|
23
|
|
|
self.log('info', template.format(ordinal(nfiles), series.getSeriesId())) |
|
24
|
|
|
|
|
25
|
|
|
def missingDependencyForImage(self, lib, fpath): |
|
26
|
|
|
template = 'Missing python package "{0}" to read file: {1}' |
|
27
|
|
|
self.log('warning', template.format(lib, fpath)) |
|
28
|
|
|
|
|
29
|
|
|
def fileError(self, fpath): |
|
30
|
|
|
import traceback |
|
31
|
|
|
traceback.print_exc() |
|
32
|
|
|
self.log('warning', 'Error inspecting file: {0}'.format(fpath)) |
|
33
|
|
|
|
|
34
|
|
|
def interpretedRecording(self, new, transform, parents): |
|
35
|
|
|
template = ('[provenance] Recorded the command [{1}] to create [{0}] '+ |
|
36
|
|
|
'based on [{2}]') |
|
37
|
|
|
self.log('info', template.format(', '.join(new), transform, |
|
38
|
|
|
', '.join(parents))) |
|
39
|
|
|
|
|
40
|
|
|
def knownFile(self, fpath): |
|
41
|
|
|
self.log('info', 'File already known: '+fpath) |
|
42
|
|
|
|
|
43
|
|
|
def renamedDicom(self, fpath): |
|
44
|
|
|
self.log('info', 'Renamed dicom file: '+fpath) |
|
45
|
|
|
|
|
46
|
|
|
def discoveryFinished(self, nnew, nadded, nfailed, ntotal): |
|
47
|
|
|
self.log('info', 'Discovered {0} new, added {1} to series, failed to read {2}, ' |
|
48
|
|
|
'processed {3} total files.'.format(nnew, nadded, nfailed, ntotal)) |
|
49
|
|
|
|
|
50
|
|
|
def mnefunEventReceived(self, operationName): |
|
51
|
|
|
self.log('info', 'Mnefun operation: '+operationName) |
|
52
|
|
|
|
|
53
|
|
|
def receivedBashCommand(self, command): |
|
54
|
|
|
self.log('info', 'Recording command: \n'+(' '.join(command))) |
|
55
|
|
|
|
|
56
|
|
|
def filesMarkedForApproval(self, images): |
|
57
|
|
|
paths = '\n'.join([img.path for img in images]) |
|
58
|
|
|
self.log('info', 'Files marked for approval: \n{0}'.format(paths)) |
|
59
|
|
|
|
|
60
|
|
|
def exportedToFile(self, fname): |
|
61
|
|
|
self.log('info', 'Exported to file: {0}'.format(fname)) |
|
62
|
|
|
|
|
63
|
|
|
def log(self, level, message, exceptionClass=None): |
|
64
|
|
|
if self.vlevels.index(level) >= self.vlevels.index(self.verbosity): |
|
65
|
|
|
if level == 'error': |
|
66
|
|
|
if exceptionClass is None: |
|
67
|
|
|
raise NiprovError(message) |
|
68
|
|
|
else: |
|
69
|
|
|
raise exceptionClass(message) |
|
70
|
|
|
else: |
|
71
|
|
|
print('[provenance:{0}] {1}'.format(level, message)) |
|
72
|
|
|
|
|
73
|
|
|
def addUnknownParent(self, fpath): |
|
74
|
|
|
self.log('warning', '{0} unknown. Adding to provenance'.format(fpath)) |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
SUFFIXES = {1: 'st', 2: 'nd', 3: 'rd'} |
|
78
|
|
|
def ordinal(num): |
|
79
|
|
|
if 10 <= num % 100 <= 20: |
|
80
|
|
|
suffix = 'th' |
|
81
|
|
|
else: |
|
82
|
|
|
# the second parameter is a default. |
|
83
|
|
|
suffix = SUFFIXES.get(num % 10, 'th') |
|
84
|
|
|
return str(num) + suffix |
|
85
|
|
|
|
|
86
|
|
|
|