From 2cee66ec06d192decd7dd8836daf7c4917b36aeb Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Tue, 24 Sep 2013 23:39:09 +0200 Subject: Fix issue with empty menus caused by 98155ef MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- lib.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib.py b/lib.py index 0918e36..4cd9ac7 100644 --- a/lib.py +++ b/lib.py @@ -9,7 +9,7 @@ from sys import path from os.path import expanduser, extsep from pydot import Dot, Edge, Node, Subgraph -__all__ = [] +REGISTRY = {} # modify/mutate BLACKLIST in order to filter some elements out # (not a beautiful, rather ad-hoc design, I know) @@ -41,9 +41,15 @@ def bl_map_nodes_edges(nodes, edges, prev_nnames=None): e.get_attributes()['style'] = 'invis' + class LibMeta(type): + def __new__(cls, name, bases, attrs): + ret = super(LibMeta, cls).__new__(cls, name, bases, attrs) + if REGISTRY.setdefault(name, ret) is not ret: + raise RuntimeError("Unexpected redefinition in REGISTRY") + return ret + def __init__(cls, name, bases, attrs): - __all__.append(name) super(LibMeta, cls).__init__(name, bases, attrs) old_init = cls.__init__ @@ -142,13 +148,13 @@ def xdot_graph(*args, **kwargs): label = widget.get_label() bl = self._kwargs.setdefault('blacklist', []) change = False - cls = globals()[label] + cls = REGISTRY[label] if widget.active and cls in bl: bl.remove(cls) bs = bases(cls) bs.remove(cls) for item in filter(lambda x: isinstance(x, gtk.CheckMenuItem) - and globals()[x.get_label()] in bs, + and REGISTRY[x.get_label()] in bs, widget.get_parent().get_children()): if not item.get_active(): item.set_active(True) @@ -218,9 +224,11 @@ def xdot_graph(*args, **kwargs): nodemenu.append(nodemenuitem) nodemenu.append(gtk.SeparatorMenuItem()) - for i in __all__: + # memoization + convenience + EdgeInvisible = REGISTRY.get('EdgeInvisible') + NodeInvisible = REGISTRY.get('NodeInvisible') + for i in REGISTRY.itervalues(): try: - i = globals()[i] if issubclass(i, LibEdge): if i in (LibEdge, EdgeInvisible): continue -- cgit