# HG changeset patch # User volker # Date 2015-06-23 22:37:14 # Node ID 7aab6dba2515be1f6d63f8214e5718ab62aa5d52 # Parent 49f72f9d4cc94e1b91a2da34fe302cf912034caf Added ZCA Version of the code diff --git a/src/graph/interfaces.py b/src/graph/interfaces.py --- a/src/graph/interfaces.py +++ b/src/graph/interfaces.py @@ -11,8 +11,12 @@ class IBaseAdapter(Interface): Gets an Object to be adapted """ +class IBaseComponent(Interface): + """ + All Interfaces derived from this class are marking a Class to be a component of this framework. + """ -class IAxis(Interface): +class IAxis(IBaseComponent): """ Axis interface """ @@ -21,17 +25,17 @@ class IAxis(Interface): createed with knowledge of its parent component """ -class ITickLabelAdapter(Interface): +class ITickLabelAdapter(IBaseComponent): """ An adapter that produces tick labels for an axis """ -class IViewBox(Interface): +class IViewBox(IBaseComponent): """ Container to display some components """ -class IImage(Interface): +class IImage(IBaseComponent): """ Some xy data """ diff --git a/src/graph/__init__.py b/src/graph_ZCA/__init__.py copy from src/graph/__init__.py copy to src/graph_ZCA/__init__.py diff --git a/src/graph/app.py b/src/graph_ZCA/app.py copy from src/graph/app.py copy to src/graph_ZCA/app.py --- a/src/graph/app.py +++ b/src/graph_ZCA/app.py @@ -1,13 +1,17 @@ -from zope.interface import Interface + from zope.interface import implements -from graph.components import BaseComponent, Image -from graph.interfaces import IAxis, IViewBox +from graph_ZCA.components import BaseComponent, Image, Axis, AxisComponent, ViewBox +from graph_ZCA.interfaces import IAxis, IViewBox, IBaseComponent + +from zope.component import getGlobalSiteManager, adapts + +gsm = getGlobalSiteManager() -class IPlotImage(Interface): +class IPlotImage(IBaseComponent): """ - Componante to plot a 2d graph of a given image + Component to plot a 2d graph of a given image """ @@ -17,31 +21,39 @@ class PlotImage( BaseComponent): """ implements(IPlotImage) - def __init__(self, parent, image): + def __init__(self, parent): super(PlotImage, self).__init__(parent) - self.image = image - self.x_axis = IAxis(self) - self.x_axis.orientation='bottom' - self.x_axis.start=0 - self.x_axis.end=8 + + self.vb = ViewBox(self)() - self.y_axis = IAxis(self) - self.y_axis.orientation='left' - self.y_axis.start=-5 - self.y_axis.end=3 + x_axis = Axis(self.vb)() + x_axis.orientation='bottom' + x_axis.start=0 + x_axis.end=8 - self.vb = IViewBox(self) + y_axis = Axis(self.vb)() + y_axis.orientation='left' + y_axis.start=-5 + y_axis.end=3 - self.vb.addItem( self.x_axis ) - self.vb.addItem( self.y_axis ) - self.vb.addItem( self.image ) + + self.vb.addItem( x_axis ) + self.vb.addItem( y_axis ) def render(self): return self.vb.render() +class NewAxisComponent(AxisComponent): + adapts(IViewBox) -image = Image(None, None) -plot = PlotImage( None, image) +gsm.registerAdapter(NewAxisComponent) + +image_data = [1,2,3,4] + +plot = PlotImage( None) +image = Image(plot.vb)(image_data) +plot.vb.addItem(image) + print( plot.render()) diff --git a/src/graph_ZCA/base.py b/src/graph_ZCA/base.py new file mode 100644 --- /dev/null +++ b/src/graph_ZCA/base.py @@ -0,0 +1,82 @@ +from zope.interface.interfaces import ComponentLookupError + +from zope.interface import providedBy +from zope.component import getGlobalSiteManager, getMultiAdapter + +from graph.interfaces import IBaseComponent + + +gsm = getGlobalSiteManager() + + +class BaseAdapter(object): + """ + Base for all Adapters + """ +# implements(IBaseAdapter) + + def __init__(self, obj): + """ + Gets an Object to be adapted + """ + self.obj = obj + +class BaseComponent(object): + """ + """ + + def __init__(self, parent): + self.parent = parent + self.items = [] + + def addItem(self, item): + self.items.append(item) + + def setup(self, *args, **kwargs): + pass + +class BaseFactory(object): + def __init__(self, obj, *args, **kwargs): + """ + Gets an Object to be adapted + """ + self.obj = obj + self.args = args + self.kwargs = kwargs + + def __call__(self, *args, **kwargs): + parent_vector = self.traverse_parents() + adapter = self.find_adapter(parent_vector) + adapter.setup(*args, **kwargs) + return adapter + + def find_adapter(self, parent_vector): + for i in range(len(parent_vector)): + try: + adapter = getMultiAdapter(parent_vector[i:], self.target_interface) + except ComponentLookupError: + pass + else: + if adapter : + return adapter + + def traverse_parents(self): + """ + Retrieve the vector of all component parents up to the top level component which has no parent + """ + parent_vector = [self.obj] + obj = self.obj + while obj.parent: + obj = obj.parent + parent_vector.append(obj) + parent_vector.reverse() + return parent_vector + + def getComponentInterfaceOf(self, obj): + """ + Retrieve the interfacess a certain instance is providing. + Filter out the interface that belongs to our framework. + """ + for interface in providedBy(obj): + if isinstance(interface, IBaseComponent.__class__): + return interface diff --git a/src/graph/components.py b/src/graph_ZCA/components.py copy from src/graph/components.py copy to src/graph_ZCA/components.py --- a/src/graph/components.py +++ b/src/graph_ZCA/components.py @@ -1,36 +1,10 @@ - -from zope.interface import implements, implementsOnly, Interface +from zope.interface import implementsOnly, implements, Interface from zope.component import adapts, getGlobalSiteManager -from graph.interfaces import ITickLabelAdapter -from graph.interfaces import IAxis -from graph.interfaces import IViewBox -from graph.interfaces import IImage -from graph.interfaces import IBaseAdapter - - gsm = getGlobalSiteManager() - -class BaseAdapter(object): - """ - Base for all Adapters - """ - implements(IBaseAdapter) - - def __init__(self, obj): - """ - Gets an Object to be adapted - """ - self.obj = obj - - -class BaseComponent(object): - """ - """ - - def __init__(self, parent): - self.parent = parent +from graph_ZCA.base import BaseAdapter, BaseComponent, BaseFactory +from graph_ZCA.interfaces import ITickLabelAdapter, IAxis, IViewBox, IImage class TickLabelAdapter(BaseAdapter): @@ -52,7 +26,7 @@ class TickLabelAdapter(BaseAdapter): gsm.registerAdapter(TickLabelAdapter) -class Axis(BaseComponent): +class AxisComponent(BaseComponent): """ Represents an axis """ @@ -61,7 +35,7 @@ class Axis(BaseComponent): adapts(Interface) def __init__(self, parent, orientation=None, start=None, end=None): - super(Axis, self).__init__(parent) + super(AxisComponent, self).__init__(parent) self.orientation = orientation self.start = start self.end = end @@ -70,12 +44,17 @@ class Axis(BaseComponent): return ITickLabelAdapter(self).render() def render(self): - return 'axis: ' + self.orientation + ':' + ' '.join(self.get_tick_labels()) + return '%s: ' % self.__class__.__name__ + self.orientation + ':' + ' '.join(self.get_tick_labels()) -gsm.registerAdapter(Axis) +gsm.registerAdapter(AxisComponent) -class ViewBox(BaseComponent): +class Axis(BaseFactory): + + target_interface = IAxis + + +class ViewBoxComponent(BaseComponent): """ Drawing area of a plot. THe drawing ares may have an unlimited number of axis """ @@ -84,11 +63,9 @@ class ViewBox(BaseComponent): def __init__(self, parent): - super(ViewBox, self).__init__(parent) - self.items = [] + super(ViewBoxComponent, self).__init__(parent) - def addItem(self, item): - self.items.append(item) + def render(self): result = [] @@ -97,10 +74,16 @@ class ViewBox(BaseComponent): return '\n'.join(result) -gsm.registerAdapter(ViewBox) + +gsm.registerAdapter(ViewBoxComponent) -class Image(BaseComponent): +class ViewBox(BaseFactory): + + target_interface = IViewBox + + +class ImageComponent(BaseComponent): """ A image representing f(x,y) values """ @@ -108,11 +91,18 @@ class Image(BaseComponent): adapts(Interface) - def __init__(self, parent, data): - super(Image, self).__init__(parent) + def __init__(self, parent): + super(ImageComponent, self).__init__(parent) + + def render(self): + return '%s:' % self.__class__.__name__ + str(self.data) + + def setup(self, data, **kwargs): self.data = data - def render(self): - return 'Image: Rendered image' +gsm.registerAdapter(ImageComponent) + -gsm.registerAdapter(Image) +class Image(BaseFactory): + + target_interface = IImage diff --git a/src/graph/interfaces.py b/src/graph_ZCA/interfaces.py copy from src/graph/interfaces.py copy to src/graph_ZCA/interfaces.py --- a/src/graph/interfaces.py +++ b/src/graph_ZCA/interfaces.py @@ -11,8 +11,12 @@ class IBaseAdapter(Interface): Gets an Object to be adapted """ +class IBaseComponent(Interface): + """ + All Interfaces derived from this class are marking a Class to be a component of this framework. + """ -class IAxis(Interface): +class IAxis(IBaseComponent): """ Axis interface """ @@ -21,17 +25,17 @@ class IAxis(Interface): createed with knowledge of its parent component """ -class ITickLabelAdapter(Interface): +class ITickLabelAdapter(IBaseComponent): """ An adapter that produces tick labels for an axis """ -class IViewBox(Interface): +class IViewBox(IBaseComponent): """ Container to display some components """ -class IImage(Interface): +class IImage(IBaseComponent): """ Some xy data """