Passed
Push — master ( 64ad20...04166b )
by Dean
02:49
created

SyncMap.__init__()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
cc 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1 1
from plugin.core.constants import GUID_SERVICES
2
3 1
import logging
4
5 1
log = logging.getLogger(__name__)
6
7
8 1
class SyncMap(object):
9 1
    def __init__(self, task):
10 1
        self.task = task
11
12 1
        self._by_guid = {}
13 1
        self._by_key = {}
14
15 1
    def add(self, p_section_key, p_key, guids):
16
        if not guids or not p_key:
17
            return False
18
19
        for guid in guids:
20
            self.add_one(p_section_key, p_key, guid)
21
22 1
    def add_one(self, p_section_key, p_key, guid):
23 1
        if guid is None or p_key is None:
24
            return False
25
26 1
        p_key = int(p_key)
27
28
        # Flatten `guid`
29 1
        if type(guid) is not tuple:
30
            guid = (guid.service, guid.id)
31
32 1
        if guid[0] not in GUID_SERVICES:
33
            log.info('Unknown primary agent: %r -> %r (section: %r)', guid[0], p_key, p_section_key)
34
35
        # Store in `_by_guid` map
36 1
        if guid not in self._by_guid:
37 1
            self._by_guid[guid] = set()
38
39 1
        self._by_guid[guid].add((p_section_key, p_key))
40
41
        # Store in `_by_key` map
42 1
        if p_key not in self._by_key:
43 1
            self._by_key[p_key] = set()
44
45 1
        self._by_key[p_key].add(guid)
46
47 1
        return True
48
49 1
    def by_guid(self, guid):
50
        # Flatten `guid`
51 1
        if type(guid) is not tuple:
52
            guid = (guid.service, guid.id)
53
54 1
        return self._by_guid.get(guid, set())
55
56 1
    def by_key(self, rating_key):
57 1
        if rating_key is None:
58
            return set()
59
60
        return self._by_key.get(int(rating_key), set())
61