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

#    Copyright (C) 2015 Yahoo! Inc. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from __future__ import absolute_import

try:
    import pydot
    PYDOT_AVAILABLE = True
except ImportError:
    PYDOT_AVAILABLE = False


def convert(machine, graph_name,
            graph_attrs=None, node_attrs_cb=None, edge_attrs_cb=None,
            add_start_state=True):
    """Translates the state machine into a pydot graph.

    :param machine: state machine to convert
    :type machine: FiniteMachine
    :param graph_name: name of the graph to be created
    :type graph_name: string
    :param graph_attrs: any initial graph attributes to set
                        (see http://www.graphviz.org/doc/info/attrs.html for
                        what these can be)
    :type graph_attrs: dict
    :param node_attrs_cb: a callback that takes one argument ``state``
                          and is expected to return a dict of node attributes
                          (see http://www.graphviz.org/doc/info/attrs.html for
                          what these can be)
    :type node_attrs_cb: callback
    :param edge_attrs_cb: a callback that takes three arguments ``start_state,
                          event, end_state`` and is expected to return a dict
                          of edge attributes (see
                          http://www.graphviz.org/doc/info/attrs.html for
                          what these can be)
    :type edge_attrs_cb: callback
    :param add_start_state: when enabled this creates a *private* start state
                            with the name ``__start__`` that will be a point
                            node that will have a dotted edge to the
                            ``default_start_state`` that your machine may have
                            defined (if your machine has no actively defined
                            ``default_start_state`` then this does nothing,
                            even if enabled)
    :type add_start_state: bool
    """
    if not PYDOT_AVAILABLE:
        raise RuntimeError("pydot (or pydot2 or equivalent) is required"
                           " to convert a state machine into a pydot"
                           " graph")
    graph_kwargs = {
        'rankdir': 'LR',
        'nodesep': '0.25',
        'overlap': 'false',
        'ranksep': '0.5',
        'size': "11x8.5",
        'splines': 'true',
        'ordering': 'in',
    }
    if graph_attrs is not None:
        graph_kwargs.update(graph_attrs)
    graph_kwargs['graph_name'] = graph_name
    g = pydot.Dot(**graph_kwargs)
    node_attrs = {
        'fontsize': '11',
    }
    nodes = {}
    for (start_state, event, end_state) in machine:
        if start_state not in nodes:
            start_node_attrs = node_attrs.copy()
            if node_attrs_cb is not None:
                start_node_attrs.update(node_attrs_cb(start_state))
            nodes[start_state] = pydot.Node(start_state,
                                            **start_node_attrs)
            g.add_node(nodes[start_state])
        if end_state not in nodes:
            end_node_attrs = node_attrs.copy()
            if node_attrs_cb is not None:
                end_node_attrs.update(node_attrs_cb(end_state))
            nodes[end_state] = pydot.Node(end_state, **end_node_attrs)
            g.add_node(nodes[end_state])
        edge_attrs = {}
        if edge_attrs_cb is not None:
            edge_attrs.update(edge_attrs_cb(start_state, event, end_state))
        g.add_edge(pydot.Edge(nodes[start_state], nodes[end_state],
                              **edge_attrs))
    if add_start_state and machine.default_start_state:
        start = pydot.Node("__start__", shape="point", width="0.1",
                           xlabel='start', fontcolor='green', **node_attrs)
        g.add_node(start)
        g.add_edge(pydot.Edge(start, nodes[machine.default_start_state],
                              style='dotted'))
    return g

@@ -928,11 +928,35 @@
 True
+, name_translations=None
 ):%0A
-
     
@@ -2578,16 +2578,181 @@
 e: bool%0A
+    :param name_translations: a dict that provides alternative %60%60state%60%60%0A                              string names for each state%0A    :type name_translations: dict%0A
     %22%22%22%0A
@@ -2959,16 +2959,77 @@
 graph%22)%0A
+    if not name_translations:%0A        name_translations = %7B%7D%0A
     grap
@@ -3689,32 +3689,168 @@
 b(start_state))%0A
+            pretty_start_state = name_translations.get(start_state,%0A                                                       start_state)%0A
             node
@@ -3873,24 +3873,31 @@
  pydot.Node(
+pretty_
 start_state,
@@ -4184,32 +4184,107 @@
 _cb(end_state))%0A
+            pretty_end_state = name_translations.get(end_state, end_state)%0A
             node
@@ -4309,16 +4309,23 @@
 ot.Node(
+pretty_
 end_stat

Allow providing a state-name translation dict