Coverage for src/robotide/run/runanything.py: 79%

77 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 

16import builtins 

17import wx 

18 

19from ..controller.basecontroller import _BaseController 

20from ..pluginapi import Plugin 

21from ..action import ActionInfo, SeparatorInfo 

22from ..run.configmanagerui import ConfigManagerDialog 

23from ..run.ui import Runner 

24 

25_ = wx.GetTranslation # To keep linter/code analyser happy 

26builtins.__dict__['_'] = wx.GetTranslation 

27 

28 

29class RunAnything(Plugin): 

30 __doc__ = _("""A plugin for executing commands on the system. 

31 

32 This plugin enables creation of persistent run configurations and 

33 execution of those. Output of the executed command is displayed in a 

34 separate tab.""") 

35 

36 def __init__(self, app): 

37 Plugin.__init__(self, app, default_settings={'configs': []}) 

38 self._configs = RunConfigs(self.configs) 

39 self.runner = None 

40 

41 def enable(self): 

42 self._create_menu(self._configs) 

43 

44 def on_manage_configurations(self, event): 

45 dlg = ConfigManagerDialog(self._configs, self) 

46 if dlg.ShowModal() == wx.ID_OK: 

47 self._configs.update(dlg.get_data()) 

48 self.save_setting('configs', self._configs.data_to_save()) 

49 self._create_menu(self._configs) 

50 self._configs = RunConfigs(self.configs) 

51 dlg.Destroy() 

52 

53 def _create_menu(self, configs): 

54 self.unregister_actions() 

55 self.register_action(ActionInfo(_('Macros'), _('Manage Run Configurations'), 

56 self.on_manage_configurations)) 

57 self.register_action(SeparatorInfo(_('Macros'))) 

58 for index, cfg in enumerate(configs): 

59 self._add_config_to_menu(cfg, index+1) 

60 

61 def _add_config_to_menu(self, config, index): 

62 def run(event): 

63 self.runner = Runner(config, self.notebook) 

64 self.runner.run() 

65 info = ActionInfo(_('Macros'), name='%d: %s' % (index, config.name), 

66 doc=config.help, action=run) 

67 self.register_action(info) 

68 # self.register_shortcut('CtrlCmd-C', lambda e: self.runner.output_panel.Copy()) 

69 

70 

71class RunConfigs(_BaseController): 

72 

73 def __init__(self, saved_data): 

74 self._configs = [] 1afdkecb

75 for item in saved_data: 1afdkecb

76 self.add(item[0], item[1], item[2]) 1adecb

77 

78 def __iter__(self): 

79 return iter(self._configs) 

80 

81 def __len__(self): 

82 return len(self._configs) 1k

83 

84 def __getitem__(self, index): 

85 return self._configs[index] 1dcb

86 

87 def add(self, name, command, doc): 

88 config = RunConfig(name, command, doc) 1afdecb

89 self._configs.append(config) 1afdecb

90 return config 1afdecb

91 

92 def update(self, data): 

93 for index, datum in enumerate(data): 1b

94 self.edit(index, *datum) 1b

95 

96 def move_up(self, index): 

97 self._swap(index-1, index) 

98 

99 def move_down(self, index): 

100 self._swap(index, index+1) 

101 

102 def _swap(self, index1, index2): 

103 self._configs[index1], self._configs[index2] = self._configs[index2], self._configs[index1] 

104 

105 def edit(self, index, name, command, doc): 

106 config = self._configs[index] 1cb

107 config.name, config.command, config.doc = name, command, doc 1cb

108 

109 def delete(self, index): 

110 if index < len(self._configs): 

111 self._configs.pop(index) 

112 

113 def data_to_save(self): 

114 return [ (c.name, c.command, c.doc) for c in self._configs ] 1e

115 

116 

117class RunConfig(object): 

118 help = property(lambda self: '%s (%s)' % (self.doc, self.command)) 

119 

120 def __init__(self, name, command, doc): 

121 self.name = name 1aghijfdecb

122 self.command = command 1aghijfdecb

123 self.doc = doc 1aghijfdecb

124 self._finished = False 1aghijfdecb

125 self._error = False 1aghijfdecb

126