Passed
Push — develop ( 7270f6...02f680 )
by Dean
02:23
created

BackupGroup.list()   B

Complexity

Conditions 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 26.8308
Metric Value
cc 6
dl 0
loc 17
ccs 2
cts 12
cp 0.1666
crap 26.8308
rs 8
1 1
from plugin.core.backup.constants import BACKUP_PATH
2
3 1
import os
4
5
6 1
class BackupGroup(object):
7 1
    def __init__(self, name, path):
8
        self.name = name
9
        self.path = path
10
11 1
    @classmethod
12
    def load(cls, path):
13
        name = os.path.relpath(path, BACKUP_PATH)
14
15
        return cls(name, path)
16
17 1
    @classmethod
18 1
    def list(cls, search_path=BACKUP_PATH, max_depth=0):
19
        if not os.path.exists(search_path):
20
            return
21
22
        names = os.listdir(search_path)
23
24
        for name in names:
25
            path = os.path.join(search_path, name)
26
27
            if path.endswith('.bgr'):
28
                # Load backup group
29
                yield cls.load(path)
30
            elif max_depth > 0:
31
                # Search sub-directory for backup groups
32
                for p in cls.list(path, max_depth - 1):
33
                    yield p
34
35 1
    def __repr__(self):
36
        return '<BackupGroup name: %r>' % self.name
37