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
« 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.
16import builtins
17import wx
19from ..controller.basecontroller import _BaseController
20from ..pluginapi import Plugin
21from ..action import ActionInfo, SeparatorInfo
22from ..run.configmanagerui import ConfigManagerDialog
23from ..run.ui import Runner
25_ = wx.GetTranslation # To keep linter/code analyser happy
26builtins.__dict__['_'] = wx.GetTranslation
29class RunAnything(Plugin):
30 __doc__ = _("""A plugin for executing commands on the system.
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.""")
36 def __init__(self, app):
37 Plugin.__init__(self, app, default_settings={'configs': []})
38 self._configs = RunConfigs(self.configs)
39 self.runner = None
41 def enable(self):
42 self._create_menu(self._configs)
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()
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)
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())
71class RunConfigs(_BaseController):
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
78 def __iter__(self):
79 return iter(self._configs)
81 def __len__(self):
82 return len(self._configs) 1k
84 def __getitem__(self, index):
85 return self._configs[index] 1dcb
87 def add(self, name, command, doc):
88 config = RunConfig(name, command, doc) 1afdecb
89 self._configs.append(config) 1afdecb
90 return config 1afdecb
92 def update(self, data):
93 for index, datum in enumerate(data): 1b
94 self.edit(index, *datum) 1b
96 def move_up(self, index):
97 self._swap(index-1, index)
99 def move_down(self, index):
100 self._swap(index, index+1)
102 def _swap(self, index1, index2):
103 self._configs[index1], self._configs[index2] = self._configs[index2], self._configs[index1]
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
109 def delete(self, index):
110 if index < len(self._configs):
111 self._configs.pop(index)
113 def data_to_save(self):
114 return [ (c.name, c.command, c.doc) for c in self._configs ] 1e
117class RunConfig(object):
118 help = property(lambda self: '%s (%s)' % (self.doc, self.command))
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