Changeset - 3046ea21244b
[Not reviewed]
tip default
0 3 0
volker - 10 years ago 2015-06-23 23:40:20
volker.jaenisch@inqbus.de
Lots of comments
3 files changed with 55 insertions and 28 deletions:
0 comments (0 inline, 0 general)
src/graph_ZCA/app.py
Show inline comments
 
# Here starts the boiler plate
 
from zope.interface import implements   # <- this is needed here to form the PlotImage component
 
from zope.component import getGlobalSiteManager, adapts   # <- Only needed for the extension afterwards
 
gsm = getGlobalSiteManager()                              # <- Only needed for the extension afterwards
 
# End of Boiler plate
 

	
 
from zope.interface import implements
 
# Import of the sub components.
 
from graph_ZCA.components import BaseComponent, Image, Axis, ViewBox
 

	
 
from graph_ZCA.components import BaseComponent, Image, Axis, AxisComponent, ViewBox, TickLabelComponent
 
# Only needed for the extension afterwards.
 
from graph_ZCA.interfaces import IAxis, IViewBox, IBaseComponent
 

	
 
from zope.component import getGlobalSiteManager, adapts
 

	
 
gsm = getGlobalSiteManager()
 
from graph_ZCA.components import TickLabelComponent, AxisComponent
 

	
 

	
 
# Set up a new interface for the component PlotImage. Here usually properties, functions and other exposed items
 
# of a PlotImage instance would be documented and registered to the framework. Since the only function render is
 
# already documented for IBaseComponent nothing has to be done here.
 
class IPlotImage(IBaseComponent):
 
    """
 
    Component to plot a 2d graph of a given image
 
    """
 

	
 

	
 
# Here the component itself is constructed. The component inherits from a simple BaseComponent without much importance.
 
# The only crucial requirement is that every component knows its parent component as self.parent, which is the first
 
# parameter to the constructor. The top most component has a parent of NONE.
 
class PlotImage( BaseComponent):
 
    """
 
    Makes a 2D plot for an image
 
    """
 
    implements(IPlotImage)
 
    implements(IPlotImage)   # here the interface specified above is used.
 

	
 
    def __init__(self, parent):
 
        super(PlotImage, self).__init__(parent)
 
        super(PlotImage, self).__init__(parent)   # tribute to the super class
 

	
 
        self.vb = ViewBox(self)()
 
        self.vb = ViewBox(self)()   # make a ViewBox. Please notice the double Braces. These are neccessary for the framework.
 
                                    # In fact here in no instance generated but a factory. And with the second brace
 
                                    # the factory produces the instance.
 
                                    # The PlotImage "self" is assigned as parent for the ViewBox
 

	
 
        x_axis = Axis(self.vb)()
 
        x_axis = Axis(self.vb)()    # Same pattern here
 
                                    # The axis are assigned to the viewbox
 
        x_axis.orientation='bottom'
 
        x_axis.start=0
 
        x_axis.end=8
 

	
 
        y_axis = Axis(self.vb)()
 
        y_axis = Axis(self.vb)()    # and here
 
        y_axis.orientation='left'
 
        y_axis.start=-5
 
        y_axis.end=3
 

	
 

	
 
        self.vb.addItem( x_axis )
 
        self.vb.addItem( x_axis )   # Finally the axis are given to the viewbox. This step may be not necessary and
 
                                    # may be done automatically while assigning the parent to the axis.
 
        self.vb.addItem( y_axis )
 

	
 
    def render(self):
 
    def render(self):               # In this prototype code every component optains a render method that is used to generate
 
                                    # some simple output
 
        return self.vb.render()
 

	
 

	
 
class NewAxisComponent(AxisComponent):
 
    adapts(IViewBox)
 

	
 
gsm.registerAdapter(NewAxisComponent)
 

	
 

	
 
class NewTickLabelComponent(TickLabelComponent):
 
    adapts(IAxis)
 

	
 
    def render(self):
 
        return " ".join([ "q%i" % i for i in range(self.axis.start, self.axis.end)])
 

	
 
gsm.registerAdapter(NewTickLabelComponent)
 

	
 
# # The following statements can be uncommented to illustrate how to swap classes.
 
# #
 
# # This example shows how to swap all Axis components which parents are a ViewBoxes to be of class NewAxisComponent
 
# class NewAxisComponent(AxisComponent):
 
#      adapts(IViewBox)
 
#
 
# gsm.registerAdapter(NewAxisComponent)
 
#
 
# # This example shows how to exchange the TickLabelComponent for all Axis
 
# class NewTickLabelComponent(TickLabelComponent):
 
#     adapts(IAxis)
 
#
 
#     def render(self):
 
#         return " ".join([ "q%i" % i for i in range(self.axis.start, self.axis.end)])
 
#
 
# gsm.registerAdapter(NewTickLabelComponent)
 

	
 

	
 
image_data = [1,2,3,4]
 

	
 
plot =  PlotImage( None)
 
image = Image(plot.vb)(image_data)
src/graph_ZCA/base.py
Show inline comments
 
@@ -32,12 +32,13 @@ class BaseComponent(object):
 
    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
src/graph_ZCA/interfaces.py
Show inline comments
 
@@ -13,12 +13,22 @@ class IBaseAdapter(Interface):
 

	
 
class IBaseComponent(Interface):
 
    """
 
    All Interfaces derived from this class are marking a Class to be a component of this framework.
 
    """
 

	
 
    def addItem(self, item):
 
        """
 
        Add a sub component
 
        """
 

	
 
    def setup(self, *args, **kwargs):
 
        """
 
        Set parameters and bring the component to live
 
        """
 

	
 
class IAxis(IBaseComponent):
 
    """
 
    Axis interface
 
    """
 
    def __init__(self, parent):
 
        """
0 comments (0 inline, 0 general)