Пример #1
0
def importp(filepath, dependencies=Dependencies()):
    """Add provenance in bulk from a file, such as saved by export().

    Named importp as opposed to import because the latter is a reserved word
    in Python.

    This can serve as a backup, migration tool, or for exchange.
    """
    repository = dependencies.getRepository()
    importDeps = Dependencies()
    importDeps.getConfiguration().database_url = filepath
    importRepo = JsonFile(importDeps)
    allFiles = importRepo.all()
    for pfile in allFiles:
        repository.add(pfile)
Пример #2
0
 def __init__(self, dependencies=Dependencies()):
     self.filesys = dependencies.getFilesystem()
     self.json = dependencies.getSerializer()
     self.factory = dependencies.getFileFactory()
     self.pictureCache = dependencies.getPictureCache()
     url = dependencies.getConfiguration().database_url
     self.datafile = os.path.expanduser(url)
Пример #3
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    #    settings = {}
    #    settings['reload_all'] = True
    #    settings['debug_all'] = True
    #    settings['mako.directories'] = os.path.join(here, 'templates')
    config = Configurator(settings=settings)
    config.include('pyramid_mako')
    config.add_static_view('static', 'static', cache_max_age=10)
    config.add_static_view('snapshots',
                           os.path.expanduser('~/.niprov-snapshots'),
                           cache_max_age=10)
    config.add_route('home', '/')
    config.add_route('latest', '/latest')
    config.add_route('stats', '/stats')
    config.add_route('short', '/id/{id}')
    config.add_route('pipeline', '/id/{id}/pipeline')
    config.add_route('location', '/locations/{host}*path')
    config.add_route('modality', '/modalities/{modality}')
    config.add_route('subject', '/subjects/{subject}')
    config.add_route('project', '/projects/{project}')
    config.add_route('user', '/users/{user}')
    config.add_route('search', '/search')
    config.add_route('modalities', '/modalities')
    config.add_route('projects', '/projects')
    config.add_route('users', '/users')
    config.add_request_method(lambda r: Dependencies(),
                              'dependencies',
                              reify=True)
    config.scan()
    return config.make_wsgi_app()
Пример #4
0
 def test_reconfigureOrGetConfiguration_with_new_config_returns_it(self):
     from niprov.dependencies import Dependencies, Configuration
     dependencies = Dependencies()
     newSettings = Configuration()
     newSettings.verbose = True
     outConfig = dependencies.reconfigureOrGetConfiguration(newSettings)
     self.assertEqual(newSettings, outConfig)
Пример #5
0
def export(provenance, medium, form, pipeline=False, dependencies=Dependencies()):
    """Publish or simply return provenance for selected files.

    To get provenance on one specific file, pass its path as the 'forFile' 
    argument. Alternatively, to get all files associated with a certain subject,
    use the 'forSubject' argument. If none of these is used, provenance for the 
    most recently registered files is reported.

    Args:
        provenance: Niprov BaseFile object or list of such.
        medium (str): The medium in which to publish the provenance. 
            One of:
                'stdout'  (print the provenance to the terminal), 
                'direct'  (return object to caller), 
                'file'    (write to a text file),
                'viewer'  (open in the system image viewer).
        form (str): The format in which to serialize the provenance. 
            One of 'json','xml','narrated','simple','dict','picture'.

    Returns:
        Depends on medium selected. 
    """
    formatFactory = dependencies.getFormatFactory()
    mediumFactory = dependencies.getMediumFactory()
    makePipeline = dependencies.getPipelineFactory()

    form = formatFactory.create(form)
    medium = mediumFactory.create(medium)
    
    if pipeline:
        provenance = makePipeline.forFile(provenance)

    formattedProvenance = form.serialize(provenance)
    return medium.export(formattedProvenance, form)
