# Path: tlfp/tools/common.py
# def open_json(dirpath, filename=None):
#     if filename:
#         path = os.path.join(dirpath, filename)
#     else:
#         path = dirpath
#     try:
#         with open(path, 'r') as f:
#             return json.load(f)
#     except Exception as e:
#         print(type(e), e, file=sys.stderr)
#         sys.stderr.write("ERROR: Could not open file %s" % (path,))
#         raise e
# 
# def print_json(dico, filename=None):
#     jdump = json.dumps(dico, ensure_ascii=False, sort_keys=True, indent=2)
#     if filename:
#         # create parent directories if necessary
#         dirname = os.path.dirname(filename)
#         if dirname:
#             os.makedirs(dirname, exist_ok=True)
# 
#         try:
#             with open("%s.tmp" % filename, 'w') as f:
#                 f.write(jdump)
#             if os.path.exists(filename):
#                 os.remove(filename)
#             os.rename("%s.tmp" % filename, filename)
#         except Exception as e:
#             print(type(e), e, file=sys.stderr)
#             sys.stderr.write("ERROR: Could not write in file %s" % filename)
#             raise e
#     else:
#         print(jdump)


import sys, os, random, glob
from tlfp.tools.common import open_json, print_json
          }
        """ % (get_node_id('%s • assemblee' % stage), get_node_id('%s • senat' % stage)))
    dot_result += """
    {
        rank=same; %s; %s; %s;
    }
    """ % (get_node_id('CMP'), get_node_id('3ème lecture • assemblee'), get_node_id('3ème lecture • senat'))

    dot_result += """
    {
        rank=same; %s; %s;
    }
    """ % (get_node_id('congrès • congrès'), get_node_id('constitutionnalité • conseil constitutionnel'))
else:
    for stage in ['1ère lecture', '2ème lecture', '3ème lecture', 'CMP']:
        for step in ['depot', 'commission', 'hemicycle']:
            if stage == 'CMP' and step == 'commission': continue
            dot_result += ("""
              {
                rank=same; %s; %s;
              }
            """ % (get_node_id('%s • assemblee • %s' % (stage, step)), get_node_id('%s • senat • %s' % (stage, step))))


dot_result += '\n}'

details = "_detailed" if mode == "detailed" else ""
open('_steps%s.log' % details, 'w').write(steps_logs)
filename = os.path.join(API_DIRECTORY, 'stats',
                        'steps%s_transitions.json' % details)

# Path: tlfp/tools/common.py
# def open_json(dirpath, filename=None):
#     if filename:
#         path = os.path.join(dirpath, filename)
#     else:
#         path = dirpath
#     try:
#         with open(path, 'r') as f:
#             return json.load(f)
#     except Exception as e:
#         print(type(e), e, file=sys.stderr)
#         sys.stderr.write("ERROR: Could not open file %s" % (path,))
#         raise e
# 
# def print_json(dico, filename=None):
#     jdump = json.dumps(dico, ensure_ascii=False, sort_keys=True, indent=2)
#     if filename:
#         # create parent directories if necessary
#         dirname = os.path.dirname(filename)
#         if dirname:
#             os.makedirs(dirname, exist_ok=True)
# 
#         try:
#             with open("%s.tmp" % filename, 'w') as f:
#                 f.write(jdump)
#             if os.path.exists(filename):
#                 os.remove(filename)
#             os.rename("%s.tmp" % filename, filename)
#         except Exception as e:
#             print(type(e), e, file=sys.stderr)
#             sys.stderr.write("ERROR: Could not write in file %s" % filename)
#             raise e
#     else:
#         print(jdump)


# Path: tlfp/tools/steps_as_dot.py
import sys, os, random, glob
from tlfp.tools.common import open_json, print_json

          }
        """ % (get_node_id('%s • assemblee' % stage), get_node_id('%s • senat' % stage)))
    dot_result += """
    {
        rank=same; %s; %s; %s;
    }
    """ % (get_node_id('CMP'), get_node_id('3ème lecture • assemblee'), get_node_id('3ème lecture • senat'))

    dot_result += """
    {
        rank=same; %s; %s;
    }
    """ % (get_node_id('congrès • congrès'), get_node_id('constitutionnalité • conseil constitutionnel'))
else:
    for stage in ['1ère lecture', '2ème lecture', '3ème lecture', 'CMP']:
        for step in ['depot', 'commission', 'hemicycle']:
            if stage == 'CMP' and step == 'commission': continue
            dot_result += ("""
              {
                rank=same; %s; %s;
              }
            """ % (get_node_id('%s • assemblee • %s' % (stage, step)), get_node_id('%s • senat • %s' % (stage, step))))


dot_result += '\n}'

details = "_detailed" if mode == "detailed" else ""
open('_steps%s.log' % details, 'w').write(steps_logs)
filename = os.path.join(API_DIRECTORY, 'stats',
                        'steps%s_transitions.json' % details)

print_json(step_trans, filename)