def find_plugins(self): """ Scan a directory for objects inheriting from the ``Plugin`` class. """ glob_pattern = os.path.join('plugins', '*', '[!.]*plugin.py') extension_loader(glob_pattern) plugin_classes = Plugin.__subclasses__() plugin_objects = [] for p in plugin_classes: try: plugin = p() self.log_debug('Loaded plugin {plugin}'.format(plugin=str(p))) plugin_objects.append(plugin) except TypeError: self.log_exception( 'Failed to load plugin {plugin}'.format(plugin=str(p))) plugins_list = sorted(plugin_objects, key=lambda plugin: plugin.weight) for plugin in plugins_list: if plugin.check_pre_conditions(): self.log_debug( 'Plugin {plugin} active'.format(plugin=str(plugin.name))) plugin.set_status() else: plugin.status = PluginStatus.Disabled self.plugins.append(plugin)
def check_pre_conditions(self): """ Check to see if we have any presentation software available. If not do not install the plugin. """ log.debug('check_pre_conditions') controller_dir = os.path.join('plugins', 'presentations', 'lib') # Find all files that do not begin with '.' (lp:#1738047) and end with controller.py glob_pattern = os.path.join(controller_dir, '[!.]*controller.py') extension_loader(glob_pattern, ['presentationcontroller.py']) controller_classes = PresentationController.__subclasses__() for controller_class in controller_classes: controller = controller_class(self) self.register_controllers(controller) return bool(self.controllers)
def bootstrap_initialise(self): """ Bootstrap all the plugin manager functions Scan a directory for objects inheriting from the ``Plugin`` class. """ glob_pattern = os.path.join('plugins', '*', '[!.]*plugin.py') extension_loader(glob_pattern) plugin_classes = Plugin.__subclasses__() for p in plugin_classes: try: p() self.log_debug('Loaded plugin {plugin}'.format(plugin=str(p))) except TypeError: self.log_exception( 'Failed to load plugin {plugin}'.format(plugin=str(p)))
def test_extension_loader_no_files_found(self): """ Test the `extension_loader` function when no files are found """ # GIVEN: A mocked `Path.glob` method which does not match any files with patch('openlp.core.common.applocation.AppLocation.get_directory', return_value=Path('/', 'app', 'dir', 'openlp')), \ patch.object(Path, 'glob', return_value=[]), \ patch('openlp.core.common.importlib.import_module') as mocked_import_module: # WHEN: Calling `extension_loader` extension_loader('glob', ['file2.py', 'file3.py']) # THEN: `extension_loader` should not try to import any files assert mocked_import_module.called is False
def test_extension_loader_os_error(self): """ Test the `extension_loader` function when `import_module` raises a `ImportError` """ # GIVEN: A mocked `SourceFileLoader` which raises an `OSError` with patch('openlp.core.common.applocation.AppLocation.get_directory', return_value=Path('/', 'app', 'dir', 'openlp')), \ patch.object(Path, 'glob', return_value=[ Path('/', 'app', 'dir', 'openlp', 'import_dir', 'file1.py')]), \ patch('openlp.core.common.importlib.import_module', side_effect=OSError()), \ patch('openlp.core.common.log') as mocked_logger: # WHEN: Calling `extension_loader` extension_loader('glob') # THEN: The `OSError` should be caught and logged assert mocked_logger.exception.called
def test_extension_loader_files_found(self): """ Test the `extension_loader` function when it successfully finds and loads some files """ # GIVEN: A mocked `Path.glob` method which returns a list of files with patch('openlp.core.common.applocation.AppLocation.get_directory', return_value=Path('/', 'app', 'dir', 'openlp')), \ patch.object(Path, 'glob', return_value=[ Path('/', 'app', 'dir', 'openlp', 'import_dir', 'file1.py'), Path('/', 'app', 'dir', 'openlp', 'import_dir', 'file2.py'), Path('/', 'app', 'dir', 'openlp', 'import_dir', 'file3.py'), Path('/', 'app', 'dir', 'openlp', 'import_dir', 'file4.py')]), \ patch('openlp.core.common.importlib.import_module') as mocked_import_module: # WHEN: Calling `extension_loader` with a list of files to exclude extension_loader('glob', ['file2.py', 'file3.py']) # THEN: `extension_loader` should only try to import the files that are matched by the blob, excluding the # files listed in the `excluded_files` argument mocked_import_module.assert_has_calls([ call('openlp.import_dir.file1'), call('openlp.import_dir.file4') ])
def bootstrap_initialise(self): """ Check to see if we have any media Player's available. """ controller_dir = os.path.join('core', 'ui', 'media') # Find all files that do not begin with '.' (lp:#1738047) and end with player.py glob_pattern = os.path.join(controller_dir, '[!.]*player.py') extension_loader(glob_pattern, ['mediaplayer.py']) player_classes = MediaPlayer.__subclasses__() for player_class in player_classes: self.register_players(player_class(self)) if not self.media_players: return False saved_players, overridden_player = get_media_players() invalid_media_players = \ [media_player for media_player in saved_players if media_player not in self.media_players or not self.media_players[media_player].check_available()] if invalid_media_players: for invalidPlayer in invalid_media_players: saved_players.remove(invalidPlayer) set_media_players(saved_players, overridden_player) self._set_active_players() self._generate_extensions_lists() return True