| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
om> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
import os |
| 22 |
import shlex |
| 23 |
import sys |
| 24 |
import itertools |
| 25 |
|
| 26 |
|
| 27 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 28 |
|
| 29 |
from breezy.revision import Revision |
| 30 |
|
| 31 |
|
| 32 |
from breezy.config import GlobalConfig |
| 33 |
|
| 34 |
from breezy import bedding |
| 35 |
from breezy import lazy_regex |
| 36 |
|
| 37 |
from breezy.plugins.qbrz.lib import MS_WINDOWS |
| 38 |
from breezy.plugins.qbrz.lib.i18n import gettext, N_ |
| 39 |
|
| 40 |
|
| 41 |
import breezy.plugins.qbrz.lib.resources |
| 42 |
|
| 43 |
from breezy import errors |
| 44 |
from functools import reduce |
| 45 |
|
| 46 |
from breezy import ( |
| 47 |
osutils, |
| 48 |
urlutils, |
| 49 |
ui, |
| 50 |
) |
| 51 |
from breezy.plugins.qbrz.lib import trace |
| 52 |
from breezy.workingtree import WorkingTree |
| 53 |
from breezy.transport import get_transport |
| 54 |
from breezy.lockdir import LockDir |
| 55 |
|
| 56 |
from breezy.plugins.qbrz.lib.compatibility import configobj |
| 57 |
|
| 58 |
|
| 59 |
BTN_OK = 0 |
| 60 |
BTN_CANCEL = 1 |
| 61 |
BTN_CLOSE = 2 |
| 62 |
BTN_HELP = 3 |
| 63 |
BTN_REFRESH = 4 |
| 64 |
|
| 65 |
|
| 66 |
class StandardButton(QtWidgets.QPushButton): |
| 67 |
|
| 68 |
__types = { |
| 69 |
BTN_OK: (N_('&OK'), 'SP_DialogOkButton'), |
| 70 |
BTN_CANCEL: (N_('&Cancel'), 'SP_DialogCancelButton'), |
| 71 |
BTN_CLOSE: (N_('&Close'), 'SP_DialogCloseButton'), |
| 72 |
BTN_HELP: (N_('&Help'), 'SP_DialogHelpButton'), |
| 73 |
BTN_REFRESH: (N_('&Refresh'), 'view-refresh'), |
| 74 |
} |
| 75 |
|
| 76 |
def __init__(self, btntype, *args): |
| 77 |
label = gettext(self.__types[btntype][0]) |
| 78 |
new_args = [label] |
| 79 |
if sys.platform != 'win32' and sys.platform != 'darwin': |
| 80 |
iconname = self.__types[btntype][1] |
| 81 |
if iconname == 'view-refresh': |
| 82 |
icon = QtGui.QIcon(':/16x16/view-refresh.png') |
| 83 |
new_args = [icon, label] |
| 84 |
elif hasattr(QtWidgets.QStyle, iconname): |
| 85 |
icon = QtWidgets.QApplication.style().standardIcon(getattr(QtWidgets.QStyle, iconname)) |
| 86 |
new_args = [icon, label] |
| 87 |
new_args.extend(args) |
| 88 |
QtWidgets.QPushButton.__init__(self, *new_args) |
| 89 |
|
| 90 |
|
| 91 |
def config_filename(): |
| 92 |
return osutils.pathjoin(bedding.config_dir(), 'qbrz.conf') |
| 93 |
|
| 94 |
|
| 95 |
class Config(object): |
| 96 |
|
| 97 |
def __init__(self, filename): |
| 98 |
self._filename = filename |
| 99 |
self._configobj = None |
| 100 |
transport = get_transport(osutils.dirname(osutils.safe_unicode(filename))) |
| 101 |
self._lock = LockDir(transport, 'lock') |
| 102 |
|
| 103 |
def _load(self): |
| 104 |
if self._configobj is not None: |
| 105 |
return |
| 106 |
self._configobj = configobj.ConfigObj(self._filename, encoding='utf-8') |
| 107 |
|
| 108 |
def set_option(self, name, value, section=None): |
| 109 |
self._load() |
| 110 |
if section is None: |
| 111 |
section = 'DEFAULT' |
| 112 |
if section not in self._configobj: |
| 113 |
self._configobj[section] = {} |
| 114 |
if value: |
| 115 |
if not isinstance(value, str): |
| 116 |
|
| 117 |
|
| 118 |
value = str(value) |
| 119 |
self._configobj[section][name] = value |
| 120 |
else: |
| 121 |
if name in self._configobj[section]: |
| 122 |
del self._configobj[section][name] |
| 123 |
|
| 124 |
def get_option(self, name, section=None): |
| 125 |
self._load() |
| 126 |
if section is None: |
| 127 |
section = 'DEFAULT' |
| 128 |
try: |
| 129 |
return self._configobj[section][name] |
| 130 |
except KeyError: |
| 131 |
return None |
| 132 |
|
| 133 |
def get_option_as_bool(self, name, section=None): |
| 134 |
|
| 135 |
|
| 136 |
value_maybe_str_or_bool = self.get_option(name, section) |
| 137 |
if value_maybe_str_or_bool not in (None, ''): |
| 138 |
value = ui.bool_from_string(value_maybe_str_or_bool) |
| 139 |
return value |
| 140 |
|
| 141 |
def set_section(self, name, values): |
| 142 |
self._load() |
| 143 |
self._configobj[name] = values |
| 144 |
|
| 145 |
def get_section(self, name): |
| 146 |
self._load() |
| 147 |
try: |
| 148 |
return self._configobj[name] |
| 149 |
except KeyError: |
| 150 |
return {} |
| 151 |
|
| 152 |
def save(self): |
| 153 |
bedding.ensure_config_dir_exists(os.path.dirname(self._filename)) |
| 154 |
self._lock.lock_write() |
| 155 |
try: |
| 156 |
self._load() |
| 157 |
f = open(self._filename, 'wb') |
| 158 |
self._configobj.write(f) |
| 159 |
f.close() |
| 160 |
finally: |
| 161 |
self._lock.unlock() |
| 162 |
|
| 163 |
|
| 164 |
class QBzrConfig(Config): |
| 165 |
|
| 166 |
def __init__(self): |
| 167 |
super(QBzrConfig, self).__init__(config_filename()) |
| 168 |
|
| 169 |
def get_bookmarks(self): |
| 170 |
section = self.get_section('BOOKMARKS') |
| 171 |
i = 0 |
| 172 |
while True: |
| 173 |
try: |
| 174 |
location = section['bookmark%d' % i] |
| 175 |
except KeyError: |
| 176 |
break |
| 177 |
name = section.get('bookmark%d_name' % i, location) |
| 178 |
i += 1 |
| 179 |
yield name, location |
| 180 |
|
| 181 |
def set_bookmarks(self, bookmarks): |
| 182 |
section = {} |
| 183 |
for i, (name, location) in enumerate(bookmarks): |
| 184 |
section['bookmark%d' % i] = location |
| 185 |
section['bookmark%d_name' % i] = name |
| 186 |
self.set_section('BOOKMARKS', section) |
| 187 |
|
| 188 |
def add_bookmark(self, name, location): |
| 189 |
bookmarks = list(self.get_bookmarks()) |
| 190 |
bookmarks.append((name, location)) |
| 191 |
self.set_bookmarks(bookmarks) |
| 192 |
|
| 193 |
def get_color(self, name, section=None): |
| 194 |
""" |
| 195 |
Get a color entry from the config file. |
| 196 |
Color entries have the syntax: |
| 197 |
name = R, G, B |
| 198 |
Where the color components are integers in the range 0..255. |
| 199 |
|
| 200 |
Colors are returned as QtGui.QColor. |
| 201 |
|
| 202 |
If input is erroneous, ErrorValue exception is raised. |
| 203 |
|
| 204 |
e.g. |
| 205 |
replace_fill = 255, 0, 128 |
| 206 |
""" |
| 207 |
|
| 208 |
if section is None: |
| 209 |
name_str = '[DEFAULT]:' + name |
| 210 |
else: |
| 211 |
name_str = "[" + section + "]:" + name |
| 212 |
|
| 213 |
color_format_err_msg = "Illegal color format for {0}. Given '{1}' expected '<red>, <green>, <blue>'." |
| 214 |
color_range_err_msg = "Color components for {0} should be in the range 0..255 only. Given: {1]." |
| 215 |
|
| 216 |
val = self.get_option(name, section) |
| 217 |
if val is None: |
| 218 |
return None |
| 219 |
|
| 220 |
if list != type(val): |
| 221 |
raise ValueError(color_format_err_msg.format(name_str, val)) |
| 222 |
if 3 != len(val) or not reduce(lambda x,y: x and y.isdigit(), val, True): |
| 223 |
raise ValueError(color_format_err_msg.format(name_str, ", ".join(val))) |
| 224 |
|
| 225 |
|
| 226 |
color_components = list(map(int, val)) |
| 227 |
if not reduce(lambda x,y: x and y < 256, color_components, True): |
| 228 |
raise ValueError(color_range_err_msg.format(name_str, ", ".join(val))) |
| 229 |
|
| 230 |
|
| 231 |
return QtGui.QColor(*color_components) |
| 232 |
|
| 233 |
|
| 234 |
_global_config = None |
| 235 |
|
| 236 |
|
| 237 |
def get_global_config(): |
| 238 |
global _global_config |
| 239 |
|
| 240 |
if _global_config is None or _check_global_config_filename_valid(_global_config): |
| 241 |
_global_config = GlobalConfig() |
| 242 |
return _global_config |
| 243 |
|
| 244 |
|
| 245 |
def _check_global_config_filename_valid(config): |
| 246 |
|
| 247 |
if hasattr(config, 'file_name'): |
| 248 |
return not config.file_name == config_filename() |
| 249 |
else: |
| 250 |
return False |
| 251 |
|
| 252 |
|
| 253 |
_qbrz_config = None |
| 254 |
|
| 255 |
|
| 256 |
def get_qbrz_config(): |
| 257 |
global _qbrz_config |
| 258 |
if _qbrz_config is None or not _qbrz_config._filename == config_filename(): |
| 259 |
_qbrz_config = QBzrConfig() |
| 260 |
return _qbrz_config |
| 261 |
|
| 262 |
|
| 263 |
def get_branch_config(branch): |
| 264 |
if branch: |
| 265 |
return branch.get_config() |
| 266 |
else: |
| 267 |
return get_global_config() |
| 268 |
|
| 269 |
|
| 270 |
class _QBzrWindowBase(object): |
| 271 |
|
| 272 |
def set_title(self, title=None): |
| 273 |
if title: |
| 274 |
if isinstance(title, str): |
| 275 |
self.setWindowTitle(title) |
| 276 |
elif isinstance(title, (list, tuple)): |
| 277 |
self.setWindowTitle(" - ".join(title)) |
| 278 |
|
| 279 |
def set_title_and_icon(self, title=None): |
| 280 |
"""Set window title (from string or list) and bzr icon""" |
| 281 |
self.set_title(title) |
| 282 |
icon = QtGui.QIcon() |
| 283 |
icon.addFile(":/bzr-16.png", QtCore.QSize(16, 16)) |
| 284 |
icon.addFile(":/bzr-32.png", QtCore.QSize(32, 32)) |
| 285 |
icon.addFile(":/bzr-48.png", QtCore.QSize(48, 48)) |
| 286 |
self.setWindowIcon(icon) |
| 287 |
|
| 288 |
def create_button_box(self, *buttons): |
| 289 |
"""Create and return button box with pseudo-standard buttons |
| 290 |
@param buttons: any from BTN_OK, BTN_CANCEL, BTN_CLOSE |
| 291 |
@return: QtGui.QDialogButtonBox with attached buttons and signals |
| 292 |
|
| 293 |
RJLRJL: modified this to not do the dictionary dance every time and |
| 294 |
to work with PyQt5 |
| 295 |
""" |
| 296 |
buttonbox = QtWidgets.QDialogButtonBox(self) |
| 297 |
if BTN_OK in buttons: |
| 298 |
btn = StandardButton(BTN_OK) |
| 299 |
buttonbox.addButton(btn, QtWidgets.QDialogButtonBox.AcceptRole) |
| 300 |
buttonbox.accepted.connect(self.do_accept) |
| 301 |
if BTN_CANCEL in buttons: |
| 302 |
btn = StandardButton(BTN_CANCEL) |
| 303 |
buttonbox.addButton(btn, QtWidgets.QDialogButtonBox.RejectRole) |
| 304 |
buttonbox.rejected.connect(self.do_reject) |
| 305 |
if BTN_CLOSE in buttons: |
| 306 |
btn = StandardButton(BTN_CLOSE) |
| 307 |
buttonbox.addButton(btn, QtWidgets.QDialogButtonBox.RejectRole) |
| 308 |
buttonbox.rejected.connect(self.do_close) |
| 309 |
return buttonbox |
| 310 |
|
| 311 |
def _saveSize(self, config): |
| 312 |
name = self._window_name |
| 313 |
is_maximized = int(self.windowState()) & QtCore.Qt.WindowMaximized != 0 |
| 314 |
if is_maximized: |
| 315 |
|
| 316 |
geom = self.normalGeometry() |
| 317 |
size = geom.width(), geom.height() |
| 318 |
else: |
| 319 |
size = self.width(), self.height() |
| 320 |
config.set_option(name + "_window_size", "%dx%d" % size) |
| 321 |
config.set_option(name + "_window_maximized", is_maximized) |
| 322 |
|
| 323 |
def saveSize(self): |
| 324 |
config = get_qbrz_config() |
| 325 |
self._saveSize(config) |
| 326 |
config.save() |
| 327 |
|
| 328 |
def restoreSize(self, name, defaultSize): |
| 329 |
self._window_name = name |
| 330 |
config = get_qbrz_config() |
| 331 |
size = config.get_option(name + "_window_size") |
| 332 |
if size: |
| 333 |
size = size.split("x") |
| 334 |
if len(size) == 2: |
| 335 |
try: |
| 336 |
size = list(map(int, size)) |
| 337 |
except ValueError: |
| 338 |
size = defaultSize |
| 339 |
else: |
| 340 |
if size[0] < 100 or size[1] < 100: |
| 341 |
size = defaultSize |
| 342 |
else: |
| 343 |
size = defaultSize |
| 344 |
if size: |
| 345 |
size = QtCore.QSize(size[0], size[1]) |
| 346 |
self.resize(size.expandedTo(self.minimumSizeHint())) |
| 347 |
self._restore_size = size |
| 348 |
|
| 349 |
is_maximized = config.get_option_as_bool(name + "_window_maximized") |
| 350 |
if is_maximized: |
| 351 |
self.setWindowState(QtCore.Qt.WindowMaximized) |
| 352 |
return config |
| 353 |
|
| 354 |
def _saveSplitterSizes(self, config, splitter): |
| 355 |
name = self._window_name |
| 356 |
sizes = ':'.join(map(str, splitter.sizes())) |
| 357 |
config.set_option(name + "_splitter_sizes", sizes) |
| 358 |
|
| 359 |
def restoreSplitterSizes(self, default_sizes=None): |
| 360 |
name = self._window_name |
| 361 |
config = get_qbrz_config() |
| 362 |
sizes = config.get_option(name + "_splitter_sizes") |
| 363 |
n = len(self.splitter.sizes()) |
| 364 |
if sizes: |
| 365 |
sizes = list(map(int, sizes.split(':'))) |
| 366 |
if len(sizes) != n: |
| 367 |
sizes = None |
| 368 |
if not sizes and default_sizes and len(default_sizes) == n: |
| 369 |
sizes = default_sizes |
| 370 |
if sizes: |
| 371 |
self.splitter.setSizes(sizes) |
| 372 |
|
| 373 |
def closeEvent(self, event): |
| 374 |
self.closing = True |
| 375 |
self.saveSize() |
| 376 |
for window in self.windows: |
| 377 |
if window.isVisible(): |
| 378 |
window.close() |
| 379 |
event.accept() |
| 380 |
|
| 381 |
|
| 382 |
def linkActivated(self, target): |
| 383 |
"""Sent by labels or other rich-text enabled widgets when a link |
| 384 |
is clicked. |
| 385 |
""" |
| 386 |
|
| 387 |
scheme, link = str(target).split(":", 1) |
| 388 |
if scheme != "bzrtopic": |
| 389 |
raise RuntimeError("unknown scheme") |
| 390 |
from breezy.plugins.qbrz.lib.help import show_help |
| 391 |
show_help(link, self) |
| 392 |
|
| 393 |
def processEvents(self, flags=QtCore.QEventLoop.AllEvents): |
| 394 |
QtCore.QCoreApplication.processEvents(flags) |
| 395 |
if self.closing: |
| 396 |
raise trace.StopException() |
| 397 |
|
| 398 |
def do_close(self): |
| 399 |
self.close() |
| 400 |
|
| 401 |
def operation_blocked(self, message): |
| 402 |
"""Use self.operation_blocked in validate methods of q-dialogs |
| 403 |
to show error message about incorrect or missing parameters. |
| 404 |
|
| 405 |
We can easily switch between show_error and show_warning |
| 406 |
inside this method if we want to change the overall qbrz behavior. |
| 407 |
""" |
| 408 |
self.show_warning(message) |
| 409 |
|
| 410 |
def show_error(self, message): |
| 411 |
QtWidgets.QMessageBox.critical(self, gettext("Error"), message) |
| 412 |
|
| 413 |
def show_warning(self, message): |
| 414 |
QtWidgets.QMessageBox.warning(self, gettext("Warning"), message) |
| 415 |
|
| 416 |
def ask_confirmation(self, message, type='question'): |
| 417 |
"""Return True if user selected Yes. |
| 418 |
Optional parameter type selects dialog type. Valid values: question, warning. |
| 419 |
""" |
| 420 |
klass = QtWidgets.QMessageBox.question |
| 421 |
if type == 'warning': |
| 422 |
klass = QtWidgets.QMessageBox.warning |
| 423 |
|
| 424 |
button = klass(self, |
| 425 |
gettext("Confirm"), |
| 426 |
message, |
| 427 |
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, |
| 428 |
QtWidgets.QMessageBox.No) |
| 429 |
if button == QtWidgets.QMessageBox.Yes: |
| 430 |
return True |
| 431 |
else: |
| 432 |
return False |
| 433 |
|
| 434 |
|
| 435 |
class QBzrWindow(QtWidgets.QMainWindow, _QBzrWindowBase): |
| 436 |
|
| 437 |
def __init__(self, title=None, parent=None, centralwidget=None, ui_mode=True): |
| 438 |
QtWidgets.QMainWindow.__init__(self, parent) |
| 439 |
self.ui_mode = ui_mode |
| 440 |
|
| 441 |
self.set_title_and_icon(title) |
| 442 |
|
| 443 |
if centralwidget is None: |
| 444 |
centralwidget = QtWidgets.QWidget(self) |
| 445 |
self.centralwidget = centralwidget |
| 446 |
self.setCentralWidget(self.centralwidget) |
| 447 |
self.windows = [] |
| 448 |
self.closing = False |
| 449 |
|
| 450 |
def show(self): |
| 451 |
QtWidgets.QMainWindow.show(self) |
| 452 |
self.raise_() |
| 453 |
|
| 454 |
|
| 455 |
class QBzrDialog(QtWidgets.QDialog, _QBzrWindowBase): |
| 456 |
|
| 457 |
def __init__(self, title=None, parent=None, ui_mode=True): |
| 458 |
self.ui_mode = ui_mode |
| 459 |
QtWidgets.QDialog.__init__(self, parent) |
| 460 |
|
| 461 |
self.set_title_and_icon(title) |
| 462 |
|
| 463 |
self.windows = [] |
| 464 |
self.closing = False |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
self.setWindowFlags(QtCore.Qt.Window) |
| 471 |
|
| 472 |
def do_accept(self): |
| 473 |
self.accept() |
| 474 |
|
| 475 |
def do_reject(self): |
| 476 |
self.reject() |
| 477 |
|
| 478 |
def reject(self): |
| 479 |
self.saveSize() |
| 480 |
QtWidgets.QDialog.reject(self) |
| 481 |
|
| 482 |
def show(self): |
| 483 |
QtWidgets.QMainWindow.show(self) |
| 484 |
self.raise_() |
| 485 |
|
| 486 |
|
| 487 |
throber_movie = None |
| 488 |
|
| 489 |
|
| 490 |
class ThrobberWidget(QtWidgets.QWidget): |
| 491 |
"""A widget that indicates activity.""" |
| 492 |
|
| 493 |
def __init__(self, parent, timeout=500): |
| 494 |
QtWidgets.QWidget.__init__(self, parent) |
| 495 |
global throber_movie |
| 496 |
if not throber_movie: |
| 497 |
throber_movie = QtGui.QMovie(":/16x16/process-working.gif") |
| 498 |
throber_movie.start() |
| 499 |
|
| 500 |
self.spinner = QtWidgets.QLabel("", self) |
| 501 |
self.spinner.setMovie(throber_movie) |
| 502 |
|
| 503 |
self.message = QtWidgets.QLabel(gettext("Loading..."), self) |
| 504 |
self.transport = QtWidgets.QLabel("", self) |
| 505 |
for widget in (self.spinner, self.message, self.transport): |
| 506 |
widget.hide() |
| 507 |
|
| 508 |
self.widgets = [] |
| 509 |
self.set_layout() |
| 510 |
self.num_show = 0 |
| 511 |
|
| 512 |
def set_layout(self): |
| 513 |
layout = QtWidgets.QHBoxLayout(self) |
| 514 |
layout.setContentsMargins(0, 0, 0, 0) |
| 515 |
|
| 516 |
layout.addWidget(self.spinner) |
| 517 |
layout.addWidget(self.message, 1) |
| 518 |
layout.addWidget(self.transport) |
| 519 |
|
| 520 |
self.widgets.append(self.spinner) |
| 521 |
self.widgets.append(self.message) |
| 522 |
self.widgets.append(self.transport) |
| 523 |
|
| 524 |
def hide(self): |
| 525 |
|
| 526 |
|
| 527 |
self.num_show -= 1 |
| 528 |
if self.num_show <= 0: |
| 529 |
self.num_show = 0 |
| 530 |
QtWidgets.QWidget.hide(self) |
| 531 |
for widget in self.widgets: |
| 532 |
widget.hide() |
| 533 |
|
| 534 |
def show(self): |
| 535 |
|
| 536 |
|
| 537 |
self.num_show += 1 |
| 538 |
QtWidgets.QWidget.show(self) |
| 539 |
for widget in self.widgets: |
| 540 |
widget.show() |
| 541 |
|
| 542 |
|
| 543 |
class ToolBarThrobberWidget(ThrobberWidget): |
| 544 |
"""A widget that indicates activity. Smaller than ThrobberWidget, designed |
| 545 |
for use on a toolbar.""" |
| 546 |
|
| 547 |
def set_layout(self): |
| 548 |
layout = QtWidgets.QHBoxLayout(self) |
| 549 |
layout.setContentsMargins(0, 0, 0, 0) |
| 550 |
layout.addWidget(self.transport) |
| 551 |
layout.addWidget(self.spinner) |
| 552 |
self.widgets.append(self.spinner) |
| 553 |
self.widgets.append(self.transport) |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
DIRECTORYPICKER_SOURCE = N_("Select Source Directory") |
| 560 |
|
| 561 |
DIRECTORYPICKER_TARGET = N_("Select Target Directory") |
| 562 |
|
| 563 |
|
| 564 |
def hookup_directory_picker(dialog, chooser, target, chooser_type): |
| 565 |
"""An inline handler that serves as a 'link' between the widgets. |
| 566 |
@param dialog: dialog window object |
| 567 |
@param chooser: usually 'Browse' button in a dialog |
| 568 |
@param target: QLineEdit or QComboBox where location will be shown |
| 569 |
@param chooser_type: caption string for directory selector dialog |
| 570 |
""" |
| 571 |
caption = gettext(chooser_type) |
| 572 |
|
| 573 |
def click_handler(dlg=dialog, chooser=chooser, target=target, caption=caption): |
| 574 |
try: |
| 575 |
|
| 576 |
getter = target.currentText |
| 577 |
setter = target.setEditText |
| 578 |
except AttributeError: |
| 579 |
|
| 580 |
getter = target.text |
| 581 |
setter = target.setText |
| 582 |
directory = str(getter()) |
| 583 |
if not os.path.isdir(directory): |
| 584 |
directory = "" |
| 585 |
directory = QtWidgets.QFileDialog.getExistingDirectory(dlg, caption, directory) |
| 586 |
if directory: |
| 587 |
setter(directory) |
| 588 |
|
| 589 |
chooser.clicked.connect(click_handler) |
| 590 |
|
| 591 |
|
| 592 |
def open_browser(url): |
| 593 |
try: |
| 594 |
import webbrowser |
| 595 |
open_func = webbrowser.open |
| 596 |
except ImportError: |
| 597 |
try: |
| 598 |
open_func = os.startfile |
| 599 |
except AttributeError: |
| 600 |
open_func = lambda x: None |
| 601 |
open_func(url) |
| 602 |
|
| 603 |
|
| 604 |
_extract_name_re = lazy_regex.lazy_compile('(.*?) <.*?@.*?>') |
| 605 |
_extract_email_re = lazy_regex.lazy_compile('<(.*?@.*?)>') |
| 606 |
|
| 607 |
|
| 608 |
def extract_name(author, strict=False): |
| 609 |
m = _extract_name_re.match(author) |
| 610 |
if m: |
| 611 |
name = m.group(1) |
| 612 |
else: |
| 613 |
if strict: |
| 614 |
name = author |
| 615 |
else: |
| 616 |
m = _extract_email_re.match(author) |
| 617 |
if m: |
| 618 |
name = m.group(1) |
| 619 |
else: |
| 620 |
name = author |
| 621 |
return name.strip() |
| 622 |
|
| 623 |
|
| 624 |
def format_timestamp(timestamp): |
| 625 |
"""Returns unicode string representation of timestamp |
| 626 |
formatted in user locale""" |
| 627 |
date = QtCore.QDateTime() |
| 628 |
date.setTime_t(int(timestamp)) |
| 629 |
|
| 630 |
return str(date.toString(QtCore.Qt.LocalDate)) |
| 631 |
|
| 632 |
|
| 633 |
def is_valid_encoding(encoding): |
| 634 |
import codecs |
| 635 |
try: |
| 636 |
codecs.lookup(encoding) |
| 637 |
except LookupError: |
| 638 |
return False |
| 639 |
return True |
| 640 |
|
| 641 |
|
| 642 |
def get_set_encoding(encoding, branch): |
| 643 |
"""Return encoding value from branch config if encoding is None, |
| 644 |
otherwise store encoding value in branch config. |
| 645 |
""" |
| 646 |
if encoding is None: |
| 647 |
config = get_branch_config(branch) |
| 648 |
encoding = config.get_user_option("encoding") or 'utf-8' |
| 649 |
if not is_valid_encoding(encoding): |
| 650 |
from breezy.trace import note |
| 651 |
note(('NOTE: Invalid encoding value in branch config: %s\n' |
| 652 |
'utf-8 will be used instead') % encoding) |
| 653 |
encoding = 'utf-8' |
| 654 |
else: |
| 655 |
if branch: |
| 656 |
branch.get_config().set_user_option("encoding", encoding) |
| 657 |
return encoding |
| 658 |
|
| 659 |
|
| 660 |
def file_extension(path): |
| 661 |
"""Return extension of the file. |
| 662 |
This function is smarter than standard os.path.splitext, |
| 663 |
because it correctly process filenames with leading dot. |
| 664 |
(e.g. ".bzrignore") |
| 665 |
""" |
| 666 |
basename = os.path.basename(path) |
| 667 |
ix = basename.rfind('.') |
| 668 |
if ix > 0: |
| 669 |
ext = basename[ix:] |
| 670 |
else: |
| 671 |
ext = '' |
| 672 |
return ext |
| 673 |
|
| 674 |
|
| 675 |
class FilterOptions(object): |
| 676 |
"""Filter options container.""" |
| 677 |
|
| 678 |
__slots__ = ['deleted', 'added', 'renamed', 'modified'] |
| 679 |
|
| 680 |
def __init__(self, all_enable=False, **kw): |
| 681 |
self.added = False |
| 682 |
self.deleted = False |
| 683 |
self.modified = False |
| 684 |
self.renamed = False |
| 685 |
if all_enable: |
| 686 |
self.added = True |
| 687 |
self.deleted = True |
| 688 |
self.modified = True |
| 689 |
self.renamed = True |
| 690 |
for k in kw: |
| 691 |
setattr(self, k, kw[k]) |
| 692 |
|
| 693 |
def all_enable(self): |
| 694 |
for i in self.__slots__: |
| 695 |
setattr(self, i, True) |
| 696 |
|
| 697 |
def __bool__(self): |
| 698 |
return self.added or self.deleted or self.modified or self.renamed |
| 699 |
|
| 700 |
def is_all_enable(self): |
| 701 |
return self.added and self.deleted and self.modified and self.renamed |
| 702 |
|
| 703 |
def to_str(self): |
| 704 |
s = [] |
| 705 |
if self.deleted: |
| 706 |
s.append(gettext('deleted files')) |
| 707 |
if self.added: |
| 708 |
s.append(gettext('added files')) |
| 709 |
if self.renamed: |
| 710 |
s.append(gettext('renamed files')) |
| 711 |
if self.modified: |
| 712 |
s.append(gettext('modified files')) |
| 713 |
return ', '.join(s) |
| 714 |
|
| 715 |
def check(self, status): |
| 716 |
"""Check status (string) and return True if enabled. |
| 717 |
Allowed statuses: |
| 718 |
added, removed, deleted, renamed, modified, 'renamed and modified' |
| 719 |
|
| 720 |
@raise ValueError: when unsupported status given. |
| 721 |
""" |
| 722 |
if status == 'added': |
| 723 |
return self.added |
| 724 |
elif status in ('removed', 'deleted'): |
| 725 |
return self.deleted |
| 726 |
elif status == 'renamed': |
| 727 |
return self.renamed |
| 728 |
elif status == 'modified': |
| 729 |
return self.modified |
| 730 |
elif status == 'renamed and modified': |
| 731 |
return self.renamed or self.modified |
| 732 |
raise ValueError('unknown status: %r' % status) |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
def iter_branch_related_locations(branch): |
| 751 |
for location in [branch.get_parent(), |
| 752 |
branch.get_bound_location(), |
| 753 |
branch.get_push_location(), |
| 754 |
branch.get_submit_branch(), |
| 755 |
]: |
| 756 |
if location is not None: |
| 757 |
yield url_for_display(location) |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
def fill_pull_combo(combo, branch): |
| 762 |
if branch is None: |
| 763 |
p = '' |
| 764 |
related = [] |
| 765 |
else: |
| 766 |
p = url_for_display(branch.get_parent() or '') |
| 767 |
related = iter_branch_related_locations(branch) |
| 768 |
fill_combo_with(combo, p, related, iter_saved_pull_locations()) |
| 769 |
return p |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
def fill_combo_with(combo, default, *iterables): |
| 775 |
done = set() |
| 776 |
for item in itertools.chain([default], *iterables): |
| 777 |
if item is not None and item not in done: |
| 778 |
done.add(item) |
| 779 |
combo.addItem(item) |
| 780 |
|
| 781 |
|
| 782 |
def show_shortcut_hint(action): |
| 783 |
"""Show this action's shortcut, if any, as part of the tooltip. |
| 784 |
|
| 785 |
Make sure to set the shortcut and tooltip *before* calling this. |
| 786 |
""" |
| 787 |
shortcut = action.shortcut() |
| 788 |
if shortcut: |
| 789 |
toolTip = action.toolTip() |
| 790 |
action.setToolTip("%s (%s)" % (toolTip, shortcut)) |
| 791 |
|
| 792 |
|
| 793 |
def iter_saved_pull_locations(): |
| 794 |
""" Iterate the 'pull' locations we have previously saved for the user. |
| 795 |
""" |
| 796 |
config = get_qbrz_config() |
| 797 |
try: |
| 798 |
sect = config.get_section('Pull Locations') |
| 799 |
except KeyError: |
| 800 |
return [] |
| 801 |
items = sorted(sect.items()) |
| 802 |
return [i[1] for i in items] |
| 803 |
|
| 804 |
|
| 805 |
def save_pull_location(branch, location): |
| 806 |
""" Helper to optionally save the 'pull' location a user specified for |
| 807 |
a branch. Uses an MRU scheme to avoid runaway growth in the saved locations |
| 808 |
and keeping the most relevant locations at the top. |
| 809 |
|
| 810 |
The location is *not* saved if: |
| 811 |
|
| 812 |
* It is related to a branch (ie, the parent) |
| 813 |
* It is a directory |
| 814 |
""" |
| 815 |
if branch is not None and location in iter_branch_related_locations(branch): |
| 816 |
return |
| 817 |
if os.path.isdir(location): |
| 818 |
return |
| 819 |
existing = list(iter_saved_pull_locations()) |
| 820 |
try: |
| 821 |
existing.remove(location) |
| 822 |
except ValueError: |
| 823 |
pass |
| 824 |
existing.insert(0, location) |
| 825 |
|
| 826 |
max_items = 20 |
| 827 |
existing = existing[:max_items] |
| 828 |
|
| 829 |
config = get_qbrz_config() |
| 830 |
|
| 831 |
section = {} |
| 832 |
for i, save_location in enumerate(existing): |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
key = "%04d" % i |
| 837 |
section[key] = save_location |
| 838 |
config.set_section('Pull Locations', section) |
| 839 |
config.save() |
| 840 |
|
| 841 |
|
| 842 |
def url_for_display(url): |
| 843 |
"""Return human-readable URL or local path for file:/// URLs. |
| 844 |
Wrapper around breezy.urlutils.unescape_for_display |
| 845 |
""" |
| 846 |
if not url: |
| 847 |
return url |
| 848 |
return urlutils.unescape_for_display(url, 'utf-8') |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
def content_seems_to_be_binary(lines: list) -> bool: |
| 854 |
""" |
| 855 |
Check list of lines for any binary content at all (i.e. presence of 0x00 byte there). |
| 856 |
@return: True if 0x00 byte found: we have to hope we haven't got binary data |
| 857 |
that hasn't got a null-byte in it. |
| 858 |
""" |
| 859 |
for line in lines: |
| 860 |
if isinstance(line, bytes) and b'\x00' in line: |
| 861 |
return True |
| 862 |
return False |
| 863 |
|
| 864 |
|
| 865 |
class BackgroundJob(object): |
| 866 |
|
| 867 |
def __init__(self, parent): |
| 868 |
self.is_running = False |
| 869 |
self.stoping = False |
| 870 |
self.parent = parent |
| 871 |
self.restart_timeout = None |
| 872 |
|
| 873 |
def run(self): |
| 874 |
pass |
| 875 |
|
| 876 |
def run_wrapper(self): |
| 877 |
try: |
| 878 |
self.run() |
| 879 |
except: |
| 880 |
self.parent.report_exception() |
| 881 |
self.is_running = False |
| 882 |
|
| 883 |
if self.restart_timeout: |
| 884 |
self.start(self.restart_timeout) |
| 885 |
|
| 886 |
def restart(self, timeout=0): |
| 887 |
self.restart_timeout = timeout |
| 888 |
raise trace.StopException() |
| 889 |
|
| 890 |
def start(self, timeout=0): |
| 891 |
if not self.is_running: |
| 892 |
self.is_running = True |
| 893 |
self.stoping = False |
| 894 |
QtCore.QTimer.singleShot(timeout, self.run_wrapper) |
| 895 |
|
| 896 |
def stop(self): |
| 897 |
self.stoping = True |
| 898 |
|
| 899 |
def processEvents(self, flags=QtCore.QEventLoop.AllEvents): |
| 900 |
self.parent.processEvents(flags) |
| 901 |
if self.stoping: |
| 902 |
self.stoping = False |
| 903 |
raise trace.StopException() |
| 904 |
|
| 905 |
|
| 906 |
loading_queue = None |
| 907 |
|
| 908 |
|
| 909 |
def runs_in_loading_queue(f): |
| 910 |
"""Methods decorated with this will not run at the same time, but will be |
| 911 |
queued. Methods decorated with this will not be able to return results, |
| 912 |
but should rather update the ui themselves. Methods decorated with this |
| 913 |
should detect, and stop if their execution is no longer required. |
| 914 |
|
| 915 |
""" |
| 916 |
|
| 917 |
def decorate(*args, **kargs): |
| 918 |
run_in_loading_queue(f, *args, **kargs) |
| 919 |
|
| 920 |
return decorate |
| 921 |
|
| 922 |
|
| 923 |
def run_in_loading_queue(cur_f, *cur_args, **cur_kargs): |
| 924 |
global loading_queue |
| 925 |
if loading_queue is None: |
| 926 |
loading_queue = [] |
| 927 |
try: |
| 928 |
loading_queue.append((cur_f, cur_args, cur_kargs)) |
| 929 |
|
| 930 |
while len(loading_queue): |
| 931 |
try: |
| 932 |
f, args, kargs = loading_queue.pop(0) |
| 933 |
f(*args, **kargs) |
| 934 |
except: |
| 935 |
trace.report_exception() |
| 936 |
finally: |
| 937 |
loading_queue = None |
| 938 |
else: |
| 939 |
loading_queue.append((cur_f, cur_args, cur_kargs)) |
| 940 |
|
| 941 |
|
| 942 |
def get_apparent_authors_new(rev): |
| 943 |
return rev.get_apparent_authors() |
| 944 |
|
| 945 |
|
| 946 |
def get_apparent_authors_old(rev): |
| 947 |
return [rev.properties.get('author', rev.committer)] |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
if hasattr(Revision, 'get_apparent_authors'): |
| 952 |
get_apparent_authors = get_apparent_authors_new |
| 953 |
else: |
| 954 |
get_apparent_authors = get_apparent_authors_old |
| 955 |
|
| 956 |
|
| 957 |
def get_apparent_author(rev): |
| 958 |
return ', '.join(get_apparent_authors(rev)) |
| 959 |
|
| 960 |
|
| 961 |
def get_apparent_author_name(rev): |
| 962 |
return ', '.join(map(extract_name, get_apparent_authors(rev))) |
| 963 |
|
| 964 |
|
| 965 |
def get_summary(rev): |
| 966 |
if rev.message is None: |
| 967 |
return gettext('(no message)') |
| 968 |
return rev.get_summary() or gettext('(no message)') |
| 969 |
|
| 970 |
|
| 971 |
def get_message(rev): |
| 972 |
return rev.message or gettext('(no message)') |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
def ensure_unicode(s, encoding='ascii'): |
| 977 |
|
| 978 |
return s |
| 979 |
|
| 980 |
|
| 981 |
def open_tree(directory, ui_mode=False, _critical_dialog=QtWidgets.QMessageBox.critical): |
| 982 |
"""Open working tree with its root at specified directory or above |
| 983 |
(similar to WorkingTree.open_containing). |
| 984 |
If there is no working tree and ui_mode is True then show GUI dialog |
| 985 |
with error message and None will be returned. Otherwise errors |
| 986 |
(NotBranchError or NoWorkingTree) will be propagated to caller. |
| 987 |
|
| 988 |
If directory is None then current directory will be used. |
| 989 |
|
| 990 |
@param _critical_dialog: could be used to provide mock object for testing. |
| 991 |
""" |
| 992 |
if directory is None: |
| 993 |
directory = '.' |
| 994 |
try: |
| 995 |
return WorkingTree.open_containing(directory)[0] |
| 996 |
except errors.NotBranchError: |
| 997 |
if ui_mode: |
| 998 |
_critical_dialog(None, gettext("Error"), gettext('Not a branch "%s"') % os.path.abspath(directory), gettext('&Close')) |
| 999 |
return None |
| 1000 |
else: |
| 1001 |
raise |
| 1002 |
except errors.NoWorkingTree: |
| 1003 |
if ui_mode: |
| 1004 |
_critical_dialog(None, gettext("Error"), gettext('No working tree exists for "%s"') % os.path.abspath(directory), gettext('&Close')) |
| 1005 |
return None |
| 1006 |
else: |
| 1007 |
raise |
| 1008 |
|
| 1009 |
|
| 1010 |
def launchpad_project_from_url(url): |
| 1011 |
"""If url is a Launchpad code URL, get the project name. |
| 1012 |
|
| 1013 |
@return: project name or None |
| 1014 |
""" |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
DISTROS = ('debian', 'ubuntu') |
| 1026 |
from urllib.parse import urlsplit |
| 1027 |
scheme, host, path = urlsplit(url)[:3] |
| 1028 |
|
| 1029 |
if (host in ('bazaar.launchpad.net', |
| 1030 |
'bazaar.launchpad.dev', |
| 1031 |
'bazaar.qastaging.launchpad.net', |
| 1032 |
'bazaar.staging.launchpad.net')): |
| 1033 |
parts = path.strip('/').split('/') |
| 1034 |
if parts[0].startswith('~'): |
| 1035 |
if len(parts) == 3 and parts[1] not in DISTROS: |
| 1036 |
|
| 1037 |
return parts[1] |
| 1038 |
elif len(parts) == 5 and parts[1] in DISTROS: |
| 1039 |
|
| 1040 |
return parts[-2] |
| 1041 |
elif parts[0] in ('%2Bbranch', '+branch'): |
| 1042 |
parts_length = len(parts) |
| 1043 |
if parts_length >= 2: |
| 1044 |
part1 = parts[1] |
| 1045 |
if parts_length in (2,3) and part1 not in DISTROS: |
| 1046 |
|
| 1047 |
|
| 1048 |
return part1 |
| 1049 |
elif parts_length in (3,4) and part1 in DISTROS: |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
return parts[-1] |
| 1054 |
return None |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
def _shlex_split_unicode_linux(unicode_string): |
| 1060 |
"""Split unicode string to list of unicode arguments.""" |
| 1061 |
return [p for p in shlex.split(unicode_string)] |
| 1062 |
|
| 1063 |
|
| 1064 |
def _shlex_split_unicode_windows(unicode_string): |
| 1065 |
"""Split unicode string to list of unicode arguments. |
| 1066 |
Take care about backslashes as valid path separators. |
| 1067 |
""" |
| 1068 |
utf8_string = unicode_string.replace('\\', '\\\\') |
| 1069 |
return [p for p in shlex.split(utf8_string)] |
| 1070 |
|
| 1071 |
|
| 1072 |
if MS_WINDOWS: |
| 1073 |
shlex_split_unicode = _shlex_split_unicode_windows |
| 1074 |
else: |
| 1075 |
shlex_split_unicode = _shlex_split_unicode_linux |
| 1076 |
|
| 1077 |
|
| 1078 |
def get_icon(name, size=22): |
| 1079 |
|
| 1080 |
|
| 1081 |
return QtGui.QIcon(":/%dx%d/%s.png" % (size, size, name)) |
| 1082 |
|
| 1083 |
|
| 1084 |
class InfoWidget(QtWidgets.QFrame): |
| 1085 |
def __init__(self, parent=None): |
| 1086 |
QtWidgets.QFrame.__init__(self, parent) |
| 1087 |
self.setFrameShape(QtWidgets.QFrame.StyledPanel) |
| 1088 |
|
| 1089 |
self.setAutoFillBackground(True) |
| 1090 |
self.setBackgroundRole(QtGui.QPalette.ToolTipBase) |
| 1091 |
self.setForegroundRole(QtGui.QPalette.ToolTipText) |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
monospace_font = None |
| 1099 |
|
| 1100 |
|
| 1101 |
def get_monospace_font(): |
| 1102 |
global monospace_font |
| 1103 |
if monospace_font is None: |
| 1104 |
monospace_font = _get_monospace_font() |
| 1105 |
return monospace_font |
| 1106 |
|
| 1107 |
|
| 1108 |
def _get_monospace_font(): |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
size = QtWidgets.QApplication.font().pointSize() |
| 1115 |
|
| 1116 |
for font_family in ("Monospace", "Courier New"): |
| 1117 |
font = QtGui.QFont(font_family, size) |
| 1118 |
|
| 1119 |
if QtGui.QFontInfo(font).fixedPitch(): |
| 1120 |
return font |
| 1121 |
|
| 1122 |
|
| 1123 |
font = QtGui.QFont("", size) |
| 1124 |
font.setFixedPitch(True) |
| 1125 |
return font |
| 1126 |
|
| 1127 |
|
| 1128 |
def get_set_tab_width_chars(branch=None, tab_width_chars=None): |
| 1129 |
"""Function to get the tab width in characters from the configuration. |
| 1130 |
|
| 1131 |
@param branch: Use branch.conf as well as bazaar.conf if this is provided. |
| 1132 |
@param tab_width_chars: Number of characters to use as tab width: if branch |
| 1133 |
is provided, the tab width will be stored in branch.conf |
| 1134 |
|
| 1135 |
Both arguments are optional, but if tab_width_chars is provided and branch is |
| 1136 |
not, nothing will be done. |
| 1137 |
|
| 1138 |
@return: Tab width, in characters. |
| 1139 |
""" |
| 1140 |
if tab_width_chars is None: |
| 1141 |
config = get_branch_config(branch) |
| 1142 |
try: |
| 1143 |
tab_width_chars = int(config.get_user_option('tab_width')) |
| 1144 |
if tab_width_chars < 0: |
| 1145 |
raise TypeError("Invalid tab width") |
| 1146 |
except TypeError: |
| 1147 |
tab_width_chars = 8 |
| 1148 |
else: |
| 1149 |
if branch: |
| 1150 |
branch.get_config().set_user_option("tab_width", str(tab_width_chars)) |
| 1151 |
|
| 1152 |
return tab_width_chars |
| 1153 |
|
| 1154 |
|
| 1155 |
def get_tab_width_pixels(branch=None, tab_width_chars=None): |
| 1156 |
"""Function to get the tab width in pixels based on a monospaced font. |
| 1157 |
|
| 1158 |
If tab_width_chars is provided, it is simply converted to a value in pixels. If |
| 1159 |
it is not provided, the configuration is retrieved from bazaar.conf. If branch is |
| 1160 |
provided (and tab_width_chars is not), branch.conf is also checked. |
| 1161 |
|
| 1162 |
@param tab_width_chars: Number of characters of tab width to convert to pixels. |
| 1163 |
@param branch: Branch to use when retrieving tab width from configuration. |
| 1164 |
|
| 1165 |
@return: Tab width, in pixels. |
| 1166 |
""" |
| 1167 |
monospacedFont = get_monospace_font() |
| 1168 |
char_width = QtGui.QFontMetrics(monospacedFont).width(" ") |
| 1169 |
if tab_width_chars is None: |
| 1170 |
tab_width_chars = get_set_tab_width_chars(branch=branch) |
| 1171 |
return char_width*tab_width_chars |