Creating a hierarchical dot graph from a list of paths.
The Function
def dir2dot(str_dir_file): # split lines list_dir_lines = str_dir_file.split('\n') # find the number of layers list_line_layers = [] for str_each_line in list_dir_lines: list_l_layer=str_each_line.split('_') list_line_layers.append(len(list_l_layer)) int_total_layers = max(list_line_layers) # dictionary of layers dict_layers={} for int_layer in range(0,int_total_layers): dict_layers[int_layer] = set() # place stuff in ther dicts for str_each_line in list_dir_lines: list_line_comps = str_each_line.split('_') int_line_layers = len(list_line_comps) for int_layer in range(0,int_line_layers): if len(list_line_comps[int_layer]) > 0: dict_layers[int_layer].add(list_line_comps[int_layer]) # normalise nodes to add color int_node_width = 1.0/int_total_layers # all the Nodes section list_dot_nodes = [] for int_layer in range(0,int_total_layers): list_layer_nodes = dict_layers[int_layer] str_layer = '"\n\t\t"'.join(list_layer_nodes) str_dot_format = '\t{node[style=filled,color=grey%d]\n\t\t"%s"\n\t}\n' str_node_layer = str_dot_format % (100*(int_node_width*int_layer), str_layer) list_dot_nodes.append(str_node_layer) # sort out the edges set_dot_edges = set() for str_each_line in list_dir_lines: list_line_comps = str_each_line.split('_') for int_level in range(1,len(list_line_comps)): str_from_node = list_line_comps[int_level-1] str_to_node = list_line_comps[int_level] str_dot_edge = '"%s" -- "%s"' % (str_from_node, str_to_node) set_dot_edges.add(str_dot_edge) # output str_dot_nodes = '\n'.join(list_dot_nodes) str_dot_edges = '\n'.join(set_dot_edges) str_dot_format = 'graph{\n%s\n%s\n}' % (str_dot_nodes,str_dot_edges) file_w_output = open('cm_dot.dot','w') file_w_output.write(str_dot_format) file_w_output.close()
Running the Function
# first open the file and read the contents # as a string with open('raw_tree_data.txt','r') as file_r_dir_file: str_dir_data = file_r_dir_file.read() # Then feed that string to the function. dir2dot(str_dir_data)
One reply on “Hierarchical Directories to Dot Graph – Python”
[…] ← Previous […]