Пример #6
0
def discover(root, dependencies=Dependencies()):
    """
    Search a directory for image files, and add them to your provenance collection.

    Files are only included if they match the filters in the 
    'discover_file_extensions' settings.
    Refer to niprov.add for details on what happens to individual files.

    Args:
        root (str): The top directory in which to look for new files.
    """
    filesys = dependencies.getFilesystem()
    filefilter = dependencies.getFileFilter()
    listener = dependencies.getListener()

    dirs = filesys.walk(root)
    stats = {
        'total': 0,
        'new': 0,
        'series-new-file': 0,
        'failed': 0,
        'new-version': 0
    }
    for (root, sdirs, files) in dirs:
        for filename in files:
            filepath = os.path.join(root, filename)
            if filefilter.include(filename):
                stats['total'] = stats['total'] + 1
                p = add(filepath, transient=False, dependencies=dependencies)
                stats[p.status] = stats[p.status] + 1
    listener.discoveryFinished(nnew=stats['new'],
                               nadded=stats['series-new-file'],
                               nfailed=stats['failed'],
                               ntotal=stats['total'])
Пример #7
0
 def test_reconfigureOrGetConfiguration_with_None_doesnt_affect_config(
         self):
     from niprov.dependencies import Dependencies
     dependencies = Dependencies()
     self.assertEqual(dependencies.getListener().verbosity, 'info')
     dependencies.reconfigureOrGetConfiguration(None)
     self.assertEqual(dependencies.getListener().verbosity, 'info')
Пример #8
0
def markedForApproval(dependencies=Dependencies()):
    """List files marked for approval by a human.
    """
    query = dependencies.getQuery()
    listener = dependencies.getListener()
    markedFiles = query.byApproval('pending')
    listener.filesMarkedForApproval(markedFiles)
    return markedFiles
Пример #9
0
 def test_reconfigureOrGetConfiguration_creates_new_dependencies(self):
     from niprov.dependencies import Dependencies, Configuration
     dependencies = Dependencies()
     self.assertEqual(dependencies.getListener().verbosity, 'info')
     newSettings = Configuration()
     newSettings.verbosity = 'warning'
     dependencies.reconfigureOrGetConfiguration(newSettings)
     self.assertEqual(dependencies.getListener().verbosity, 'warning')
Пример #10
0
def approve(filepath, dependencies=Dependencies()):
    """Mark this file as approved.

    Args:
        filepath (str): Path to the tracked file that has been found valid.
    """
    loc = dependencies.getLocationFactory().completeString(filepath)
    repository = dependencies.getRepository()
    repository.updateApproval(loc, 'granted')
Пример #11
0
def search(text, dependencies=Dependencies()):
    """
    Search for files with the given text in their provenance fields.

    Args:
        text (str): Words to look for.

    Returns:
        list: List of BaseFile objects
    """
    return dependencies.getRepository().search(text)
Пример #12
0
 def test_Changing_storage_setting_changes_repository_provided(self):
     from niprov.dependencies import Dependencies
     from niprov.jsonfile import JsonFile
     from niprov.mongo import MongoRepository
     dependencies = Dependencies()
     with patch('niprov.mongo.pymongo'):
         dependencies.config.database_type = 'file'
         self.assertIsInstance(dependencies.getRepository(), JsonFile)
         dependencies.config.database_type = 'MongoDB'
         self.assertIsInstance(dependencies.getRepository(),
                               MongoRepository)
Пример #13
0
def renameDicoms(dicomdir, dependencies=Dependencies()):
    """ Add the .dcm extension to any non-hidden files without extension.

        Args:
            dicomdir (str): Directory in which to rename files.
    """
    listener = dependencies.getListener()

    for f in glob.glob(os.path.join(dicomdir, '*')):
        if '.' not in os.path.basename(f):
            listener.renamedDicom(f)
            shutil.move(f, f + '.dcm')
Пример #14
0
def selectApproved(files, dependencies=Dependencies()):
    """Return only files that have approval status 'granted'.

    Args:
        files (list): List of paths of files to check for approval status.
    """
    repository = dependencies.getRepository()
    location = dependencies.getLocationFactory()
    selection = []
    for filepath in files:
        locationString = location.completeString(filepath)
        img = repository.byLocation(locationString)
        if img.provenance.get('approval') == 'granted':
            selection.append(img.path)
    return selection
