# Path: src/moddy/fsm.py
# def is_sub_fsm_specification(name_cls_tuple):
#     """
#     Test if the tuple from a transition list (name, classType)
#     is a subFsm specification"""
#     _, cls = name_cls_tuple
#     if isinstance(cls, type):
#         return cls
#     return None
# 
# Path: src/moddy/utils.py
# def create_dirs_and_open_output_file(file_path):
#     '''
#     Create missing directories to <file_path> and open <file_path> for writing
#     @return: file descriptor
#     '''
#     if os.path.dirname(file_path) != "":
#         os.makedirs(os.path.dirname(file_path), exist_ok=True)
#     return open(file_path, 'w')


import subprocess
import os
from .fsm import is_sub_fsm_specification
from .utils import create_dirs_and_open_output_file

    def fsm_gen(self, level, fsm, name="", is_sub_fsm=False):
        """
        generate the dot language statements for a (sub)fsm
        return lines,initial_state
        initial_state is only valid on subFsms
        """
        lines = []
        initial_state = None

        dict_transitions = fsm.get_dict_transitions()
        # States
        for state, list_trans in dict_transitions.items():
            if state == "":
                sstr = "%s [style=invisible];" % map_state_names(name, state)
                if is_sub_fsm:
                    sstr = None
            elif state == "ANY":
                sstr = (
                    '%s [label="from any state" shape=none];'
                    % map_state_names(name, state)
                )
            else:
                sstr = "%s [label=%s];" % (map_state_names(name, state), state)
            if sstr is not None:
                lines.append([level, sstr])

        # Transitions
        for from_state, list_trans in dict_transitions.items():
            for trans in list_trans:

# Path: src/moddy/fsm.py
# def is_sub_fsm_specification(name_cls_tuple):
#     """
#     Test if the tuple from a transition list (name, classType)
#     is a subFsm specification"""
#     _, cls = name_cls_tuple
#     if isinstance(cls, type):
#         return cls
#     return None
# 
# Path: src/moddy/utils.py
# def create_dirs_and_open_output_file(file_path):
#     '''
#     Create missing directories to <file_path> and open <file_path> for writing
#     @return: file descriptor
#     '''
#     if os.path.dirname(file_path) != "":
#         os.makedirs(os.path.dirname(file_path), exist_ok=True)
#     return open(file_path, 'w')


# Path: src/moddy/dot_fsm.py
import subprocess
import os
from .fsm import is_sub_fsm_specification
from .utils import create_dirs_and_open_output_file


    def fsm_gen(self, level, fsm, name="", is_sub_fsm=False):
        """
        generate the dot language statements for a (sub)fsm
        return lines,initial_state
        initial_state is only valid on subFsms
        """
        lines = []
        initial_state = None

        dict_transitions = fsm.get_dict_transitions()
        # States
        for state, list_trans in dict_transitions.items():
            if state == "":
                sstr = "%s [style=invisible];" % map_state_names(name, state)
                if is_sub_fsm:
                    sstr = None
            elif state == "ANY":
                sstr = (
                    '%s [label="from any state" shape=none];'
                    % map_state_names(name, state)
                )
            else:
                sstr = "%s [label=%s];" % (map_state_names(name, state), state)
            if sstr is not None:
                lines.append([level, sstr])

        # Transitions
        for from_state, list_trans in dict_transitions.items():
            for trans in list_trans:

                sub_fsm_cls = is_sub_fsm_specification(trans)