Source

qadpid / qadpid / writer.py

Full commit
"""
Writer Code

Copyright 2011 John Chandler
This software is distributed under the terms of the GNU General Public License version 3
"""

def get_next_file_id():
    """ Generator returning incrementing file IDs """
    current_id = 0
    while True:
        current_id += 1
        yield current_id
        

class DOTWriter( object ):
    """ DOT File Writer """
    def __init__( self, code_hierarchy ):
        self._code_hierarchy = code_hierarchy

    def write( self, file_object ):
        """ Write a DOT file to the file object
            Assumes we can start writing at the current location in the file (i.e. append)
        """
        # Header
        file_object.write( "digraph code_hierarchy {\n" )

        # Labels
        file_id = get_next_file_id()
        labels  = {}
        for filename in self._code_hierarchy:
            labels[filename] = "f%s" % file_id.next()
            file_object.write( '  %s [label="%s"];\n' % ( labels[filename], filename ) )

        # Relationships
        for filename in self._code_hierarchy:
            for dep_type, dep_filename in self._code_hierarchy[filename]:
                try:
                    file_object.write( "  %s -> %s;\n" % ( labels[dep_filename], labels[filename] ) )
                except KeyError:
                    # Doesn't current handle include/require files without the .php suffix, so ignore
                    pass

        # Footer
        file_object.write( "}\n" )