Completed
Pull Request — master (#127)
by Jasper
01:07
created

export()   B

Complexity

Conditions 6

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 6
dl 0
loc 49
rs 7.6949
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 'stdout' (print the provenance to the terminal), 'direct' 
18
            (return object to caller), or 'file' (write to a text file).
19
        form (str): The format in which to serialize the provenance. 
20
            One of 'json','xml','narrated','simple','dict'.
21
        forFile (str): Select one file based on this path.
22
        forSubject (str): Select files regarding this subject.
23
        statistics (bool): Print overall statistics.
24
25
    Returns:
26
        Depends on medium selected. 
27
    """
28
    formatFactory = dependencies.getFormatFactory()
29
    mediumFactory = dependencies.getMediumFactory()
30
    repository = dependencies.getRepository()
31
    listener = dependencies.getListener()
32
    location = dependencies.getLocationFactory()
33
    makePipeline = dependencies.getPipelineFactory()
34
35
    form = formatFactory.create(form)
36
    medium = mediumFactory.create(medium)
37
    
38
    if statistics:
39
        provenance = repository.statistics()
40
    elif forFile:
41
        forFile = location.completeString(forFile)
42
        if not repository.knowsByLocation(forFile):
43
            listener.unknownFile(forFile)
44
            return
45
        provenance = repository.byLocation(forFile)
46
        if pipeline:
47
            provenance = makePipeline.forFile(provenance)
48
    elif forSubject:
49
        provenance = repository.bySubject(forSubject)
50
    else:
51
        provenance = repository.latest()
52
53
    formattedProvenance = form.serialize(provenance)
54
    return medium.export(formattedProvenance, form)
55
56
57
def get(forFile=None, forSubject=None, statistics=False, pipeline=False, 
58
    dependencies=Dependencies()):
59
    """Shortcut for export(medium='direct', form='object').
60
    """
61
    return export(medium='direct', form='object', 
62
        forFile=forFile, forSubject=forSubject, statistics=statistics, 
63
        pipeline=pipeline, dependencies=dependencies)
64
65
def print_(forFile=None, forSubject=None, statistics=False, pipeline=False, 
66
    dependencies=Dependencies()):
67
    """Shortcut for export(medium='stdout', form='simple').
68
    """
69
    return export(medium='stdout', form='simple', 
70
        forFile=forFile, forSubject=forSubject, statistics=statistics, 
71
        pipeline=pipeline, dependencies=dependencies)
72
73
def backup(dependencies=Dependencies()):
74
    """Shortcut for export(medium='file', form='json') for all provenance.
75
    """
76
    return export(medium='file', form='json', dependencies=dependencies)
77
78