Changeset - 7aab6dba2515
[Not reviewed]
default
0 1 5
volker - 10 years ago 2015-06-23 22:37:14
volker.jaenisch@inqbus.de
Added ZCA Version of the code
6 files changed with 166 insertions and 74 deletions:
0 comments (0 inline, 0 general)
src/graph/interfaces.py
Show inline comments
 

	
 
from zope.interface import Interface
 

	
 
# ADAPTER BASE CLASSES
 
class IBaseAdapter(Interface):
 
    """
 
    Base of all adapters
 
    """
 
    def __init__(self, obj):
 
        """
 
        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
 
    """
 
    def __init__(self, parent):
 
        """
 
        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
 
    """
 

	
src/graph_ZCA/__init__.py
Show inline comments
 
file copied from src/graph/__init__.py to src/graph_ZCA/__init__.py
src/graph_ZCA/app.py
Show inline comments
 
file copied from src/graph/app.py to src/graph_ZCA/app.py
 
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
 
    """
 

	
 

	
 
class PlotImage( BaseComponent):
 
    """
 
    Makes a 2D plot for an image
 
    """
 
    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())
 

	
src/graph_ZCA/base.py
Show inline comments
 
new file 100644
 
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
src/graph_ZCA/components.py
Show inline comments
 
file copied from src/graph/components.py to src/graph_ZCA/components.py
 

	
 
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):
 
    """
 
    Generates Ticklabels for an axis
 
    """
 
    implementsOnly(ITickLabelAdapter)
 
    adapts(IAxis)
 

	
 
    def __init__(self, axis):
 
        """
 
        Gets an Object to be adapted
 
        """
 
        self.axis = axis
 

	
 
    def render(self):
 
        return map(str, range(self.axis.start, self.axis.end))
 

	
 
gsm.registerAdapter(TickLabelAdapter)
 

	
 

	
 
class Axis(BaseComponent):
 
class AxisComponent(BaseComponent):
 
    """
 
    Represents an axis
 
    """
 

	
 
    implements(IAxis)
 
    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
 

	
 
    def get_tick_labels(self):
 
        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
 
    """
 
    implements(IViewBox)
 
    adapts(Interface)
 

	
 

	
 
    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 = []
 
        for item in self.items:
 
            result.append(item.render())
 

	
 
        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
 
    """
 
    implements(IImage)
 
    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
src/graph_ZCA/interfaces.py
Show inline comments
 
file copied from src/graph/interfaces.py to src/graph_ZCA/interfaces.py
 

	
 
from zope.interface import Interface
 

	
 
# ADAPTER BASE CLASSES
 
class IBaseAdapter(Interface):
 
    """
 
    Base of all adapters
 
    """
 
    def __init__(self, obj):
 
        """
 
        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
 
    """
 
    def __init__(self, parent):
 
        """
 
        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
 
    """
 

	
0 comments (0 inline, 0 general)