From 7eccf40188a2326bec49d67f92ce0ca3d8919cec Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Tue, 24 Sep 2013 23:46:20 +0200 Subject: Refactor attributes handling: conciseness and flexibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- lib.py | 17 +++++++++ lib_attributes.py | 23 +++++++++++ lib_edges.py | 111 +++++++++++++++++++++++++++--------------------------- lib_nodes.py | 59 ++++++++++++++--------------- lib_subgraphs.py | 17 +++++---- 5 files changed, 134 insertions(+), 93 deletions(-) create mode 100644 lib_attributes.py diff --git a/lib.py b/lib.py index 4cd9ac7..ff40240 100644 --- a/lib.py +++ b/lib.py @@ -64,7 +64,10 @@ class LibMeta(type): map(lambda x: nnames.update(getattr(x, 'nnames')), tuple(subgraphs) + tuple(nodes) + tuple(edges)) + for akey, avalue in getattr(cls, 'defaults', {}).iteritems(): + kwargs.setdefault(akey, avalue) old_init(self, *args, **kwargs) + super(cls, self).__init__(*args, **kwargs) bl_map_nodes_edges(filter(lambda x: not bl_test(x), nodes), edges, nnames) @@ -122,6 +125,20 @@ class EdgeInvisible(LibEdge): super(EdgeInvisible, self).__init__(*args, **kwargs) +# Attributes + +class LibAttributeMeta(type): + def __getattr__(cls, what): + return (cls.__name__.lower(), what) + + +class LibAttribute(object): + __metaclass__ = LibAttributeMeta + def __new__(self, what): + return (self.__name__.lower(), what) + #return getattr(self, what) + + # # main-helpers # diff --git a/lib_attributes.py b/lib_attributes.py new file mode 100644 index 0000000..a129fe9 --- /dev/null +++ b/lib_attributes.py @@ -0,0 +1,23 @@ +# vim: set fileencoding=UTF-8: +# Copyright 2013 Red Hat, Inc. +# Author: Jan Pokorný +# Distributed under GPLv2+; generated content under CC-BY-SA 3.0 +# (to view a copy, visit http://creativecommons.org/licenses/by-sa/3.0/) +"""Attribute library incl. visual aspects""" + +# XXX: perhaps validators, unificators (symbolic colors), ... + +from lib import LibAttribute + +g = globals() +for i in ( + 'ARROWHEAD', + 'COLOR', + 'CONSTRAINT', + 'DIR', + 'FILLCOLOR', + 'FONTCOLOR', + 'LABEL', + 'SHAPE', + 'STYLE'): + g[i] = type(i, (LibAttribute,), {}) diff --git a/lib_edges.py b/lib_edges.py index c1c8201..19617eb 100644 --- a/lib_edges.py +++ b/lib_edges.py @@ -6,107 +6,108 @@ """Edge library incl. visual aspects""" from lib import LibEdge +from lib_attributes import * class DBUS(LibEdge): - def __init__(self, *args, **kwargs): - kwargs.setdefault('style', 'dotted') - super(DBUS, self).__init__(*args, **kwargs) + defaults = dict(( + STYLE.dotted, + )) class OddjobExec(DBUS): - def __init__(self, *args, **kwargs): - kwargs.setdefault('label', 'oddjob\nexec') - super(OddjobExec, self).__init__(*args, **kwargs) + defaults = dict(( + LABEL('oddjob\nexec'), + )) class HTTPS(LibEdge): - def __init__(self, *args, **kwargs): - kwargs.setdefault('style', 'bold') - super(HTTPS, self).__init__(*args, **kwargs) + defaults = dict(( + STYLE.bold, + )) -class LuciHTTPS(HTTPS): - def __init__(self, *args, **kwargs): - kwargs.setdefault('label', 'port 8084') - super(LuciHTTPS, self).__init__(*args, **kwargs) +class LuciHTTPS(HTTPS): + defaults = dict(( + LABEL('port 8084'), + )) class ApplicationSpecificProtocol(LibEdge): - def __init__(self, *args, **kwargs): - kwargs.setdefault('style', 'dashed') - super(ApplicationSpecificProtocol, self).__init__(*args, **kwargs) + defaults = dict(( + STYLE.dashed, + )) class RICCIRPC(ApplicationSpecificProtocol): - def __init__(self, *args, **kwargs): - kwargs.setdefault('label', 'port 11111') - super(RICCIRPC, self).__init__(*args, **kwargs) + defaults = dict(( + LABEL('port 11111'), + )) class SNMP(ApplicationSpecificProtocol): - def __init__(self, *args, **kwargs): - kwargs.setdefault('label', 'port 161') - super(SNMP, self).__init__(*args, **kwargs) + defaults = dict(( + LABEL('port 161'), + )) class CIM(ApplicationSpecificProtocol): - def __init__(self, *args, **kwargs): - kwargs.setdefault('label', 'port 898[89]') - super(CIM, self).__init__(*args, **kwargs) + defaults = dict(( + LABEL('port 898[89]'), + )) class Consume(LibEdge): - def __init__(self, *args, **kwargs): - kwargs.setdefault('color', 'darkgreen') - kwargs.setdefault('fontcolor', 'darkgreen') - super(Consume, self).__init__(*args, **kwargs) + defaults = dict(( + COLOR.darkgreen, + FONTCOLOR.darkgreen, + )) class ConsumeReversed(Consume): - def __init__(self, *args, **kwargs): - kwargs.setdefault('dir', 'back') - super(ConsumeReversed, self).__init__(*reversed(args), **kwargs) + defaults = dict(( + DIR.back, + )) class Produce(LibEdge): - def __init__(self, *args, **kwargs): - kwargs.setdefault('color', 'orangered') - kwargs.setdefault('fontcolor', 'orangered') - super(Produce, self).__init__(*args, **kwargs) + defaults = dict(( + COLOR.orangered, + FONTCOLOR.orangered, + )) class Delegate(LibEdge): - def __init__(self, *args, **kwargs): - kwargs.setdefault('color', 'navy') - kwargs.setdefault('fontcolor', 'navy') - #kwargs.setdefault('dir', 'both') - super(Delegate, self).__init__(*args, **kwargs) + defaults = dict(( + COLOR.navy, + FONTCOLOR.navy, + #DIR.both, + )) class FencedBy(LibEdge): - def __init__(self, *args, **kwargs): - kwargs.setdefault('color', 'red') - kwargs.setdefault('dir', 'back') - super(FencedBy, self).__init__(*args, **kwargs) + defaults = dict(( + COLOR.red, + DIR.back, + )) # peer-to-peer (1:1 hence constraint='false') class Exchange(LibEdge): - def __init__(self, *args, **kwargs): - kwargs.setdefault('color', 'saddlebrown') - kwargs.setdefault('fontcolor', 'saddlebrown') - kwargs.setdefault('dir', 'both') - kwargs.setdefault('constraint', 'false') - super(Exchange, self).__init__(*args, **kwargs) + defaults = dict(( + COLOR.saddlebrown, + FONTCOLOR.saddlebrown, + DIR.both, + CONSTRAINT.false, + )) # data bus class Databus(Exchange): - def __init__(self, *args, **kwargs): - kwargs.setdefault('color', 'saddlebrown') - kwargs.setdefault('fontcolor', 'saddlebrown') - super(Databus, self).__init__(*args, **kwargs) + defaults = dict(( + COLOR.saddlebrown, + FONTCOLOR.saddlebrown, + )) # mixing diff --git a/lib_nodes.py b/lib_nodes.py index a3e08ec..20b5c66 100644 --- a/lib_nodes.py +++ b/lib_nodes.py @@ -6,27 +6,28 @@ """Node library incl. visual aspects""" from lib import LibNode +from lib_attributes import * class Program(LibNode): - def __init__(self, *args, **kwargs): - kwargs.setdefault('shape', 'box') - kwargs.setdefault('style', 'filled') - super(Program, self).__init__(*args, **kwargs) + defaults = dict(( + SHAPE.box, + STYLE.filled, + )) class Agent(Program): - def __init__(self, *args, **kwargs): - kwargs.setdefault('fillcolor', 'lavenderblush') - kwargs.setdefault('style', 'filled') - super(Agent, self).__init__(*args, **kwargs) + defaults = dict(( + FILLCOLOR.lavenderblush, + STYLE.filled, + )) class Library(Program): - def __init__(self, *args, **kwargs): - kwargs.setdefault('fillcolor', 'lavenderblush') - kwargs.setdefault('style', 'filled') - super(Library, self).__init__(*args, **kwargs) + defaults = dict(( + FILLCOLOR.lavenderblush, + STYLE.filled, + )) class Executable(Program): @@ -34,33 +35,31 @@ class Executable(Program): class Daemon(Executable): - def __init__(self, *args, **kwargs): - kwargs.setdefault('fillcolor', 'cornsilk') - kwargs.setdefault('style', 'filled') - super(Daemon, self).__init__(*args, **kwargs) + defaults = dict(( + FILLCOLOR.cornsilk, + STYLE.filled, + )) class Artefact(LibNode): - def __init__(self, *args, **kwargs): - kwargs.setdefault('shape', 'box3d') - kwargs.setdefault('fillcolor', 'wheat') - kwargs.setdefault('style', 'filled') - super(Artefact, self).__init__(*args, **kwargs) + defaults = dict(( + SHAPE.box3d, + FILLCOLOR.wheat, + STYLE.filled, + )) class Device(LibNode): - def __init__(self, *args, **kwargs): - kwargs.setdefault('shape', 'box3d') - kwargs.setdefault('fillcolor', 'wheat') - kwargs.setdefault('style', 'filled') - super(Device, self).__init__(*args, **kwargs) + defaults = dict(( + SHAPE.box3d, + FILLCOLOR.wheat, + STYLE.filled, + )) class StorageDevice(Device): - def __init__(self, *args, **kwargs): - super(StorageDevice, self).__init__(*args, **kwargs) + pass class FenceDevice(Device): - def __init__(self, *args, **kwargs): - super(FenceDevice, self).__init__(*args, **kwargs) + pass diff --git a/lib_subgraphs.py b/lib_subgraphs.py index 9d74e42..fd233b0 100644 --- a/lib_subgraphs.py +++ b/lib_subgraphs.py @@ -7,17 +7,18 @@ """Subgraph library incl. visual aspects""" from lib import LibSubgraph +from lib_attributes import * class SubgraphImportant(LibSubgraph): - def __init__(self, *args, **kwargs): - kwargs.setdefault('fillcolor', '#eaeaea') - kwargs.setdefault('style', 'bold, filled, rounded') - super(SubgraphImportant, self).__init__(*args, **kwargs) + defaults = dict(( + FILLCOLOR('#eaeaea'), + STYLE('bold, filled, rounded'), + )) class SubgraphStandard(LibSubgraph): - def __init__(self, *args, **kwargs): - kwargs.setdefault('fillcolor', '#f5f5f5') - kwargs.setdefault('style', 'filled, rounded') - super(SubgraphStandard, self).__init__(*args, **kwargs) + defaults = dict(( + FILLCOLOR('#f5f5f5'), + STYLE('filled, rounded'), + )) -- cgit