PipelineFactory.lookupRelativesRecursive()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
dl 0
loc 13
rs 7.3333
c 0
b 0
f 0
1
from niprov.dependencies import Dependencies
2
from niprov.pipeline import Pipeline
3
4
5
class PipelineFactory(object):
6
7
    def __init__(self, dependencies=Dependencies()):
8
        self.files = dependencies.getRepository()
9
10
    def forFile(self, image):
11
        """Create a Pipeline object based on known files 'parents' field.
12
        """
13
        filesByLocation = {image.location.toString():image}
14
15
        def lookupRelativesRecursive(images, relationToLookFor):
16
            if relationToLookFor is 'parents':
17
                parentLocations = set()
18
                for image in images:
19
                    parentLocations.update(image.provenance.get('parents',[]))
20
                relatives = self.files.byLocations(list(parentLocations))
21
            elif relationToLookFor is 'children':
22
                thisGenerationLocations = [i.location.toString() for i in images]
23
                relatives = self.files.byParents(thisGenerationLocations)
24
            for relative in relatives:
25
                filesByLocation[relative.location.toString()] = relative
26
            if relatives:
27
                lookupRelativesRecursive(relatives, relationToLookFor)
28
29
        lookupRelativesRecursive([image], 'parents')
30
        lookupRelativesRecursive([image], 'children')
31
        return Pipeline(filesByLocation.values())
32