Completed
Push — master ( b66745...9821e6 )
by Jasper
02:18 queued 58s
created

markForApproval()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 20
rs 8.5454
c 0
b 0
f 0
1
#!/usr/bin/python
2
# -*- coding: UTF-8 -*-
3
from niprov.dependencies import Dependencies
4
5
6
def markForApproval(files, reset=False, dependencies=Dependencies()):
7
    """Mark a list of files for approval by a human.
8
9
    Args:
10
        files (list): List of paths of files tracked by niprov to mark for
11
            approval.
12
        reset (bool): Also mark files that have already been approved. False 
13
            by default.
14
    """
15
    repository = dependencies.getRepository()
16
    location = dependencies.getLocationFactory()
17
    for filepath in files:
18
        loc = location.completeString(filepath)
19
        if not reset:
20
            image = repository.byLocation(loc)
21
            if image is None:
22
                raise ValueError('Unknown file: '+filepath)
23
            if image.provenance.get('approval') == 'granted':
24
                continue
25
        repository.updateApproval(loc,'pending')
26
27
def markedForApproval(dependencies=Dependencies()):
28
    """List files marked for approval by a human.
29
    """
30
    query = dependencies.getQuery() 
31
    listener = dependencies.getListener() 
32
    markedFiles = query.byApproval('pending')
33
    listener.filesMarkedForApproval(markedFiles)
34
    return markedFiles
35
36
def approve(filepath, dependencies=Dependencies()):
37
    """Mark this file as approved.
38
39
    Args:
40
        filepath (str): Path to the tracked file that has been found valid.
41
    """
42
    loc = dependencies.getLocationFactory().completeString(filepath)
43
    repository = dependencies.getRepository()
44
    repository.updateApproval(loc, 'granted')
45
46
def selectApproved(files, dependencies=Dependencies()):
47
    """Return only files that have approval status 'granted'.
48
49
    Args:
50
        files (list): List of paths of files to check for approval status.
51
    """
52
    repository = dependencies.getRepository() 
53
    location = dependencies.getLocationFactory()
54
    selection = []
55
    for filepath in files:
56
        locationString = location.completeString(filepath)
57
        img = repository.byLocation(locationString)
58
        if img.provenance.get('approval') == 'granted':
59
            selection.append(img.path)
60
    return selection
61