Пример #15
0
def markForApproval(files, reset=False, dependencies=Dependencies()):
    """Mark a list of files for approval by a human.

    Args:
        files (list): List of paths of files tracked by niprov to mark for
            approval.
        reset (bool): Also mark files that have already been approved. False 
            by default.
    """
    repository = dependencies.getRepository()
    location = dependencies.getLocationFactory()
    for filepath in files:
        loc = location.completeString(filepath)
        if not reset:
            image = repository.byLocation(loc)
            if image is None:
                raise ValueError('Unknown file: ' + filepath)
            if image.provenance.get('approval') == 'granted':
                continue
        repository.updateApproval(loc, 'pending')
Пример #16
0
 def test_provides_LocationFactory(self):
     from niprov.dependencies import Dependencies
     from niprov.locationfactory import LocationFactory
     dependencies = Dependencies()
     self.assertIsInstance(dependencies.getLocationFactory(),
                           LocationFactory)
Пример #17
0
 def test_Filefactory_provided(self):
     from niprov.dependencies import Dependencies
     from niprov.files import FileFactory
     dependencies = Dependencies()
     self.assertIsInstance(dependencies.getFileFactory(), FileFactory)
Пример #18
0
 def test_provides_PipelineFactory(self):
     from niprov.dependencies import Dependencies
     from niprov.pipelinefactory import PipelineFactory
     dependencies = Dependencies()
     self.assertIsInstance(dependencies.getPipelineFactory(),
                           PipelineFactory)
Пример #19
0
 def __init__(self):
     self.config = Configuration()
     self.deps = Dependencies(self.config)
Пример #20
0
 def test_provides_FormatFactory(self):
     from niprov.dependencies import Dependencies
     from niprov.formatfactory import FormatFactory
     dependencies = Dependencies()
     self.assertIsInstance(dependencies.getFormatFactory(), FormatFactory)
Пример #21
0
 def test_Changing_setting_directly_affects_component_setting(self):
     from niprov.dependencies import Dependencies
     dependencies = Dependencies()
     self.assertEqual(dependencies.getListener().verbosity, 'info')
     dependencies.config.verbosity = 'warning'
     self.assertEqual(dependencies.getListener().verbosity, 'warning')
Пример #22
0
 def __init__(self, dependencies=Dependencies()):
     self.dependencies = dependencies
Пример #23
0
 def test_provides_MediumFactory(self):
     from niprov.dependencies import Dependencies
     from niprov.mediumfactory import MediumFactory
     dependencies = Dependencies()
     self.assertIsInstance(dependencies.getMediumFactory(), MediumFactory)
Пример #24
0
def print_(provenance, pipeline=False, dependencies=Dependencies()):
    """Shortcut for export(medium='stdout', form='simple').
    """
    return export(provenance, medium='stdout', form='simple',
                  pipeline=pipeline, dependencies=dependencies)
Пример #25
0
 def __init__(self, dependencies=Dependencies()):
     self.libs = dependencies.getLibraries()
     self.listener = dependencies.getListener()
     self.dependencies = dependencies
Пример #26
0
 def test_provides_Externals(self):
     from niprov.dependencies import Dependencies
     from niprov.externals import Externals
     dependencies = Dependencies()
     self.assertIsInstance(dependencies.getExternals(), Externals)
Пример #27
0
def backup(dependencies=Dependencies()):
    """Shortcut for export(medium='file', form='json') for all provenance.
    """
    provenance = dependencies.getQuery().all()
    return export(provenance, medium='file', form='json', 
                  dependencies=dependencies)
Пример #28
0
 def __init__(self, dependencies=Dependencies()):
     self.files = dependencies.getRepository()
Пример #29
0
def view(provenance, pipeline=False, dependencies=Dependencies()):
    """Shortcut for export(medium='viewer', form='picture').
    """
    return export(provenance, medium='viewer', form='picture', 
        pipeline=pipeline, dependencies=dependencies)
Пример #30
0
 def __init__(self, dependencies=Dependencies()):
     self.fileExtension = 'txt'