<commit_before>"""
Utility class for displaying node scripts.
"""

import os
import sys
import pydot
import json
import tempfile


def get_node_positions(nodedict):
    """
    Build the pydot graph.
    """
    g = pydot.Dot(margin="0.1", ranksep="0.7", nodesep="1.5")
    for name, node in nodedict.iteritems():
        n = pydot.Node(name, width="0.5", fixedsize="0.5")
        g.add_node(n)

    for name, node in nodedict.iteritems():
        for i in node["inputs"]:
            try:
                src = g.get_node(i)
                if isinstance(src, list):
                    src = src[0]
                dst = g.get_node(name)
                if isinstance(dst, list):
                    dst = dst[0]
                g.add_edge(pydot.Edge(src, dst))
            except IndexError:
                print "Input %s not found" % i

    with tempfile.NamedTemporaryFile(delete=False, suffix=".dot") as t:
        g.write_dot(t.name)
    g = pydot.graph_from_dot_file(t.name)
    os.unlink(t.name)

    out = {}
    for name, node in nodedict.iteritems():
        gn = g.get_node(name)
        if isinstance(gn, list):
            gn = gn[0]
        out[name] = [int(float(d)) \
                for d in gn.get_pos().replace('"', "").split(",")]
    return out
   
if __name__ == "__main__":
    nodes = {}
    with open(sys.argv[1], "r") as f:
        nodes = json.load(f)
    print get_node_positions(nodes)
<commit_msg>Fixed a bug in the graphviz layouter for nodes with names that contain a dash<commit_after>"""
Utility class for displaying node scripts.
"""

import os
import sys
import pydot
import json
import tempfile


def quote_name_if_dashed(name):
    if name.find("-") != -1:
        return "\"%s\"" % name
    return name

def get_node_positions(nodedict, aspect=None):
    """
    Build the pydot graph.
    """
    g = pydot.Dot(margin="0.1", ranksep="0.7", nodesep="1.5")
    if aspect is not None:
        g.set_aspect(round(aspect))
    for name, node in nodedict.iteritems():
        n = pydot.Node(name, width="0.5", fixedsize="0.5")
        g.add_node(n)

    for name, node in nodedict.iteritems():
        for i in node["inputs"]:
            try:
                src = g.get_node(quote_name_if_dashed(i))
                if isinstance(src, list):
                    src = src[0]
                dst = g.get_node(quote_name_if_dashed(name))
                if isinstance(dst, list):
                    dst = dst[0]
                g.add_edge(pydot.Edge(src, dst))
            except IndexError:
                print "Input %s not found" % i

    with tempfile.NamedTemporaryFile(delete=False, suffix=".dot") as t:
        g.write_dot(t.name)
    g = pydot.graph_from_dot_file(t.name)
    os.unlink(t.name)

    out = {}
    for name, node in nodedict.iteritems():
        gn = g.get_node(quote_name_if_dashed(name))
        if isinstance(gn, list):
            gn = gn[0]
        out[name] = [int(float(d)) \
                for d in gn.get_pos().replace('"', "").split(",")]
    return out
   
if __name__ == "__main__":
    nodes = {}
    with open(sys.argv[1], "r") as f:
        nodes = json.load(f)
    print get_node_positions(nodes)

{'fraction_non_alphanumeric': 0.13566257475605917, 'fraction_numerical': 0.00912810827824992, 'mean_word_length': 2.029551954242135, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6479501', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1052, 'n_words': 261}