Coverage for src/robotide/application/pluginconnector.py: 83%

49 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

1# Copyright 2008-2015 Nokia Networks 

2# Copyright 2016- Robot Framework Foundation 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15 

16from .. import utils 1ab

17from ..context import LOG 1ab

18 

19 

20def plugin_factory(application, plugin_class): 1ab

21 try: 

22 plugin = plugin_class(application) 

23 except Exception as e: 

24 print(e) 

25 msg, traceback = utils.get_error_details() 

26 return BrokenPlugin(msg, traceback, plugin_class) 

27 else: 

28 return PluginConnector(plugin, application) 

29 

30 

31class _PluginConnector(object): 1ab

32 

33 def __init__(self, name, doc='', error=None): 1ab

34 self.name = name 

35 self.doc = doc 

36 self.error = error 

37 self.enabled = False 

38 self.metadata = {} 

39 self.config_panel = lambda meself: None 

40 

41 

42class PluginConnector(_PluginConnector): 1ab

43 

44 def __init__(self, plugin, application): 1ab

45 _PluginConnector.__init__(self, plugin.name, plugin.doc) 

46 self.conn_plugin = plugin 

47 self._settings = application.settings['Plugins'].add_section(plugin.name) 

48 self.config_panel = plugin.config_panel 

49 try: 

50 self.on_config_panel = plugin.on_config_panel 

51 except AttributeError: 

52 pass 

53 self.metadata = plugin.metadata 

54 

55 def enable_on_startup(self): 1ab

56 if self._settings.get('_enabled', self.conn_plugin.initially_enabled): 1acd

57 self.enable() 1ac

58 

59 def enable(self): 1ab

60 self._settings.set('_enabled', True) 1ac

61 self.enabled = True 1ac

62 self.conn_plugin.enable() 1ac

63 

64 def disable(self): 1ab

65 if self.enabled: 1ad

66 self._settings.set('_enabled', False) 

67 self.enabled = False 

68 self.conn_plugin.disable() 

69 

70 

71class BrokenPlugin(_PluginConnector): 1ab

72 

73 def __init__(self, error_msg, traceback, plugin_class): 1ab

74 name = utils.name_from_class(plugin_class, 'Plugin') 

75 doc = 'This plugin is disabled because it failed to load properly.\nError: ' + error_msg + '\n' + traceback 

76 _PluginConnector.__init__(self, name, doc=doc, error=error_msg) 

77 LOG.error("Taking %s plugin into use failed:\n%s" % (name, error_msg)) 

78 

79 def enable_on_startup(self): 1ab

80 """ Just ignoring it """ 

81 pass