diff options
Diffstat (limited to 'lib.py')
-rw-r--r-- | lib.py | 67 |
1 files changed, 61 insertions, 6 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) |