Fix breaks queue next method

This commit is contained in:
Gobinath 2021-06-19 17:37:17 -04:00
parent b4dc189095
commit 2fb22bbc2d
16 changed files with 65 additions and 59 deletions

View File

@ -36,7 +36,6 @@ class Queue:
self.__index: int = 0
self.__random: bool = random_queue
self.__first_peek = True
self.__first_next = True
def is_empty(self) -> bool:
"""
@ -65,20 +64,15 @@ class Queue:
def next(self) -> Optional[Break]:
"""
Move to the next item in the queue.
Return the current value and move to the next.
"""
if self.__first_next:
self.__first_next = False
self.__first_peek = False
return self.peek()
if self.__index == (len(self.__values) - 1) and self.__random:
# Starting a new cycle. If required, shuffle the queue next time.
self.shuffle()
current_break = self.peek()
self.__index = (self.__index + 1) % len(self.__values)
return self.peek()
if self.__random and current_break is not None and self.__index == 0:
# Starting a new cycle. If required, shuffle the queue next time.
# self.__index will be zero only at the end of a cycle if there is at least one break
self.shuffle()
return current_break
def reset(self) -> None:
"""

View File

@ -107,15 +107,19 @@ class BreakScheduler(BreakAPI):
short_break_time: Optional[datetime.datetime] = None
long_break_time: Optional[datetime.datetime] = None
if next_break.is_long_break():
long_break_time = next_break_time
next_short_break = self.__breaks_store.peek(BreakType.SHORT)
if next_short_break:
short_break_time = next_break_time + datetime.timedelta(minutes=next_short_break.waiting_time)
else:
short_break_time = next_break_time
next_long_break = self.__breaks_store.peek(BreakType.LONG)
if next_long_break:
long_break_time = next_break_time + datetime.timedelta(minutes=next_long_break.waiting_time)
self.__context.core_api.set_status(_('Next break at %s') % (utility.format_time(next_break_time)))
formatted_time = utility.format_time(next_break_time)
logging.info("Scheduling the break '%s' at %s", next_break.name, formatted_time)
self.__context.core_api.set_status(_('Next break at %s') % formatted_time)
self.__plugins.update_next_break(next_break, short_break_time, long_break_time)
self.__timer.schedule(next_break_time)

View File

@ -64,6 +64,10 @@ class BreaksStore:
if self.is_empty():
return break_obj
if self.__current_break:
# Reset the last break time
self.__current_break.reset_time()
if self.__short_queue.is_empty() or break_type == BreakType.LONG:
break_obj = self.__next_long()
elif self.__long_queue.is_empty() or break_type == BreakType.SHORT:
@ -77,10 +81,6 @@ class BreaksStore:
else:
break_obj = self.__next_short()
if self.__current_break:
# Reset the last break time
self.__current_break.reset_time()
if break_obj:
self.__current_break = break_obj
self.__context.session.set(SESSION_KEY_BREAK, break_obj.name)
@ -112,10 +112,9 @@ class BreaksStore:
if break_obj is not None:
self.__context.session.set(SESSION_KEY_BREAK_TYPE, BreakType.SHORT)
# Reduce the waiting time from the next long break
if not self.__long_queue.is_empty():
next_long_break = self.__long_queue.peek()
if next_long_break:
next_long_break.waiting_time -= break_obj.waiting_time
next_long_break = self.__long_queue.peek()
if next_long_break:
next_long_break.waiting_time -= break_obj.waiting_time
return break_obj

View File

@ -118,7 +118,8 @@ class PluginLoader:
new_settings['path'] = os.path.join(plugin_dir, plugin_id)
plugin_obj = PluginProxy(plugin['id'], module, plugin_enabled, plugin_config, new_settings)
self.__plugins[plugin['id']] = plugin_obj
plugin_obj.enable()
if plugin_enabled:
plugin_obj.enable()
@staticmethod
def load_plugins_config(context: Context, config: Config) -> List[dict]:

View File

