1 | import json
|
---|
2 | from copy import deepcopy
|
---|
3 | from flowtimer.Phase import Phase
|
---|
4 |
|
---|
5 |
|
---|
6 | class RecurringPhaseSequence:
|
---|
7 |
|
---|
8 | @classmethod
|
---|
9 | def from_json(cls, a_json_string):
|
---|
10 | def custom_object_hook(d):
|
---|
11 | if 'title' in d and 'duration' in d:
|
---|
12 | return Phase(d['title'], d['duration'])
|
---|
13 | if 'phase_list' in d and 'repetitions' in d:
|
---|
14 | return RecurringPhaseSequence(d['phase_list'], d['repetitions'])
|
---|
15 | return d
|
---|
16 | return json.loads(a_json_string, object_hook=custom_object_hook)
|
---|
17 |
|
---|
18 | @classmethod
|
---|
19 | def default_json_string(cls):
|
---|
20 | return json.dumps({"phase_list": [{"title": "Huddle", "duration": 10},
|
---|
21 | {"title": "Tasking", "duration": 5},
|
---|
22 | {"title": "Work", "duration": 45},
|
---|
23 | {"title": "Break", "duration": 15}],
|
---|
24 | "repetitions": 3})
|
---|
25 |
|
---|
26 | def __init__(self, phase_list, repetitions):
|
---|
27 | self.phase_list = phase_list
|
---|
28 | self.repetitions = repetitions
|
---|
29 |
|
---|
30 | def to_json(self):
|
---|
31 | return json.dumps(self.__dict__, default=lambda each: each.to_json())
|
---|
32 |
|
---|
33 | def unrolled(self):
|
---|
34 | return [deepcopy(seq) for seq in [each for each in self.repetitions * self.phase_list]]
|
---|
35 | # return self.repetitions * self.phase_list
|
---|