Completed
Push — master ( 6d0d49...6d6a7d )
by Jasper
02:25 queued 57s
created

view()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
1
#!/usr/bin/python
2
# -*- coding: UTF-8 -*-
3
from niprov.dependencies import Dependencies
4
5
6
def export(medium, form, forFile=None, forSubject=None, 
7
        statistics=False, pipeline=False, dependencies=Dependencies()):
8
    """Publish or simply return provenance for selected files.
9
10
    To get provenance on one specific file, pass its path as the 'forFile' 
11
    argument. Alternatively, to get all files associated with a certain subject,
12
    use the 'forSubject' argument. If none of these is used, provenance for the 
13
    most recently registered files is reported.
14
15
    Args:
16
        medium (str): The medium in which to publish the provenance. 
17
            One of:
18
                'stdout'  (print the provenance to the terminal), 
19
                'direct'  (return object to caller), 
20
                'file'    (write to a text file),
21
                'viewer'  (open in the system image viewer).
22
        form (str): The format in which to serialize the provenance. 
23
            One of 'json','xml','narrated','simple','dict','picture'.
24
        forFile (str): Select one file based on this path.
25
        forSubject (str): Select files regarding this subject.
26
        statistics (bool): Print overall statistics.
27
28
    Returns:
29
        Depends on medium selected. 
30
    """
31
    formatFactory = dependencies.getFormatFactory()
32
    mediumFactory = dependencies.getMediumFactory()
33
    repository = dependencies.getRepository()
34
    listener = dependencies.getListener()
35
    location = dependencies.getLocationFactory()
36
    makePipeline = dependencies.getPipelineFactory()
37
38
    form = formatFactory.create(form)
39
    medium = mediumFactory.create(medium)
40
    
41
    if statistics:
42
        provenance = repository.statistics()
43
    elif forFile:
44
        forFile = location.completeString(forFile)
45
        if not repository.knowsByLocation(forFile):
46
            listener.unknownFile(forFile)
47
            return
48
        provenance = repository.byLocation(forFile)
49
        if pipeline:
50
            provenance = makePipeline.forFile(provenance)
51
    elif forSubject:
52
        provenance = repository.bySubject(forSubject)
53
    else:
54
        provenance = repository.latest()
55
56
    formattedProvenance = form.serialize(provenance)
57
    return medium.export(formattedProvenance, form)
58
59
60
def get(forFile=None, forSubject=None, statistics=False, pipeline=False, 
61
    dependencies=Dependencies()):
62
    """Shortcut for export(medium='direct', form='object').
63
    """
64
    return export(medium='direct', form='object', 
65
        forFile=forFile, forSubject=forSubject, statistics=statistics, 
66
        pipeline=pipeline, dependencies=dependencies)
67
68
def print_(forFile=None, forSubject=None, statistics=False, pipeline=False, 
69
    dependencies=Dependencies()):
70
    """Shortcut for export(medium='stdout', form='simple').
71
    """
72
    return export(medium='stdout', form='simple', 
73
        forFile=forFile, forSubject=forSubject, statistics=statistics, 
74
        pipeline=pipeline, dependencies=dependencies)
75
76
def backup(dependencies=Dependencies()):
77
    """Shortcut for export(medium='file', form='json') for all provenance.
78
    """
79
    return export(medium='file', form='json', dependencies=dependencies)
80
81
def view(forFile=None, forSubject=None, statistics=False, pipeline=False, 
82
    dependencies=Dependencies()):
83
    """Shortcut for export(medium='viewer', form='picture').
84
    """
85
    return export(medium='viewer', form='picture', 
86
        forFile=forFile, forSubject=forSubject, statistics=statistics, 
87
        pipeline=pipeline, dependencies=dependencies)
88
89