Completed
Push — master ( b66745...9821e6 )
by Jasper
02:18 queued 58s
created

Commandline.fileAdded()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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