summaryrefslogtreecommitdiffstats
path: root/lib.py
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2013-05-10 19:03:18 +0200
committerJan Pokorný <jpokorny@redhat.com>2013-05-10 19:03:18 +0200
commitdef29ba4ca5d614819c9a646199ae638955dd486 (patch)
tree8262399d41ef21aff2a2c550c13aa606d28474ad /lib.py
parent8665e2a2dfb170a38ac423d188d6869d39613185 (diff)
downloadcluster-overview-def29ba4ca5d614819c9a646199ae638955dd486.tar.gz
cluster-overview-def29ba4ca5d614819c9a646199ae638955dd486.tar.xz
cluster-overview-def29ba4ca5d614819c9a646199ae638955dd486.zip
Further modularization + XDot (gtk interactive display) support
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'lib.py')
-rw-r--r--lib.py67
1 files changed, 61 insertions, 6 deletions
diff --git a/lib.py b/lib.py
index 348bb34..778d2e3 100644
--- a/lib.py
+++ b/lib.py
@@ -6,30 +6,39 @@
# (to view a copy, visit http://creativecommons.org/licenses/by-sa/3.0/)
"""Elements library incl. visual aspects"""
+from sys import path
+from os.path import expanduser, extsep
from pydot import Dot, Edge, Node, Subgraph
__all__ = []
-BLACKLIST = ()
+
+# modify/mutate BLACKLIST in order to filter some elements out
+# (not a beautiful, rather ad-hoc design, I know)
+BLACKLIST = []
#
-# Customimzed constructors=
+# Customization via meta level
#
+def bl_test(x):
+ return \
+ not set((x.__class__,) + x.__class__.__bases__).intersection(BLACKLIST)
+
+
class LibMeta(type):
def __init__(cls, name, bases, attrs):
__all__.append(name)
super(LibMeta, cls).__init__(name, bases, attrs)
old_init = cls.__init__
- test = lambda x: not set(x.__class__.__bases__).intersection(BLACKLIST)
def new_init(self, *args, **kwargs):
subgraphs = kwargs.pop('_subgraphs', ())
nodes = kwargs.pop('_nodes', ())
edges = kwargs.pop('_edges', ())
old_init(self, *args, **kwargs)
- for s in filter(test, subgraphs): s and self.add_subgraph(s)
- for n in filter(test, nodes): n and self.add_node(n)
- for e in filter(test, edges): e and self.add_edge(e)
+ for s in filter(bl_test, subgraphs): s and self.add_subgraph(s)
+ for n in filter(bl_test, nodes): n and self.add_node(n)
+ for e in filter(bl_test, edges): e and self.add_edge(e)
cls.__init__ = new_init
@@ -217,3 +226,49 @@ class DelegateSNMP(Delegate, SNMP):
class DelegateCIM(Delegate, CIM):
pass
+
+
+#
+# main-helpers
+#
+
+def export(fnc):
+ __all__.append(fnc.__name__)
+ return fnc
+
+
+@export
+def gen_graph(graph, blacklist=(), **kwargs):
+ # can eventually restore the state
+ BLACKLIST[:] = blacklist
+ return graph()
+
+
+@export
+def xdot_graph(*args, **kwargs):
+ import gtk
+ import gtk.gdk
+ try:
+ import xdot
+ except ImportError:
+ #path.append(expanduser('~/wrkspc/goss-medium/jrfonseca.xdot'))
+ #import xdot
+ print 'missing xdot; use "pip install xdot" or equivalent'
+ raise
+ window = xdot.DotWindow()
+ window.set_dotcode(gen_graph(*args, **kwargs).to_string())
+ window.connect('destroy', gtk.main_quit)
+ gtk.main()
+
+
+@export
+def main(graph, argv, *args, **kws):
+ x, argv = (1, argv[1:]) if len(argv) > 1 and argv[1] == '-x' else (0, argv)
+ output = kws.pop('output', 'sinenomine')
+ if x:
+ xdot_graph(graph, **kws)
+ else:
+ ext = argv[1] if len(argv) > 1 else 'pdf'
+ fmt = {'dot': 'raw'}.get(ext, ext)
+ output += extsep + ext
+ gen_graph(graph, **kws).write(output, format=fmt, prog=('dot',) + args)