diff options
author | Jan Pokorný <jpokorny@redhat.com> | 2013-05-10 19:03:18 +0200 |
---|---|---|
committer | Jan Pokorný <jpokorny@redhat.com> | 2013-05-10 19:03:18 +0200 |
commit | def29ba4ca5d614819c9a646199ae638955dd486 (patch) | |
tree | 8262399d41ef21aff2a2c550c13aa606d28474ad | |
parent | 8665e2a2dfb170a38ac423d188d6869d39613185 (diff) | |
download | cluster-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>
-rw-r--r-- | lib.py | 67 | ||||
-rwxr-xr-x | overview.py | 10 |
2 files changed, 66 insertions, 11 deletions
@@ -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) diff --git a/overview.py b/overview.py index 70f20f3..9dc90f9 100755 --- a/overview.py +++ b/overview.py @@ -17,16 +17,18 @@ conveniently using XDot: http://code.google.com/p/jrfonseca/wiki/XDot # TODO: integrate ^^^? http://code.google.com/p/jrfonseca/wiki/XDot#Embedding from sys import argv -from os.path import extsep, splitext +from os.path import splitext from lib import * FONT = 'Inconsolata' FONTDEF = map(lambda switch: '-' + switch + 'fontname=' + FONT, "GNE") +BLACKLIST = () +#BLACKLIST = (Daemon,) OUTPUT = splitext(__file__)[0] -graph = lambda BLACKLIST=(): \ +graph = lambda: \ LibDot('Cluster.conf-related tools', overlap='scalexy', compound='true', @@ -164,6 +166,4 @@ LibDot('Cluster.conf-related tools', if __name__ == '__main__': - ext = argv[1] if len(argv) > 1 else 'pdf' - fmt = {'dot': 'raw'}.get(ext, ext) - graph((DBUS)).write(OUTPUT + extsep + ext, format=fmt, prog=['dot'] + FONTDEF) + main(graph, argv, *FONTDEF, blacklist=BLACKLIST, output=OUTPUT) |