def __init__(self): self._fast = {} self._root = _Node(None, None, None) self._class_to_object_ids = {} # Will be set by the PluginManager later if done as an instance. self.pm = get_weakref(None) self._weak_self = get_weakref(self) EPModelsContainer.__init__(self)
def __init__(self, command_id, shortcut, scope, commands_manager): self.command_id = command_id self.shortcut = shortcut self.scope = scope self.commands_manager = get_weakref(commands_manager) if isinstance(shortcut, str): self._shortcut_repr = shortcut else: self._shortcut_repr = shortcut.toString()
def set_instance(self, ep, instance, context=None): if ep.__class__ in string_types: raise ValueError('Expected the actual EP class to be passed.') self._name_to_ep[ep.__name__] = ep instance.pm = get_weakref(self) instances = self._ep_to_context_to_instance.get(ep) if instances is None: instances = self._ep_to_context_to_instance[ep] = {} instances[context] = instance
def __init__(self, items, offset, max_offset, delta, container): from pyvmmonitor_core.weak_utils import WeakList from pyvmmonitor_core.weak_utils import get_weakref self._items = WeakList(items) self._offset = offset self._last_offset = 0 self._max_offset = max_offset self._delta = delta self._container = get_weakref(container)
def _init_item( item, center, radius_in_px, pen, fill_color, alpha, pixels_displacement=(0, 0), graphics_widget=None): ''' :param alpha: 255 means opaque, 0 means transparent. ''' item._center = center item._radius_in_px = radius_in_px item._pixels_displacement = pixels_displacement item._last_radius = None item._last_center = None item._last_pixels_displacement = None item._last_transform = None item._custom_hover = False item._hover_pen = None item._hover_fill_color = None item._hover_alpha = None item._hover_radius_in_px = None item._regular_pen = pen item._regular_fill_color = fill_color item._regular_alpha = alpha item._regular_radius_in_px = radius_in_px # May be changed for a function and if True is returned, the mouse press # is accepted. item.accept_mouse_press = lambda event: False item.on_enter_hover = Callback() item.on_leave_hover = Callback() item.on_mouse_press = Callback() item.on_mouse_move = Callback() item.on_mouse_release = Callback() item._delay_update = 0 if hasattr(item, 'setPen'): set_graphics_item_colors(item, pen, fill_color, alpha) assert graphics_widget is not None item._graphics_widget = get_weakref(graphics_widget) # Needed to set the real position in pixels for the radius and pixels displacement. item._update_with_graphics_widget()
def __init__(self, widget): from pyvmmonitor_core.commands_manager import create_default_commands_manager commands_manager = create_default_commands_manager() self._commands_manager = commands_manager self._qshortcuts = {} self._scope_to_widget = { IQtCommandsManager.DEFAULT_SCOPE: get_weakref(widget) } self._shortcuts_registered = set()
def stop(self): container = self._container() if container is not None: if self in container._marching_ants_handlers: container._marching_ants_handlers.discard(self) container._timer.timeout.disconnect(self._update_marching_ants_offset) from pyvmmonitor_core.weak_utils import WeakList from pyvmmonitor_core.weak_utils import get_weakref self._items = WeakList() self._container = get_weakref(None)
def get_implementations(self, ep): assert not self.exited if ep.__class__ in string_types: ep = self._name_to_ep[ep] impls = self._ep_to_impls.get(ep, []) ret = [] for impl, kwargs in impls: class_ = load_class(impl) instance = class_(**kwargs) instance.pm = get_weakref(self) ret.append(instance) return ret
def __init__(self, widget, commands_manager=None): from pyvmmonitor_core.weak_utils import get_weakref if commands_manager is None: from pyvmmonitor_core.commands_manager import create_default_commands_manager commands_manager = create_default_commands_manager() self._widget = get_weakref(widget) self._commands_manager = commands_manager self._scheme_name_to_scheme = {} self._actions = {} # Default scheme is always there self.add_shortcuts_scheme(DEFAULT_SCHEME) self._active_scheme = DEFAULT_SCHEME self._qshortcuts = {}
def ask_text_input( title, msg, initial_text='', ok_caption='Ok', cancel_caption='Cancel', parent=None, get_input_error=lambda x: '', ): ''' :return None or the text that the user provided. ''' from pyvmmonitor_qt.qt.QtWidgets import QInputDialog from pyvmmonitor_core.weak_utils import get_weakref if parent is None: from pyvmmonitor_qt.qt_utils import get_main_window parent = get_main_window() input_dialog = QInputDialog(parent) input_dialog.setWindowTitle(title) input_dialog.setLabelText(msg) input_dialog.setInputMode(QInputDialog.TextInput) input_dialog.setTextValue(initial_text) input_dialog.setOkButtonText(ok_caption) input_dialog.setCancelButtonText(cancel_caption) weak_input_dialog = get_weakref(input_dialog) def on_text_value_changed(txt): input_dialog = weak_input_dialog() input_error = get_input_error(txt) if input_error: input_dialog.setLabelText(msg + "\n" + input_error) else: input_dialog.setLabelText(msg) input_dialog.textValueChanged.connect(on_text_value_changed) on_text_value_changed(initial_text) while True: ret = input_dialog.exec_() if not ret: return None txt = input_dialog.textValue() if not get_input_error(txt): return txt
def get_instance(self, ep, context=None): ''' Creates an instance in this plugin manager: Meaning that whenever a new EP is asked in the same context it'll receive the same instance created previously (and it'll be kept alive in the plugin manager). Also, the instance will have its 'pm' attribute set to be this plugin manager. ''' if self.exited: raise AssertionError('PluginManager already exited') if ep.__class__ in string_types: ep = self._name_to_ep[ep] try: return self._ep_to_context_to_instance[ep][context] except KeyError: try: impls = self._ep_to_instance_impls[(ep, context)] except KeyError: found = False if context is not None: found = True try: impls = self._ep_to_instance_impls[(ep, None)] except KeyError: found = False if not found: if ep in self._ep_to_impls: # Registered but not a kept instance. raise NotInstanceError() else: # Not registered at all. raise NotRegisteredError() assert len(impls) == 1 impl, kwargs = impls[0] class_ = load_class(impl) instances = self._ep_to_context_to_instance.get(ep) if instances is None: instances = self._ep_to_context_to_instance[ep] = {} ret = instances[context] = class_(**kwargs) ret.pm = get_weakref(self) return ret
def test_callback(): c = Callback() class F(object): def __init__(self): self.called = None def __call__(self, b): self.called = b f = F() c.register(f.__call__) assert len(c) == 1 c(1) assert f.called == 1 f = get_weakref(f) assert f() is None c(1) assert len(c) == 0
def scroll_pos(widget, val=None): vertical_scrollbar = widget.verticalScrollBar() if not val: return vertical_scrollbar.value() else: min_val = vertical_scrollbar.minimum() max_val = vertical_scrollbar.maximum() if min_val <= val <= max_val: vertical_scrollbar.setValue(val) else: weak = get_weakref(vertical_scrollbar) # We'll wait for the range to change to apply it (i.e.: if we're restoring the # contents of a widget, it may take a while until it actually receives a range). def _on_range_changed(self, *args, **kwargs): vertical_scrollbar = weak() # Don't create a cycle if vertical_scrollbar is None: return vertical_scrollbar.rangeChanged.disconnect(_on_range_changed) vertical_scrollbar.setValue(val) vertical_scrollbar.rangeChanged.connect(_on_range_changed)
def test_system_mutex(): mutex_name = 'pyvmmonitor 11111__15' system_mutex = SystemMutex(mutex_name) assert system_mutex.get_mutex_aquired() mutex2 = SystemMutex(mutex_name) assert not mutex2.get_mutex_aquired() del mutex2 system_mutex.release_mutex() mutex3 = SystemMutex(mutex_name) assert mutex3.get_mutex_aquired() mutex3 = get_weakref(mutex3) # Garbage-collected # Calling release more times should not be an error system_mutex.release_mutex() mutex4 = SystemMutex(mutex_name) assert mutex4.get_mutex_aquired() with pytest.raises(AssertionError): SystemMutex('mutex/') # Invalid name
def set_scope_widget(self, scope, widget): self._scope_to_widget[scope] = get_weakref(widget) for shortcut in self._shortcuts_registered: if shortcut.scope == scope: self._apply_shortcut(shortcut)
def __init__(self, function): from pyvmmonitor_core.weak_utils import get_weakref self._update_method = get_weakref(function) self._disposed = False self._invalidate = False
def is_qobject_alive(obj): return shiboken2.isValid(obj) elif qt_api == 'pyqt5': import sip def is_qobject_alive(obj): return not sip.isdeleted(obj) else: def is_qobject_alive(obj): raise AssertionError('Not supported') _main_window = get_weakref(None) def set_main_window(main_window): global _main_window _main_window = weakref.ref(main_window) def get_main_window(): m = _main_window() if m is None or not is_qobject_alive(m): return None return m _ADDITIONAL_EXCEPTION_MSG = '' # Unicode
def __init__(self, qobject, method_name): assert is_qobject_alive(qobject) assert hasattr(qobject, method_name) self.qobject = get_weakref(qobject) self.method_name = method_name
def __init__(self): self.obj_id = '' self.pm = \ self.model = get_weakref(None)
def __init__(self, commands_manager, obtain_qshortcut): from pyvmmonitor_core.weak_utils import get_weakref self._shortcut_cache_key_to_commad_id_to_command_infos = {} self._commands_manager = get_weakref(commands_manager) self._obtain_qshortcut = obtain_qshortcut
def register_scope(self, scope): self._commands_manager.register_scope(scope) self._scope_to_widget[scope] = get_weakref(None)