@ -13,6 +13,7 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import inspect
import logging
from datetime import datetime
from typing import Optional, Any
@ -50,24 +51,26 @@ class PluginProxy(Plugin):
return self.__enabled
def __is_supported(self, break_obj: Break):
if self.__config.get('break_override_allowed', False):
return break_obj.is_plugin_enabled(self.__id, self.__enabled)
if self.__enabled:
return True
else:
return self.__enabled
return self.__config.get("break_override_allowed", False) and break_obj.is_plugin_enabled(self.__id)
def can_breaks_override(self) -> bool:
return self.__config.get('break_override_allowed', False)
return self.__config.get("break_override_allowed", False)
def disable(self) -> None:
if self.__enabled:
self.__enabled = False
if ModuleUtil.has_method(self.__plugin, 'disable', 0):
if ModuleUtil.has_method(self.__plugin, "disable", 0):
logging.debug("Disable the plugin '%s'", self.__id)
return self.__plugin.disable()
def enable(self) -> None:
if not self.__enabled:
self.__enabled = True
if ModuleUtil.has_method(self.__plugin, 'enable', 0):
if ModuleUtil.has_method(self.__plugin, "enable", 0):
logging.debug("Enable the plugin '%s'", self.__id)
return self.__plugin.enable()
def update_settings(self, plugin_settings: dict) -> None:
@ -77,14 +80,16 @@ class PluginProxy(Plugin):
"""
This function is called to initialize the plugin.
"""
if ModuleUtil.has_method(self.__plugin, 'init', 2):
if ModuleUtil.has_method(self.__plugin, "init", 2):
logging.debug("Init the plugin '%s'", self.__id)
self.__plugin.init(context, self.__settings)
def get_break_action(self, break_obj: Break) -> Optional[BreakAction]:
"""
This function is called before on_pre_break and on_start_break.
"""
if ModuleUtil.has_method(self.__plugin, 'get_break_action', 1):
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, "get_break_action", 1):
logging.debug("Get break action from the plugin '%s'", self.__id)
return self.__plugin.get_break_action(break_obj)
return BreakAction.allow()
@ -93,35 +98,40 @@ class PluginProxy(Plugin):
Called some time before starting the break to prepare the plugins. For example, the notification plugin will
show a notification during this method call.
"""
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, 'on_pre_break', 1):
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, "on_pre_break", 1):
logging.debug("Call on_pre_break of the plugin '%s'", self.__id)
self.__plugin.on_pre_break(break_obj)
def on_start_break(self, break_obj: Break) -> None:
"""
Called when starting a break.
"""
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, 'on_start_break', 1):
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, "on_start_break", 1):
logging.debug("Call on_start_break of the plugin '%s'", self.__id)
self.__plugin.on_start_break(break_obj)
def on_count_down(self, break_obj: Break, countdown: int, seconds: int) -> None:
"""
Called during a break.
"""
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, 'on_count_down', 3):
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, "on_count_down", 3):
# Do not log the on_count_down function call as there will be too many entries
self.__plugin.on_count_down(break_obj, countdown, seconds)
def on_stop_break(self, break_obj: Break, skipped: bool, postponed: bool) -> None:
"""
Called when a break is stopped.
"""
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, 'on_stop_break', 3):
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, "on_stop_break", 3):
logging.debug("Call on_stop_break of the plugin '%s'", self.__id)
self.__plugin.on_stop_break(break_obj, skipped, postponed)
def get_widget(self, break_obj: Break) -> Optional[Widget]:
"""
Return an optional break screen widget.
"""
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, 'get_widget', 1):
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, "get_widget", 1):
logging.debug("Get widget from the plugin '%s'", self.__id)
return self.__plugin.get_widget(break_obj)
return None
@ -129,7 +139,8 @@ class PluginProxy(Plugin):
"""
Return an optional break screen widget.
"""
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, 'get_tray_action', 1):
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, "get_tray_action", 1):
logging.debug("Get tray icon from the plugin '%s'", self.__id)
return self.__plugin.get_tray_action(break_obj)
return None
@ -137,21 +148,24 @@ class PluginProxy(Plugin):
"""
Called when Safe Eyes is started.
"""
if self.__enabled and ModuleUtil.has_method(self.__plugin, 'on_start', 0):
if self.__enabled and ModuleUtil.has_method(self.__plugin, "on_start", 0):
logging.debug("Call on_start of the plugin '%s'", self.__id)
self.__plugin.on_start()
def on_stop(self) -> None:
"""
Called when Safe Eyes is stopped.
"""
if self.__enabled and ModuleUtil.has_method(self.__plugin, 'on_stop', 0):
if self.__enabled and ModuleUtil.has_method(self.__plugin, "on_stop", 0):
logging.debug("Call on_stop of the plugin '%s'", self.__id)
self.__plugin.on_stop()
def on_exit(self) -> None:
"""
Called when Safe Eyes is closed.
"""
if self.__enabled and ModuleUtil.has_method(self.__plugin, 'on_exit', 0):
if self.__enabled and ModuleUtil.has_method(self.__plugin, "on_exit", 0):
logging.debug("Call on_exit of the plugin '%s'", self.__id)
self.__plugin.on_exit()
def update_next_break(self, break_obj: Break, next_short_break: Optional[datetime],
@ -159,7 +173,8 @@ class PluginProxy(Plugin):
"""
Called when the next break is scheduled.
"""
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, 'update_next_break', 3):
if self.__is_supported(break_obj) and ModuleUtil.has_method(self.__plugin, "update_next_break", 3):
logging.debug("Call update_next_break of the plugin '%s'", self.__id)
self.__plugin.update_next_break(break_obj, next_short_break, next_long_break)

View File

@ -58,7 +58,6 @@ def init(ctx: Context, plugin_config: dict) -> None:
Initialize the plugin.
"""
global player
logging.info('Audible Alert: initialize the plugin')
player = Player(plugin_config)

