Skip to content

Commit d375a4c

Browse files
committed
add context to core and model
1 parent 513cc59 commit d375a4c

File tree

3 files changed

+113
-106
lines changed

3 files changed

+113
-106
lines changed

safeeyes/context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class Context(MutableMapping):
8282
session: dict[str, typing.Any]
8383
state: State
8484

85+
skipped: bool = False
86+
postponed: bool = False
87+
skip_button_disabled: bool = False
88+
postpone_button_disabled: bool = False
89+
8590
ext: dict
8691

8792
def __init__(

safeeyes/core.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
from safeeyes.model import State
3030
from safeeyes.model import Config
3131

32+
from safeeyes.context import Context
33+
3234
import gi
3335

3436
gi.require_version("GLib", "2.0")
@@ -45,6 +47,7 @@ class SafeEyesCore:
4547
postpone_duration: int = 0
4648
default_postpone_duration: int = 0
4749
pre_break_warning_time: int = 0
50+
context: Context
4851

4952
_break_queue: typing.Optional[BreakQueue] = None
5053

@@ -62,7 +65,7 @@ class SafeEyesCore:
6265
# set to true when a break was requested
6366
_take_break_now: bool = False
6467

65-
def __init__(self, context) -> None:
68+
def __init__(self, context: Context) -> None:
6669
"""Create an instance of SafeEyesCore and initialize the variables."""
6770
# This event is fired before <time-to-prepare> for a break
6871
self.on_pre_break = EventHook()
@@ -77,11 +80,7 @@ def __init__(self, context) -> None:
7780
# This event is fired when deciding the next break time
7881
self.on_update_next_break = EventHook()
7982
self.context = context
80-
self.context["skipped"] = False
81-
self.context["postponed"] = False
82-
self.context["skip_button_disabled"] = False
83-
self.context["postpone_button_disabled"] = False
84-
self.context["state"] = State.WAITING
83+
self.context.state = State.WAITING
8584

8685
def initialize(self, config: Config):
8786
"""Initialize the internal properties from configuration."""
@@ -116,14 +115,14 @@ def stop(self, is_resting=False) -> None:
116115
self.paused_time = datetime.datetime.now().timestamp()
117116
# Stop the break thread
118117
self.running = False
119-
if self.context["state"] != State.QUIT:
120-
self.context["state"] = State.RESTING if (is_resting) else State.STOPPED
118+
if self.context.state != State.QUIT:
119+
self.context.state = State.RESTING if (is_resting) else State.STOPPED
121120

122121
self.__wakeup_scheduler()
123122

124123
def skip(self) -> None:
125124
"""User skipped the break using Skip button."""
126-
self.context["skipped"] = True
125+
self.context.skipped = True
127126

128127
def postpone(self, duration=-1) -> None:
129128
"""User postponed the break using Postpone button."""
@@ -132,7 +131,7 @@ def postpone(self, duration=-1) -> None:
132131
else:
133132
self.postpone_duration = self.default_postpone_duration
134133
logging.debug("Postpone the break for %d seconds", self.postpone_duration)
135-
self.context["postponed"] = True
134+
self.context.postponed = True
136135

137136
def get_break_time(
138137
self, break_type: typing.Optional[BreakType] = None
@@ -154,7 +153,7 @@ def take_break(self, break_type: typing.Optional[BreakType] = None) -> None:
154153
"""
155154
if self._break_queue is None:
156155
return
157-
if not self.context["state"] == State.WAITING:
156+
if not self.context.state == State.WAITING:
158157
return
159158

160159
if break_type is not None and self._break_queue.get_break().type != break_type:
@@ -189,7 +188,7 @@ def __scheduler_job(self) -> None:
189188
current_time = datetime.datetime.now()
190189
current_timestamp = current_time.timestamp()
191190

192-
if self.context["state"] == State.RESTING and self.paused_time > -1:
191+
if self.context.state == State.RESTING and self.paused_time > -1:
193192
# Safe Eyes was resting
194193
paused_duration = int(current_timestamp - self.paused_time)
195194
self.paused_time = -1
@@ -203,11 +202,11 @@ def __scheduler_job(self) -> None:
203202
# Skip the next long break
204203
self._break_queue.skip_long_break()
205204

206-
if self.context["postponed"]:
205+
if self.context.postponed:
207206
# Previous break was postponed
208207
logging.info("Prepare for postponed break")
209208
time_to_wait = self.postpone_duration
210-
self.context["postponed"] = False
209+
self.context.postponed = False
211210
elif current_timestamp < self.scheduled_next_break_timestamp:
212211
# Non-standard break was set.
213212
time_to_wait = round(
@@ -221,7 +220,7 @@ def __scheduler_job(self) -> None:
221220
self.scheduled_next_break_time = current_time + datetime.timedelta(
222221
seconds=time_to_wait
223222
)
224-
self.context["state"] = State.WAITING
223+
self.context.state = State.WAITING
225224
self.__fire_on_update_next_break(self.scheduled_next_break_time)
226225

227226
# Wait for the pre break warning period
@@ -262,7 +261,7 @@ def __fire_pre_break(self) -> None:
262261
if self._break_queue is None:
263262
# This will only be called by methods which check this
264263
return
265-
self.context["state"] = State.PRE_BREAK
264+
self.context.state = State.PRE_BREAK
266265
proceed = self.__fire_hook(self.on_pre_break, self._break_queue.get_break())
267266
if not proceed:
268267
# Plugins wanted to ignore this break
@@ -298,9 +297,9 @@ def __do_start_break(self) -> None:
298297
# Plugins want to ignore this break
299298
self.__start_next_break()
300299
return
301-
if self.context["postponed"]:
300+
if self.context.postponed:
302301
# Plugins want to postpone this break
303-
self.context["postponed"] = False
302+
self.context.postponed = False
304303

305304
if self.scheduled_next_break_time is None:
306305
raise Exception("this should never happen")
@@ -322,7 +321,7 @@ def __start_break(self) -> None:
322321
if self._break_queue is None:
323322
# This will only be called by methods which check this
324323
return
325-
self.context["state"] = State.BREAK
324+
self.context.state = State.BREAK
326325
break_obj = self._break_queue.get_break()
327326
self._taking_break = break_obj
328327
self._countdown = break_obj.duration
@@ -340,8 +339,8 @@ def __cycle_break_countdown(self) -> None:
340339
if (
341340
self._countdown > 0
342341
and self.running
343-
and not self.context["skipped"]
344-
and not self.context["postponed"]
342+
and not self.context.skipped
343+
and not self.context.postponed
345344
):
346345
countdown = self._countdown
347346
self._countdown -= 1
@@ -359,14 +358,14 @@ def __cycle_break_countdown(self) -> None:
359358

360359
def __fire_stop_break(self) -> None:
361360
# Loop terminated because of timeout (not skipped) -> Close the break alert
362-
if not self.context["skipped"] and not self.context["postponed"]:
361+
if not self.context.skipped and not self.context.postponed:
363362
logging.info("Break is terminated automatically")
364363
self.__fire_hook(self.on_stop_break)
365364

366365
# Reset the skipped flag
367-
self.context["skipped"] = False
368-
self.context["skip_button_disabled"] = False
369-
self.context["postpone_button_disabled"] = False
366+
self.context.skipped = False
367+
self.context.skip_button_disabled = False
368+
self.context.postpone_button_disabled = False
370369
self.__start_next_break()
371370

372371
def __wait_for(
@@ -440,7 +439,7 @@ def __start_next_break(self) -> None:
440439
if self._break_queue is None:
441440
# This will only be called by methods which check this
442441
return
443-
if not self.context["postponed"]:
442+
if not self.context.postponed:
444443
self._break_queue.next()
445444

446445
if self.running:

0 commit comments

Comments
 (0)