Completed
Push — master ( 9821e6...398a65 )
by Jasper
8s
created

Commandline.usingCopyAsParent()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
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 usingCopyAsParent(self, copy):
18
        loc = str(copy.location)
19
        self.log('warning', 'Used provenance from copy found at '+loc)
20
21
    def fileAdded(self, image):
22
        if image.status == 'new':
23
            self.log('info', 'New file: {0}'.format(image.path))
24
        if image.status == 'series-new-file':
25
            template = 'Added {0} file to series: {1}'
26
            nfiles = len(image.provenance['filesInSeries'])
27
            self.log('info', template.format(ordinal(nfiles), image.getSeriesId()))
28
        if image.status == 'new-version':
29
            self.log('info', 'Added new version for: {}'.format(image.path))
30
31
    def missingDependencyForImage(self, lib, fpath):
32
        template = 'Missing python package "{0}" to read file: {1}'        
33
        self.log('warning', template.format(lib, fpath))
34
35
    def fileError(self, fpath):
36
        import traceback
37
        traceback.print_exc()
38
        self.log('warning', 'Error inspecting file: {0}'.format(fpath))
39
40
    def interpretedRecording(self, new, transform, parents):
41
        template = ('[provenance] Recorded the command [{1}] to create [{0}] '+
42
            'based on [{2}]')
43
        self.log('info', template.format(', '.join(new), transform, 
44
            ', '.join(parents)))
45
46
    def renamedDicom(self, fpath):
47
        self.log('info', 'Renamed dicom file: '+fpath)
48
49
    def discoveryFinished(self, nnew, nadded, nfailed, ntotal):
50
        self.log('info', 'Discovered {0} new, added {1} to series, failed to read {2}, '
51
           'processed {3} total files.'.format(nnew, nadded, nfailed, ntotal))
52
53
    def mnefunEventReceived(self, operationName):
54
        self.log('info', 'Mnefun operation: '+operationName)
55
56
    def receivedBashCommand(self, command):
57
        self.log('info', 'Recording command: \n'+(' '.join(command)))
58
59
    def filesMarkedForApproval(self, images):
60
        paths = '\n'.join([img.path for img in images])
61
        self.log('info', 'Files marked for approval: \n{0}'.format(paths))
62
63
    def exportedToFile(self, fname):
64
        self.log('info', 'Exported to file: {0}'.format(fname))
65
66
    def log(self, level, message, exceptionClass=None):
67
        if self.vlevels.index(level) >= self.vlevels.index(self.verbosity):
68
            if level == 'error':
69
                if exceptionClass is None:
70
                    raise NiprovError(message)
71
                else:
72
                    raise exceptionClass(message)
73
            else:
74
                print('[provenance:{0}] {1}'.format(level, message))
75
76
    def addUnknownParent(self, fpath):
77
        self.log('warning', '{0} unknown. Adding to provenance'.format(fpath))
78
        
79
80
SUFFIXES = {1: 'st', 2: 'nd', 3: 'rd'}
81
def ordinal(num):
82
    if 10 <= num % 100 <= 20:
83
        suffix = 'th'
84
    else:
85
        # the second parameter is a default.
86
        suffix = SUFFIXES.get(num % 10, 'th')
87
    return str(num) + suffix
88
89