slackoff.config.Settings._update()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nop 3
1
from dataclasses import dataclass
2
3
import log
4
from datafiles import datafile, field
5
6
7
@dataclass
8
class Workspace:
9
    name: str
10
    active: bool = True
11
    uses: int = 0
12
13
14
@datafile("~/Library/Preferences/slackoff.yml")
15
@dataclass
16
class Settings:
17
    workspaces: list[Workspace] = field(default_factory=list)
18
19
    def activate(self, name: str):
20
        log.debug(f"Marking workspace active: {name}")
21
        workspace = self._update(name, True)
22
        workspace.uses += 1
23
24
    def deactivate(self, name: str):
25
        log.debug(f"Marking workspace inactive: {name}")
26
        self._update(name, False)
27
28
    def _update(self, name: str, active: bool):
29
        for workspace in self.workspaces:
30
            if workspace.name == name:
31
                workspace.active = active
32
                return workspace
33
34
        workspace = Workspace(name, active)
35
        self.workspaces.append(workspace)
36
        return workspace
37
38
39
settings = Settings()
40