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

from pygraph.classes.digraph import digraph
from pygraph.algorithms.sorting import topological_sorting

import ipfblock
import ipfblock.connection

class IPFGraph(object):
    """ Image processing flow graph
    
        This class represents image processing chain. 
    """

    def __init__(self):
        self.blocks = dict() # {"Block name" : IPFBlock}
        self.connections = [] 
        
    def add_block(self, block_name, ipf_block):
        self.blocks[block_name] = ipf_block
    
    def remove_block(self, block_name):
        del self.blocks[block_name]
    
    def add_connection(self, oport, iport):
        """ Add connection between input (iport) and output (oport) ports 
            
            Function raises exception ValueError if ports can`t be connected.
        """
        con = ipfblock.connection.Connection(oport, iport)
        self.connections.append(con)
        
    def process(self):
        """ Process image processing flow for IPFGraph """
        
        # Invalidate all input ports in blocks
        ((iport.invalidate() for iport in block.input_ports.values()) 
                             for block in self.blocks.values())
        
        # Map image processing flow to directed graph
        # apply topological sorting and execute processing blocks in
        # topological order 
        
        graph = digraph()
        for block in self.blocks:
            graph.add_node(block)
            for iport in block.input_ports.values():
                graph.add_node(iport)
                graph.add_edge((iport, block))
            for oport in block.output_ports.values():
                graph.add_node(oport)
                graph.add_edge(block, oport)
        for connection in self.connections:
            graph.add_node(connection)
            graph.add_edge(connection._iport, connection)
            graph.add_edge(connection, connection._oport)
        
        sorted_graph = topological_sorting(graph)
        for node in sorted_graph:
            print node
        
    
    
        
        
        
@@ -1451,21 +1451,34 @@
 dd_node(
-block
+self.blocks%5Bblock%5D
 )%0A      
@@ -1488,37 +1488,50 @@
    for iport in 
-block
+self.blocks%5Bblock%5D
 .input_ports.val
@@ -1610,16 +1610,17 @@
 dge(
+ 
 (iport, 
 bloc
@@ -1615,22 +1615,36 @@
 (iport, 
-block)
+self.blocks%5Bblock%5D) 
 )%0A      
@@ -1662,21 +1662,34 @@
 port in 
-block
+self.blocks%5Bblock%5D
 .output_
@@ -1777,21 +1777,36 @@
 dge(
-block
+ (self.blocks%5Bblock%5D
 , oport)
 %0A   
@@ -1797,24 +1797,26 @@
 ock%5D, oport)
+ )
 %0A        for
@@ -1906,32 +1906,34 @@
  graph.add_edge(
+ (
 connection._ipor
@@ -1928,17 +1928,17 @@
 ection._
-i
+o
 port, co
@@ -1938,32 +1938,34 @@
 ort, connection)
+ )
 %0A            gra
@@ -1976,16 +1976,18 @@
 dd_edge(
+ (
 connecti
@@ -2002,22 +2002,24 @@
 ection._
-o
+i
 port)
+ )
 %0A       

Fix IPFGraph process function