Completed
Push — master ( 6d6a7d...e4617d )
by Jasper
8s
created

get()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
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(provenance, medium, form, pipeline=False, dependencies=Dependencies()):
7
    """Publish or simply return provenance for selected files.
8
9
    To get provenance on one specific file, pass its path as the 'forFile' 
10
    argument. Alternatively, to get all files associated with a certain subject,
11
    use the 'forSubject' argument. If none of these is used, provenance for the 
12
    most recently registered files is reported.
13
14
    Args:
15
        provenance: Niprov BaseFile object or list of such.
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
25
    Returns:
26
        Depends on medium selected. 
27
    """
28
    formatFactory = dependencies.getFormatFactory()
29
    mediumFactory = dependencies.getMediumFactory()
30
    makePipeline = dependencies.getPipelineFactory()
31
32
    form = formatFactory.create(form)
33
    medium = mediumFactory.create(medium)
34
    
35
    if pipeline:
36
        provenance = makePipeline.forFile(provenance)
37
38
    formattedProvenance = form.serialize(provenance)
39
    return medium.export(formattedProvenance, form)
40
41
def print_(provenance, pipeline=False, dependencies=Dependencies()):
42
    """Shortcut for export(medium='stdout', form='simple').
43
    """
44
    return export(provenance, medium='stdout', form='simple',
45
                  pipeline=pipeline, dependencies=dependencies)
46
47
def backup(dependencies=Dependencies()):
48
    """Shortcut for export(medium='file', form='json') for all provenance.
49
    """
50
    provenance = dependencies.getQuery().all()
51
    return export(provenance, medium='file', form='json', 
52
                  dependencies=dependencies)
53
54
def view(provenance, pipeline=False, dependencies=Dependencies()):
55
    """Shortcut for export(medium='viewer', form='picture').
56
    """
57
    return export(provenance, medium='viewer', form='picture', 
58
        pipeline=pipeline, dependencies=dependencies)
59
60