Go to the documentation of this file.00001 """
00002 Copyright (c) 2013, Cogniteam
00003 All rights reserved.
00004
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007
00008 * Redistributions of source code must retain the above copyright
00009 notice, this list of conditions and the following disclaimer.
00010
00011 * Redistributions in binary form must reproduce the above copyright
00012 notice, this list of conditions and the following disclaimer in the
00013 documentation and/or other materials provided with the distribution.
00014
00015 * Neither the name of the Cogniteam nor the
00016 names of its contributors may be used to endorse or promote products
00017 derived from this software without specific prior written permission.
00018
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
00023 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 """
00030
00031 import pydot
00032
00033
00034 class Graph(object):
00035
00036 @staticmethod
00037 def __read_dot_data_from_file(path):
00038 try:
00039 with open(path, 'r') as file_handler:
00040 return file_handler.read()
00041 except IOError:
00042 return None
00043
00044 def __init__(self, dot_processor, source, parent=None):
00045 super(Graph, self).__init__()
00046 self.dot_processor = dot_processor
00047 self.parent = parent
00048 self.name = None
00049 self.nodes = None
00050 self.edges = None
00051 self.source = source
00052
00053 def load(self):
00054 print 'Reading dot data from', self.source
00055 self._create_dot_from_file(self.source)
00056
00057 def _create_dot_from_file(self, dot_file_path):
00058 dot_data = self.__read_dot_data_from_file(dot_file_path)
00059 if dot_data is None:
00060 raise GraphParseException("Could not read dot file")
00061
00062 graph = pydot.graph_from_dot_data(dot_data.encode("ascii", "ignore"))
00063 if graph is None:
00064 raise GraphParseException("Could not create graph based on loaded dot data")
00065
00066 dot_graph = graph.create_dot()
00067 if dot_graph is None:
00068 raise GraphParseException("Could not create DOT graph based on graph")
00069
00070 self.nodes, self.edges = self.dot_processor.process(dot_graph)
00071
00072
00073 class GraphParseException(Exception):
00074 pass