Completed
Pull Request — master (#147)
by Jasper
01:33
created

Commandline.unknownFile()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0

1 Method

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