Fix cyclic import between utils and resources

This commit is contained in:
Romain Failliot 2021-11-21 13:29:17 -05:00
parent 4fa86dc9a6
commit 81e87eb161
4 changed files with 48 additions and 48 deletions

View File

@ -95,6 +95,7 @@ disable=raw-checker-failed,
missing-class-docstring,
missing-function-docstring,
missing-module-docstring,
no-name-in-module,
no-self-use,
too-few-public-methods,
too-many-arguments,

View File

@ -45,7 +45,7 @@ from diffuse.preferences import Preferences
from diffuse.resources import theResources
from diffuse.vcs.vcs_registry import VcsRegistry
from diffuse.widgets import FileDiffViewer
from diffuse.widgets import LINE_MODE, CHAR_MODE, ALIGN_MODE
from diffuse.widgets import createMenu, LINE_MODE, CHAR_MODE, ALIGN_MODE
theVCSs = VcsRegistry()
@ -1550,7 +1550,7 @@ def _create_menu_bar(specs, radio, accel_group):
menu_bar = Gtk.MenuBar.new()
for label, spec in specs:
menu = Gtk.MenuItem.new_with_mnemonic(label)
menu.set_submenu(utils.createMenu(spec, radio, accel_group))
menu.set_submenu(createMenu(spec, radio, accel_group))
menu.set_use_underline(True)
menu.show()
menu_bar.append(menu)

View File

@ -30,7 +30,6 @@ from gi.repository import Gtk
# pylint: enable=wrong-import-position
from diffuse import constants
from diffuse.resources import theResources
# convenience class for displaying a message dialogue
class MessageDialog(Gtk.MessageDialog):
@ -272,50 +271,6 @@ def step_adjustment(adj, delta):
v = min(v, int(adj.get_upper() - adj.get_page_size()))
adj.set_value(v)
# convenience method for creating a menu according to a template
def createMenu(specs, radio=None, accel_group=None):
menu = Gtk.Menu.new()
for spec in specs:
if len(spec) > 0:
if len(spec) > 7 and spec[7] is not None:
g, k = spec[7]
if g not in radio:
item = Gtk.RadioMenuItem.new_with_mnemonic_from_widget(None, spec[0])
radio[g] = (item, {})
else:
item = Gtk.RadioMenuItem.new_with_mnemonic_from_widget(radio[g][0], spec[0])
radio[g][1][k] = item
else:
item = Gtk.ImageMenuItem.new_with_mnemonic(spec[0])
cb = spec[1]
if cb is not None:
data = spec[2]
item.connect('activate', cb, data)
if len(spec) > 3 and spec[3] is not None:
image = Gtk.Image.new()
image.set_from_stock(spec[3], Gtk.IconSize.MENU) # pylint: disable=no-member
item.set_image(image)
if accel_group is not None and len(spec) > 4:
a = theResources.getKeyBindings('menu', spec[4])
if len(a) > 0:
key, modifier = a[0]
item.add_accelerator(
'activate',
accel_group,
key,
modifier,
Gtk.AccelFlags.VISIBLE)
if len(spec) > 5:
item.set_sensitive(spec[5])
if len(spec) > 6 and spec[6] is not None:
item.set_submenu(createMenu(spec[6], radio, accel_group))
item.set_use_underline(True)
else:
item = Gtk.SeparatorMenuItem.new()
item.show()
menu.append(item)
return menu
# masks used to indicate the presence of particular line endings
DOS_FORMAT = 1

View File

@ -1833,7 +1833,7 @@ class FileDiffViewer(Gtk.Grid):
can_swap = (f != self.current_pane)
# pylint: disable=line-too-long
menu = utils.createMenu(
menu = createMenu(
[ [_('Align with Selection'), self.align_with_selection_cb, [f, i], Gtk.STOCK_EXECUTE, None, can_align],
[_('Isolate'), self.button_cb, 'isolate', None, None, can_isolate ],
[_('Merge Selection'), self.merge_lines_cb, f, None, None, can_merge],
@ -3685,6 +3685,50 @@ class FileDiffViewer(Gtk.Grid):
def merge_from_right_then_left(self):
self._mergeBoth(True)
# convenience method for creating a menu according to a template
def createMenu(specs, radio=None, accel_group=None):
menu = Gtk.Menu.new()
for spec in specs:
if len(spec) > 0:
if len(spec) > 7 and spec[7] is not None:
g, k = spec[7]
if g not in radio:
item = Gtk.RadioMenuItem.new_with_mnemonic_from_widget(None, spec[0])
radio[g] = (item, {})
else:
item = Gtk.RadioMenuItem.new_with_mnemonic_from_widget(radio[g][0], spec[0])
radio[g][1][k] = item
else:
item = Gtk.ImageMenuItem.new_with_mnemonic(spec[0])
cb = spec[1]
if cb is not None:
data = spec[2]
item.connect('activate', cb, data)
if len(spec) > 3 and spec[3] is not None:
image = Gtk.Image.new()
image.set_from_stock(spec[3], Gtk.IconSize.MENU) # pylint: disable=no-member
item.set_image(image)
if accel_group is not None and len(spec) > 4:
a = theResources.getKeyBindings('menu', spec[4])
if len(a) > 0:
key, modifier = a[0]
item.add_accelerator(
'activate',
accel_group,
key,
modifier,
Gtk.AccelFlags.VISIBLE)
if len(spec) > 5:
item.set_sensitive(spec[5])
if len(spec) > 6 and spec[6] is not None:
item.set_submenu(createMenu(spec[6], radio, accel_group))
item.set_use_underline(True)
else:
item = Gtk.SeparatorMenuItem.new()
item.show()
menu.append(item)
return menu
ALPHANUMERIC_CLASS = 0
WHITESPACE_CLASS = 1
OTHER_CLASS = 2