source: flowtimer/flowtimer/RecurringPhaseSequence.py@ d7a66ad

guix
Last change on this file since d7a66ad was d7a66ad, checked in by Enrico Schwass <ennoausberlin@…>, 9 months ago

more refactorings

Signed-off-by: Enrico Schwass <ennoausberlin@…>

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[84123db]1import json
[37ae3b7]2from copy import deepcopy
[3b76475]3from flowtimer.Phase import Phase
[84123db]4
5
6class RecurringPhaseSequence:
7
[37ae3b7]8 @classmethod
9 def from_json(cls, a_json_string):
10 def custom_object_hook(d):
[daa2276]11 if 'title' in d and 'initial_ticks' in d:
12 return Phase(d['title'], d['initial_ticks'])
13 if 'phase_list' in d and 'initial_repetitions' in d:
[1fd7029]14 return RecurringPhaseSequence(d["title"], d['phase_list'], d['initial_repetitions'])
15 print("Wrong format")
[37ae3b7]16 return d
17 return json.loads(a_json_string, object_hook=custom_object_hook)
18
19 @classmethod
20 def default_json_string(cls):
[1fd7029]21 return json.dumps({"title": "default",
[d7a66ad]22 "phase_list": [{"title": "Tasking", "initial_ticks": 5},
[daa2276]23 {"title": "Work", "initial_ticks": 45},
24 {"title": "Break", "initial_ticks": 15}],
25 "initial_repetitions": 3})
[37ae3b7]26
[f959488]27 @classmethod
28 def default(cls):
29 return cls.from_json(cls.default_json_string())
30
[1fd7029]31 def __init__(self, title, phase_list, repetitions):
[f959488]32 assert repetitions > 0
33 assert phase_list is not []
[1fd7029]34 self._title = title
[f959488]35 self.state = "initial"
[84123db]36 self.phase_list = phase_list
[f959488]37 self.current_phase = phase_list[0]
38 self.initial_repetitions = repetitions
39 self.passes_left = repetitions
[84123db]40
41 def to_json(self):
42 return json.dumps(self.__dict__, default=lambda each: each.to_json())
43
[1fd7029]44 @property
[44c3377]45 def title(self):
[36c9ef5]46 return self._title
[44c3377]47
[d7a66ad]48 def __str__(self):
49 return ("Sequence title:" + self.title + "\n" + str(self.current_phase))
50
[5741f6d]51 def is_sequence(self):
52 return True
53
[daa2276]54 def current_phase_number(self):
55 return self.phase_list.index(self.current_phase)
56
57 def phases_left_in_pass(self):
58 return len(self.upcoming_phases_in_pass())
59
[f959488]60 def upcoming_phases_in_pass(self):
[daa2276]61 if self.current_phase_number() < len(self.phase_list) - 1:
62 return self.phase_list[self.current_phase_number()+1:]
[f959488]63 return []
64
[1fd7029]65 @property
66 def initial_ticks(self):
67 return sum([each.initial_ticks for each in self.phase_list])
68
[daa2276]69 @property
[f959488]70 def ticks_left(self):
[daa2276]71 return (
72 (self.passes_left-1) * sum([each.initial_ticks for each in self.phase_list]) +
73 self.current_phase.ticks_left +
74 sum([each.ticks_left for each in self.upcoming_phases_in_pass()]))
[f959488]75
[d7a66ad]76 def completed(self):
[a438158]77 return ((self.passes_left < 1) and
78 (not self.upcoming_phases_in_pass() and
[d7a66ad]79 self.current_phase.completed()))
[f959488]80
81 def abort(self):
82 self.current_phase.abort()
[d7a66ad]83 self.state = "aborted"
[f959488]84
[1fd7029]85 def start(self):
86 self.state = "running"
87
[36c9ef5]88 def skip(self):
89 if self.upcoming_phases_in_pass():
90 self.current_phase.reset()
91 self.current_phase = self.upcoming_phases_in_pass()[0]
92 return
93 else:
[a438158]94 print("Sequence finished")
[36c9ef5]95 if self.passes_left == 0:
96 self.abort()
97 return
98 else:
99 self.passes_left -= 1
100 self.current_phase.reset()
101 self.current_phase = self.phase_list[0]
102
[d7a66ad]103 def advance_to_next_phase(self):
104 current_index = self.current_phase_number()
105 if current_index < len(self.phase_list) - 1:
106 # Move to the next phase in the sequence
107 self.current_phase.reset()
108 self.current_phase = self.phase_list[current_index + 1]
109 else:
110 # Completed a full sequence; check if more repetitions are needed
111 self.passes_left -= 1
112 if self.passes_left < 1:
113 self.state = "completed"
114 else:
115 self.current_phase.reset()
116 self.current_phase = self.phase_list[0] # Reset to the first phase
117
[f959488]118 def tick(self, ticks):
[d7a66ad]119 print("Before tick")
120 print(self.current_phase)
121 print("Tick:", ticks)
122 if not self.completed():
[f959488]123 result = self.current_phase.tick(ticks)
[d7a66ad]124 print("Result", result, "\n")
125 if self.current_phase.completed():
126 print(self.current_phase.title, "Phase Completed")
127 self.advance_to_next_phase()
128 print("Advanced to", self.current_phase, "\n")
129 result = self.tick(abs(result))
130 return
131 if self.completed():
132 self.advance_to_next_phase()
133 print("Passes left:", self.passes_left)
[f959488]134
[84123db]135 def unrolled(self):
[daa2276]136 return [deepcopy(seq) for seq in [each for each in self.initial_repetitions * self.phase_list]]
Note: See TracBrowser for help on using the repository browser.