View File

@ -298,7 +298,6 @@ def init(context: Context, config: dict) -> None:
"""
This function is called to initialize the plugin.
"""
logging.info("Break Screen: initialize the plugin")
global safe_eyes_context, break_config
safe_eyes_context = context
break_config = config

View File

@ -140,7 +140,6 @@ system_state: SystemState
def init(ctx: Context, plugin_config: dict):
logging.info('Do Not Disturb: initialize the plugin')
global system_state
system_state = SystemState(ctx, plugin_config)

View File

@ -145,7 +145,6 @@ def init(ctx: Context, plugin_config: dict) -> None:
"""
Initialize the plugin.
"""
logging.info('Health Stats: initialize the plugin')
global stats
stats = HealthStats(ctx, plugin_config)

View File

@ -66,7 +66,6 @@ def init(ctx: Context, plugin_config: dict) -> None:
"""
Initialize the screensaver plugin.
"""
logging.info('Media Control: initialize the plugin')
global tray_icon_path
tray_icon_path = os.path.join(plugin_config['path'], "resource/pause.png")

View File

@ -45,7 +45,6 @@ def init(ctx: Context, plugin_config: dict) -> None:
"""
global context
global warning_time
logging.info("Notification: initialize the plugin")
context = ctx
warning_time = ctx.config.get("pre_break_warning_time")
@ -75,7 +74,6 @@ def on_start_break(break_obj: Break) -> None:
Close the notification.
"""
global notification
logging.debug("Notification: close the pre-break notification")
if notification:
try:
notification.close()

View File

@ -97,7 +97,6 @@ def init(ctx: Context, plugin_config: dict) -> None:
"""
Initialize the screensaver plugin.
"""
logging.info("Screensaver: initialize the plugin")
global screensaver
global tray_icon_path
screensaver = Screensaver(ctx, plugin_config)

View File

@ -286,7 +286,6 @@ class TrayIcon:
"""
Update the next break time to be displayed in the menu and optionally in the tray icon.
"""
logging.debug("Tray Icon: update the next break information")
self.__date_time = date_time
self.__set_next_break_info()
@ -400,7 +399,6 @@ def init(ctx: Context, plugin_config: dict):
"""
global context
global tray_icon
logging.debug("Tray Icon: initialize the plugin")
context = ctx
if not tray_icon:
tray_icon = TrayIcon(context, plugin_config)

View File

@ -66,14 +66,14 @@ class Break:
"""
return self.type == BreakType.SHORT
def is_plugin_enabled(self, plugin_id, is_plugin_enabled) -> bool:
def is_plugin_enabled(self, plugin_id) -> bool:
"""
Check whether this break supports the given plugin.
"""
if self.plugins:
return plugin_id in self.plugins
else:
return is_plugin_enabled
return False
def reset_time(self) -> None:
self.waiting_time = self.__original_time

View File

@ -1,7 +1,7 @@
import unittest
from safeeyes.breaks.queue import Queue
from safeeyes.spi.breaks.spi import Break, BreakType
from safeeyes.spi.breaks import Break, BreakType
class QueueTestCase(unittest.TestCase):
@ -18,11 +18,14 @@ class QueueTestCase(unittest.TestCase):
self.assertEqual(queue.is_empty(), False)
self.assertEqual(queue.peek(), short_1)
self.assertEqual(queue.next(), short_2)
self.assertEqual(queue.next(), short_3)
self.assertEqual(queue.peek(), short_3)
self.assertEqual(queue.next(), short_4)
self.assertEqual(queue.next(), short_1)
self.assertEqual(queue.next(), short_2)
self.assertEqual(queue.peek(), short_3)
self.assertEqual(queue.next(), short_3)
self.assertEqual(queue.next(), short_4)
self.assertEqual(queue.peek(), short_1)
self.assertEqual(queue.next(), short_1)
self.assertEqual(queue.peek(), short_2)
def test_rand_queue(self):
queue = Queue(True)

View File

@ -1,6 +1,6 @@
import unittest
from safeeyes.util.env import DesktopEnvironment
from safeeyes.env.desktop import DesktopEnvironment
class DesktopEnvironmentTestCase(unittest.TestCase):