| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
"""Dialog to run arbitrary bzr command.""" |
| 23 |
|
| 24 |
import os |
| 25 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 26 |
|
| 27 |
from breezy import osutils |
| 28 |
|
| 29 |
from breezy.plugins.qbrz.lib import MS_WINDOWS |
| 30 |
from breezy.plugins.qbrz.lib.help import get_help_topic_as_html |
| 31 |
from breezy.plugins.qbrz.lib.i18n import gettext |
| 32 |
from breezy.plugins.qbrz.lib.subprocess import SubProcessDialog |
| 33 |
from breezy.plugins.qbrz.lib.ui_run import Ui_RunDialog |
| 34 |
from breezy.plugins.qbrz.lib.util import hookup_directory_picker, shlex_split_unicode |
| 35 |
|
| 36 |
|
| 37 |
class QBzrRunDialog(SubProcessDialog): |
| 38 |
|
| 39 |
def __init__(self, command=None, parameters=None, workdir=None, category=None, ui_mode=False, parent=None, execute=False): |
| 40 |
"""Build dialog. |
| 41 |
|
| 42 |
@param command: initial command selection. |
| 43 |
@param parameters: initial options and arguments (string) for command. |
| 44 |
@param workdir: working directory to run command. |
| 45 |
@param category: initial category selection. |
| 46 |
@param ui_mode: wait after the operation is complete. |
| 47 |
@param parent: parent window. |
| 48 |
@param execute: True => run command immediately |
| 49 |
""" |
| 50 |
super(QBzrRunDialog, self).__init__(name="run", ui_mode=ui_mode, dialog=True, parent=parent) |
| 51 |
self.ui = Ui_RunDialog() |
| 52 |
self.ui.setupUi(self) |
| 53 |
if workdir is None: |
| 54 |
workdir = osutils.getcwd() |
| 55 |
self.ui.wd_edit.setText(workdir) |
| 56 |
|
| 57 |
self.set_default_help() |
| 58 |
|
| 59 |
self.ui.cmd_layout.setColumnStretch(1, 1) |
| 60 |
self.ui.cmd_layout.setColumnStretch(2, 1) |
| 61 |
|
| 62 |
self.collect_command_names() |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
categories = sorted(self.all_cmds, key=lambda k: '' if k is None else k) |
| 69 |
self.ui.cat_combobox.insertItems(0, categories) |
| 70 |
self.set_cmd_combobox(cmd_name=command) |
| 71 |
|
| 72 |
if parameters: |
| 73 |
self.ui.opt_arg_edit.setText(parameters) |
| 74 |
|
| 75 |
|
| 76 |
for w in self.make_default_layout_widgets(): |
| 77 |
self.ui.subprocess_container_layout.addWidget(w) |
| 78 |
self.process_widget.hide_progress() |
| 79 |
|
| 80 |
|
| 81 |
self.restoreSize("run", None) |
| 82 |
self.splitter = self.ui.splitter |
| 83 |
self.restoreSplitterSizes() |
| 84 |
|
| 85 |
self.ui.hidden_checkbox.stateChanged[int].connect(self.set_show_hidden) |
| 86 |
self.ui.cmd_combobox.currentIndexChanged['QString'].connect(self.set_cmd_help) |
| 87 |
self.ui.cmd_combobox.editTextChanged['QString'].connect(self.set_cmd_help) |
| 88 |
self.ui.cat_combobox.currentIndexChanged['QString'].connect(self.set_category) |
| 89 |
hookup_directory_picker(self, self.ui.browse_button, self.ui.wd_edit, gettext("Select working directory")) |
| 90 |
self.ui.directory_button.clicked.connect(self.insert_directory) |
| 91 |
self.ui.filenames_button.clicked.connect(self.insert_filenames) |
| 92 |
|
| 93 |
|
| 94 |
if category: |
| 95 |
cb = self.ui.cat_combobox |
| 96 |
index = cb.findText(category) |
| 97 |
if index >= 0: |
| 98 |
cb.setCurrentIndex(index) |
| 99 |
|
| 100 |
if execute: |
| 101 |
|
| 102 |
self.ui.run_container.hide() |
| 103 |
self.ui.help_browser.hide() |
| 104 |
|
| 105 |
|
| 106 |
self.editButton = QtWidgets.QPushButton(gettext('&Edit')) |
| 107 |
self.editButton.clicked.connect(self.enable_command_edit) |
| 108 |
|
| 109 |
|
| 110 |
self.subprocessFailed[bool].connect(self.editButton.setHidden) |
| 111 |
|
| 112 |
|
| 113 |
self.buttonbox.addButton(self.editButton, QtWidgets.QDialogButtonBox.ResetRole) |
| 114 |
|
| 115 |
|
| 116 |
self.closeButton.setHidden(True) |
| 117 |
self.okButton.setHidden(True) |
| 118 |
self.editButton.setHidden(True) |
| 119 |
|
| 120 |
|
| 121 |
self.subprocessFinished[bool].connect(self.cancelButton.setHidden) |
| 122 |
|
| 123 |
|
| 124 |
self.do_start() |
| 125 |
else: |
| 126 |
if command: |
| 127 |
self.ui.opt_arg_edit.setFocus() |
| 128 |
else: |
| 129 |
self.ui.cmd_combobox.setFocus() |
| 130 |
|
| 131 |
def enable_command_edit(self): |
| 132 |
"""Hide Edit button and make user edit fields visible""" |
| 133 |
self.editButton.setHidden(True) |
| 134 |
self.subprocessFailed[bool].disconnect(self.editButton.setHidden) |
| 135 |
self.ui.run_container.show() |
| 136 |
self.ui.help_browser.show() |
| 137 |
self.okButton.setVisible(True) |
| 138 |
|
| 139 |
def set_default_help(self): |
| 140 |
"""Set default text in help widget.""" |
| 141 |
self.ui.help_browser.setHtml("<i><small>%s</small></i>" % gettext("Help for command")) |
| 142 |
|
| 143 |
def collect_command_names(self): |
| 144 |
"""Collect names of available bzr commands.""" |
| 145 |
from breezy import commands as _mod_commands |
| 146 |
names = list(_mod_commands.all_command_names()) |
| 147 |
self.cmds_dict = dict((n, _mod_commands.get_cmd_object(n)) for n in names) |
| 148 |
|
| 149 |
builtins = _mod_commands.builtin_command_names() |
| 150 |
self.all_cmds = {'All': []} |
| 151 |
self.public_cmds = {'All': []} |
| 152 |
for name, cmd in self.cmds_dict.items(): |
| 153 |
|
| 154 |
|
| 155 |
if name in builtins: |
| 156 |
category = 'Core' |
| 157 |
else: |
| 158 |
category = cmd.plugin_name() |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
self.all_cmds['All'].append(name) |
| 163 |
self.all_cmds.setdefault(category, []).append(name) |
| 164 |
if not cmd.hidden: |
| 165 |
self.public_cmds['All'].append(name) |
| 166 |
self.public_cmds.setdefault(category, []).append(name) |
| 167 |
|
| 168 |
for category in self.all_cmds: |
| 169 |
self.all_cmds[category].sort() |
| 170 |
try: |
| 171 |
self.public_cmds[category].sort() |
| 172 |
except KeyError: |
| 173 |
|
| 174 |
pass |
| 175 |
|
| 176 |
def set_category(self, category): |
| 177 |
cmd_name = self._get_cmd_name() |
| 178 |
all = self.ui.hidden_checkbox.isChecked() |
| 179 |
category = str(category) |
| 180 |
self.set_cmd_combobox(cmd_name=cmd_name, all=all, category=category) |
| 181 |
|
| 182 |
def set_show_hidden(self, show): |
| 183 |
cmd_name = self._get_cmd_name() |
| 184 |
all = bool(show) |
| 185 |
category = str(self.ui.cat_combobox.currentText()) |
| 186 |
self.set_cmd_combobox(cmd_name=cmd_name, all=all, category=category) |
| 187 |
|
| 188 |
def set_cmd_combobox(self, cmd_name=None, all=False, category=None): |
| 189 |
"""Fill command combobox with bzr commands names. |
| 190 |
|
| 191 |
@param cmd_name: if not None, the command to initially select |
| 192 |
if it exists in the list. |
| 193 |
@param all: show all commands including hidden ones. |
| 194 |
@param category: show commands just for this category. |
| 195 |
If None, commands in all categories are shown. |
| 196 |
""" |
| 197 |
cb = self.ui.cmd_combobox |
| 198 |
cb.clear() |
| 199 |
if all: |
| 200 |
lookup = self.all_cmds |
| 201 |
else: |
| 202 |
lookup = self.public_cmds |
| 203 |
if category is None: |
| 204 |
category = 'All' |
| 205 |
cb.addItems(lookup.get(category, [])) |
| 206 |
if cmd_name is None: |
| 207 |
index = -1 |
| 208 |
else: |
| 209 |
index = cb.findText(cmd_name) |
| 210 |
if index >= 0: |
| 211 |
self.set_cmd_help(cmd_name) |
| 212 |
cb.setCurrentIndex(index) |
| 213 |
|
| 214 |
def _get_cmd_name(self): |
| 215 |
"""Return the command name.""" |
| 216 |
return str(self.ui.cmd_combobox.currentText()).strip() |
| 217 |
|
| 218 |
def set_cmd_help(self, cmd_name): |
| 219 |
"""Show help for selected command in help widget. |
| 220 |
|
| 221 |
@param cmd_name: name of command to show help. |
| 222 |
""" |
| 223 |
cmd_name = str(cmd_name) |
| 224 |
|
| 225 |
cmd_object = self.cmds_dict.get(cmd_name) |
| 226 |
if cmd_object: |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
html_utf8 = get_help_topic_as_html("commands/" + cmd_name) |
| 231 |
if isinstance(html_utf8, str): |
| 232 |
html_unicode = html_utf8 |
| 233 |
else: |
| 234 |
html_unicode = html_utf8.decode('utf-8') |
| 235 |
self.ui.help_browser.setHtml(html_unicode) |
| 236 |
else: |
| 237 |
self.set_default_help() |
| 238 |
|
| 239 |
def _get_cwd(self, default=None): |
| 240 |
"""Return selected working dir for command. |
| 241 |
|
| 242 |
@param default: if working dir is not exists then return this default |
| 243 |
value. |
| 244 |
""" |
| 245 |
cwd = str(self.ui.wd_edit.text()) |
| 246 |
if not os.path.isdir(cwd): |
| 247 |
cwd = default |
| 248 |
return cwd |
| 249 |
|
| 250 |
def _prepare_filepath(self, path): |
| 251 |
"""Ensure path is safe to insert to options/arguments command line. |
| 252 |
|
| 253 |
On Windows convert backslashes to slashes; |
| 254 |
if path contains spaces we need to quote it. |
| 255 |
@return: path string suitable to insert to command line. |
| 256 |
""" |
| 257 |
if MS_WINDOWS: |
| 258 |
path = path.replace('\\', '/') |
| 259 |
if " " in path: |
| 260 |
path = '"%s"' % path |
| 261 |
return path |
| 262 |
|
| 263 |
def insert_directory(self): |
| 264 |
"""Select existing directory and insert it to command line.""" |
| 265 |
cwd = self._get_cwd("") |
| 266 |
path = QtWidgets.QFileDialog.getExistingDirectory(self, |
| 267 |
gettext("Select path to insert"), |
| 268 |
cwd) |
| 269 |
if path: |
| 270 |
self.ui.opt_arg_edit.insert(self._prepare_filepath(str(path)) + " ") |
| 271 |
|
| 272 |
def insert_filenames(self): |
| 273 |
"""Select one or more existing files and insert them to command line.""" |
| 274 |
cwd = self._get_cwd("") |
| 275 |
filenames = QtWidgets.QFileDialog.getOpenFileNames(self, gettext("Select files to insert"), cwd)[0] |
| 276 |
for i in filenames: |
| 277 |
self.ui.opt_arg_edit.insert( |
| 278 |
self._prepare_filepath(str(i)) + " ") |
| 279 |
|
| 280 |
def validate(self): |
| 281 |
"""Validate before launch command: start command only if there is one.""" |
| 282 |
if self._get_cmd_name(): |
| 283 |
return True |
| 284 |
return False |
| 285 |
|
| 286 |
def do_start(self): |
| 287 |
"""Launch command.""" |
| 288 |
cwd = self._get_cwd() |
| 289 |
args = [self._get_cmd_name()] |
| 290 |
opt_arg = self.ui.opt_arg_edit.text() |
| 291 |
args.extend(shlex_split_unicode(opt_arg)) |
| 292 |
self.process_widget.do_start(cwd, *args) |
| 293 |
|
| 294 |
def _saveSize(self, config): |
| 295 |
SubProcessDialog._saveSize(self, config) |
| 296 |
self._saveSplitterSizes(config, self.splitter) |