<commit_before>#!/usr/bin/python
# -*- coding: UTF-8 -*-
from __future__ import print_function
from niprov.exceptions import UnknownFileError
from niprov.dependencies import Dependencies


class Commandline(object):

    vlevels = ['debug','info','warning','error']

    def __init__(self, dependencies=Dependencies()):
        self.verbosity = dependencies.config.verbosity
        assert self.verbosity in self.vlevels, "Unknown verbosity value"

    def fileFound(self, image):
        template = '[provenance] {0}'
        print(template.format(image.path))

    def fileFoundInSeries(self, img, series):
        template = '[provenance] Adding {0} file to series: {1}'
        nfiles = len(series.provenance['filesInSeries'])
        print(template.format(ordinal(nfiles), series.getSeriesId()))

    def missingDependencyForImage(self, lib, fpath):
        template = '[provenance] Missing python package "{0}" to read file: {1}'        
        print(template.format(lib, fpath))

    def fileError(self, fpath):
        import traceback
        traceback.print_exc()
        print('[provenance] Error inspecting file: {0}'.format(fpath))

    def interpretedRecording(self, new, transform, parents):
        template = ('[provenance] Recorded the command [{1}] to create [{0}] '+
            'based on [{2}]')
        print(template.format(', '.join(new), transform, ', '.join(parents)))

    def unknownFile(self, fpath):
        raise UnknownFileError('Unknown file: '+fpath)

    def knownFile(self, fpath):
        print('[provenance] File already known: '+fpath)

    def renamedDicom(self, fpath):
        print('[provenance] Renamed dicom file: '+fpath)

    def discoveryFinished(self, nnew, nadded, nfailed, ntotal):
        print('[provenance] Discovered {0} new, added {1} to series, failed to read {2}, '
           'processed {3} total files.'.format(nnew, nadded, nfailed, ntotal))

    def mnefunEventReceived(self, operationName):
        print('[provenance] Mnefun operation: '+operationName)

    def receivedBashCommand(self, command):
        print('[provenance] Recording command: \n'+(' '.join(command)))

    def filesMarkedForApproval(self, images):
        print('[provenance] Files marked for approval:')
        for img in images:
            print(img.path)

    def log(self, level, message):
        if self.vlevels.index(level) >= self.vlevels.index(self.verbosity):
            print('[provenance:{0}] {1}'.format(level, message))
        
        

SUFFIXES = {1: 'st', 2: 'nd', 3: 'rd'}
def ordinal(num):
    if 10 <= num % 100 <= 20:
        suffix = 'th'
    else:
        # the second parameter is a default.
        suffix = SUFFIXES.get(num % 10, 'th')
    return str(num) + suffix
<commit_msg>Existing Commandline methods call the new log() helper method instead of print().<commit_after>#!/usr/bin/python
# -*- coding: UTF-8 -*-
from __future__ import print_function
from niprov.exceptions import UnknownFileError
from niprov.dependencies import Dependencies


class Commandline(object):

    vlevels = ['debug','info','warning','error']

    def __init__(self, dependencies=Dependencies()):
        self.verbosity = dependencies.config.verbosity
        assert self.verbosity in self.vlevels, "Unknown verbosity value"

    def fileFound(self, image):
        self.log('info', 'New file: {0}'.format(image.path))

    def fileFoundInSeries(self, img, series):
        template = 'Adding {0} file to series: {1}'
        nfiles = len(series.provenance['filesInSeries'])
        self.log('info', template.format(ordinal(nfiles), series.getSeriesId()))

    def missingDependencyForImage(self, lib, fpath):
        template = 'Missing python package "{0}" to read file: {1}'        
        self.log('warning', template.format(lib, fpath))

    def fileError(self, fpath):
        import traceback
        traceback.print_exc()
        self.log('warning', 'Error inspecting file: {0}'.format(fpath))

    def interpretedRecording(self, new, transform, parents):
        template = ('[provenance] Recorded the command [{1}] to create [{0}] '+
            'based on [{2}]')
        self.log('info', template.format(', '.join(new), transform, 
            ', '.join(parents)))

    def unknownFile(self, fpath):
        raise UnknownFileError('Unknown file: '+fpath)

    def knownFile(self, fpath):
        self.log('info', 'File already known: '+fpath)

    def renamedDicom(self, fpath):
        self.log('info', 'Renamed dicom file: '+fpath)

    def discoveryFinished(self, nnew, nadded, nfailed, ntotal):
        self.log('info', 'Discovered {0} new, added {1} to series, failed to read {2}, '
           'processed {3} total files.'.format(nnew, nadded, nfailed, ntotal))

    def mnefunEventReceived(self, operationName):
        self.log('info', 'Mnefun operation: '+operationName)

    def receivedBashCommand(self, command):
        self.log('info', 'Recording command: \n'+(' '.join(command)))

    def filesMarkedForApproval(self, images):
        paths = '\n'.join([img.path for img in images])
        self.log('info', 'Files marked for approval: \n{0}'.format(paths))

    def log(self, level, message):
        if self.vlevels.index(level) >= self.vlevels.index(self.verbosity):
            print('[provenance:{0}] {1}'.format(level, message))
        

SUFFIXES = {1: 'st', 2: 'nd', 3: 'rd'}
def ordinal(num):
    if 10 <= num % 100 <= 20:
        suffix = 'th'
    else:
        # the second parameter is a default.
        suffix = SUFFIXES.get(num % 10, 'th')
    return str(num) + suffix

{'fraction_non_alphanumeric': 0.14350018005041412, 'fraction_numerical': 0.01026287360460929, 'mean_word_length': 3.2179195140470767, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5134609', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1660, 'n_words': 450}