def test_default_settings_config_file_not_exist(): config = Configuration("dummy_file") config.load() assert config.session_duration == 25 * 60 assert config.short_break_duration == 5 * 60 assert config.long_break_duration == 15 * 60 assert config.long_break_frequency == 4
def test_load_sets_default_ui_plugin_without_ui_plugin_config(config, fs): data = copy.deepcopy(config_data) del (data["plugins"]["ui"]) _create_config(config_file, data) c = Configuration(config_file) c.load() assert c.ui_plugin == "qtapp"
def test_load_reads_config_data_if_available(): config = Configuration(None, config_data) config.load() assert config.session_duration == 10 * 60 assert config.short_break_duration == 2 * 60 assert config.long_break_duration == 5 * 60 assert config.long_break_frequency == 2
def test_load_sets_default_ui_plugin_without_ui_plugin_config(config, fs): data = copy.deepcopy(config_data) del(data["plugins"]["ui"]) _create_config(config_file, data) c = Configuration(config_file) c.load() assert c.ui_plugin == "qtapp"
def test_load_prefers_env_config_file_over_specified(config, fs): os.environ["POMITO_CONFIG"] = config_file try: c = Configuration("/tmp/non_existent.ini") c.load() assert c.session_duration == 10 * 60 assert c.get_setting("section1") == [("k1", "v1")] finally: del os.environ["POMITO_CONFIG"]
def test_default_settings_config_file_no_content(fs): config_file = "/tmp/empty_file.ini" fs.CreateFile(config_file) config = Configuration(config_file) config.load() assert config.session_duration == 25 * 60 assert config.short_break_duration == 5 * 60 assert config.long_break_duration == 15 * 60 assert config.long_break_frequency == 4 assert config.ui_plugin == "qtapp" assert config.task_plugin == "nulltask"
def test_load_reads_fractional_config_duration(): fraction_data = {"pomito": {"session_duration": 0.12, "short_break_duration": 0.21, "long_break_duration": 0.53, "long_break_frequency": 2}} config = Configuration(None, fraction_data) config.load() assert config.session_duration == 7 assert config.short_break_duration == 12 assert config.long_break_duration == 31 assert config.long_break_frequency == 2
def test_load_reads_fractional_config_duration(): fraction_data = { "pomito": { "session_duration": 0.12, "short_break_duration": 0.21, "long_break_duration": 0.53, "long_break_frequency": 2 } } config = Configuration(None, fraction_data) config.load() assert config.session_duration == 7 assert config.short_break_duration == 12 assert config.long_break_duration == 31 assert config.long_break_frequency == 2
def create_fake_config(self): """Create a fake configuration instance.""" from pomito.plugins import PLUGINS PLUGINS['dummyUI'] = Mock(spec=UIPlugin) PLUGINS['dummyTask'] = MagicMock(spec=TaskPlugin) return Configuration(self.config_file, self.config_data)
def config(fs, mocker): # Create a fake config file os_module = fake_filesystem.FakeOsModule(fs) fileopen = fake_filesystem.FakeFileOpen(fs) # Patch filesystem calls with fake implementations mocker.patch("os.path", os_module.path) mocker.patch("builtins.open", fileopen) fs.CreateFile(config_file) _create_config(config_file, config_data) return Configuration(config_file)
def __init__(self, config=None, database=None, message_dispatcher=None): """Create a Pomito object. Arguments: config Configuration Path to the configuration file database peewee.SqliteDatabase database to use for tasks etc. message_dispatcher MessageDispatcher message dispatcher instance """ from pomito import pomodoro self._config = config self._database = database self._message_dispatcher = message_dispatcher self._threads = {} self._hooks = [] if self._message_dispatcher is None: self._message_dispatcher = MessageDispatcher() if self._config is None: self._config_file = os.path.join(CONFIG_DIR, "config.ini") self._config = Configuration(self._config_file) self._config.load() # Pomodoro service instance. Order of initializations are important self.pomodoro_service = pomodoro.Pomodoro(self) # Default plugins pomito.plugins.initialize(self.pomodoro_service) self.ui_plugin = pomito.plugins.get_plugin(self._config.ui_plugin) self.task_plugin = pomito.plugins.get_plugin(self._config.task_plugin) # Add the plugins to threads list self._threads['task_plugin'] = threading.Thread( target=self.task_plugin) # Default hooks from pomito.hooks import activity self._hooks.append(activity.ActivityHook(self.pomodoro_service)) return
def __init__(self, config=None, database=None, message_dispatcher=None): """Create a Pomito object. Arguments: config Configuration Path to the configuration file database peewee.SqliteDatabase database to use for tasks etc. message_dispatcher MessageDispatcher message dispatcher instance """ from pomito import pomodoro self._config = config self._database = database self._message_dispatcher = message_dispatcher self._threads = {} self._hooks = [] if self._message_dispatcher is None: self._message_dispatcher = MessageDispatcher() if self._config is None: self._config_file = os.path.join(CONFIG_DIR, "config.ini") self._config = Configuration(self._config_file) self._config.load() # Pomodoro service instance. Order of initializations are important self.pomodoro_service = pomodoro.Pomodoro(self) # Default plugins pomito.plugins.initialize(self.pomodoro_service) self.ui_plugin = pomito.plugins.get_plugin(self._config.ui_plugin) self.task_plugin = pomito.plugins.get_plugin(self._config.task_plugin) # Add the plugins to threads list self._threads['task_plugin'] = threading.Thread(target=self.task_plugin) # Default hooks from pomito.hooks import activity self._hooks.append(activity.ActivityHook(self.pomodoro_service)) return
class Pomito(object): """Controls the application lifetime. Responsibilities: - Read and initialize the configuration - Choose the run mode - Handover execution to UI plugin """ def __init__(self, config=None, database=None, message_dispatcher=None): """Create a Pomito object. Arguments: config Configuration Path to the configuration file database peewee.SqliteDatabase database to use for tasks etc. message_dispatcher MessageDispatcher message dispatcher instance """ from pomito import pomodoro self._config = config self._database = database self._message_dispatcher = message_dispatcher self._threads = {} self._hooks = [] if self._message_dispatcher is None: self._message_dispatcher = MessageDispatcher() if self._config is None: self._config_file = os.path.join(CONFIG_DIR, "config.ini") self._config = Configuration(self._config_file) self._config.load() # Pomodoro service instance. Order of initializations are important self.pomodoro_service = pomodoro.Pomodoro(self) # Default plugins pomito.plugins.initialize(self.pomodoro_service) self.ui_plugin = pomito.plugins.get_plugin(self._config.ui_plugin) self.task_plugin = pomito.plugins.get_plugin(self._config.task_plugin) # Add the plugins to threads list self._threads['task_plugin'] = threading.Thread( target=self.task_plugin) # Default hooks from pomito.hooks import activity self._hooks.append(activity.ActivityHook(self.pomodoro_service)) return def initialize(self): """Initialize configuration, database and starts worker threads.""" os.makedirs(DATA_DIR, exist_ok=True) database_path = os.path.join(DATA_DIR, "pomito.db") if self._database is None: self._database = SqliteDatabase(None) self._database.init(database_path) self._database.connect() # Initialize the plugins self.ui_plugin.initialize() self.task_plugin.initialize() # Initialize the hooks for hook in self._hooks: hook.initialize() return def run(self): """Start the application.""" if not self._validate_state(): logger.critical("Pomito.Run: Invalid state. Exiting.") return self.initialize() self._message_dispatcher.start() self.ui_plugin.run() self.exit() def exit(self): """Clean up and save any configuration data. Prepare for exiting the application.""" if self._message_dispatcher.is_alive(): self._message_dispatcher.stop() self._message_dispatcher.join() for hook in self._hooks: hook.close() if self._database is not None: self._database.close() def get_db(self): """Get the database object. Returns: database peewee.SqliteDatabase object """ return self._database def get_configuration(self): return self._config def queue_signal(self, message): self._message_dispatcher.queue_message(message) def _validate_state(self): """Validates configuration, plugins.""" import pomito.plugins _retval = True if not issubclass(type(self.ui_plugin), pomito.plugins.ui.UIPlugin): logger.error("Invalid UIPlugin object = {0}".format( self.ui_plugin)) _retval = False if not issubclass(type(self.task_plugin), pomito.plugins.task.TaskPlugin): logger.error("Invalid TaskPlugin object = {0}".format( self.task_plugin)) _retval = False return _retval
class Pomito(object): """Controls the application lifetime. Responsibilities: - Read and initialize the configuration - Choose the run mode - Handover execution to UI plugin """ def __init__(self, config=None, database=None, message_dispatcher=None): """Create a Pomito object. Arguments: config Configuration Path to the configuration file database peewee.SqliteDatabase database to use for tasks etc. message_dispatcher MessageDispatcher message dispatcher instance """ from pomito import pomodoro self._config = config self._database = database self._message_dispatcher = message_dispatcher self._threads = {} self._hooks = [] if self._message_dispatcher is None: self._message_dispatcher = MessageDispatcher() if self._config is None: self._config_file = os.path.join(CONFIG_DIR, "config.ini") self._config = Configuration(self._config_file) self._config.load() # Pomodoro service instance. Order of initializations are important self.pomodoro_service = pomodoro.Pomodoro(self) # Default plugins pomito.plugins.initialize(self.pomodoro_service) self.ui_plugin = pomito.plugins.get_plugin(self._config.ui_plugin) self.task_plugin = pomito.plugins.get_plugin(self._config.task_plugin) # Add the plugins to threads list self._threads['task_plugin'] = threading.Thread(target=self.task_plugin) # Default hooks from pomito.hooks import activity self._hooks.append(activity.ActivityHook(self.pomodoro_service)) return def initialize(self): """Initialize configuration, database and starts worker threads.""" os.makedirs(DATA_DIR, exist_ok=True) database_path = os.path.join(DATA_DIR, "pomito.db") if self._database is None: self._database = SqliteDatabase(None) self._database.init(database_path) self._database.connect() # Initialize the plugins self.ui_plugin.initialize() self.task_plugin.initialize() # Initialize the hooks for hook in self._hooks: hook.initialize() return def run(self): """Start the application.""" if not self._validate_state(): logger.critical("Pomito.Run: Invalid state. Exiting.") return self.initialize() self._message_dispatcher.start() self.ui_plugin.run() self.exit() def exit(self): """Clean up and save any configuration data. Prepare for exiting the application.""" if self._message_dispatcher.is_alive(): self._message_dispatcher.stop() self._message_dispatcher.join() for hook in self._hooks: hook.close() if self._database is not None: self._database.close() def get_db(self): """Get the database object. Returns: database peewee.SqliteDatabase object """ return self._database def get_configuration(self): return self._config def queue_signal(self, message): self._message_dispatcher.queue_message(message) def _validate_state(self): """Validates configuration, plugins.""" import pomito.plugins _retval = True if not issubclass(type(self.ui_plugin), pomito.plugins.ui.UIPlugin): logger.error("Invalid UIPlugin object = {0}".format(self.ui_plugin)) _retval = False if not issubclass(type(self.task_plugin), pomito.plugins.task.TaskPlugin): logger.error("Invalid TaskPlugin object = {0}".format(self.task_plugin)) _retval = False return _retval