# -*- coding: utf-8 -*-

import xml.etree.ElementTree
from xml.etree.ElementTree import Element 
from xml.etree.ElementTree import SubElement

from ipf.keyfromvalue import dict_key_from_value


class IPFBlock(object):
    """ Base image processing flow block class
    
    """
    # Class members
    type = "IPFBlock"
    category = ""
    # Tells that block is abstract and can not be created
    is_abstract_block = True
    
    def __init__(self):
        self.input_ports = dict()   # {"name" : IPort object}
        self.output_ports = dict()  # {"name" : OPort object}
        self.properties = dict()    # {"name" : Property object}
        self.python_code = u""      # Python code of image processing
        self.processing_function = None # Image processing function
        self.c_code = u""           # C code of image processing
        self.c_function = u""       # Name of C function
        
    def process(self):
        """ Execute IPFBlock process. Sets results to output ports values
        
            Input ports names binded to processing function named arguments
        
        """
        input = dict()
        for key in self.input_ports:
            input[key] = self.input_ports[key]._get_value()
        for key in self.properties:
            input[key] = self.properties[key].get_value() 
        if self.processing_function is not None:
            output = self.processing_function(input)
            for key in output:
                if self.output_ports.has_key(key) and output[key] is not None:
                    self.output_ports[key]._set_value(output[key])
                    
    def xml(self):
        """ Return block object in XML element 
        
        """
        block = Element(self.type)
        
        input_ports_tree = SubElement(block, "InputPorts")
        for port in self.input_ports:
            port_element = SubElement(input_ports_tree, "InputPort", {"name" : port})
            port_element.append(self.input_ports[port].xml())
        
        output_ports_tree = SubElement(block, "OutputPorts")
        for port in self.output_ports:
            port_element = SubElement(output_ports_tree, "OutputPort", {"name" : port})
            port_element.append(self.output_ports[port].xml())
        
        properties_tree = SubElement(block, "Properties")
        for property in self.properties:
            property_element = SubElement(properties_tree, "Property", {"name" : property})
            property_element.append(self.properties[property].xml())
            
        return block
    
    def get_port_name(self, port):
        iport_name = dict_key_from_value(self.input_ports, port)
        if iport_name is not None:
            return iport_name
        oport_name = dict_key_from_value(self.output_ports, port)
        return oport_name
    
    def get_preview_image(self):
        """ Return block processing result in form of image
        
            If no result is available, function returns None 
        """
        return None
            
        
        
    
    


@@ -331,16 +331,21 @@
 ry = %22%22%0A
+    %0A
     # Te
@@ -424,24 +424,129 @@
 = True%0A    %0A
+    # Block instanced number%0A    # Used for generation of unique block names%0A    __block_number = 0%0A    %0A
     def __in
@@ -590,18 +590,16 @@
 = dict()
-  
  # %7B%22nam
@@ -651,17 +651,16 @@
 = dict()
- 
  # %7B%22nam
@@ -710,19 +710,16 @@
 = dict()
-   
  # %7B%22nam
@@ -758,253 +758,94 @@
 lf.p
-ython_code = u%22%22      # Python code of image processing%0A        self.processing_function = None # Image processing function%0A        self.c_code = u%22%22           # C code of image processing%0A        self.c_function = u%22%22       # Name of C function
+rocessing_function = None # Image processing function%0A        self.__block_number += 1
 %0A   

Change IPFBlock interface, delete unused members. Add __block_number class member for automatic naming.