# 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


import sys, os
        import json
from .common import open_json
    for dos in dossiers:
        prev_step = ''
        for step in dos['steps']:
            step_name = ' • '.join((x for x in (step.get('stage'), step.get('institution'), step.get('step','')) if x))
            if procedure.get(prev_step, {}).get(step_name, False) is False:
                if verbose:
                    print('INCORRECT', prev_step, '->', step_name)
                    print(dos.get('url_dossier_senat'), '|',dos.get('url_dossier_assemblee'))
                    print()
                anomalies += 1

            #print(step_name, '      \t\t\t\t===>>', procedure.get(prev_step, {}).get(step_name))

            prev_step = step_name

    if verbose and anomalies:
        print(anomalies, 'anomalies (', len(dossiers), 'doslegs)')
    return anomalies

"""
TODO:
 - anomalies liens: /leg/tas for textes sénat
"""

if __name__ == '__main__':
    if not os.isatty(0):
        dossiers = json.loads(sys.stdin.read())
        if type(dossiers) is not list:
            dossiers = [dossiers]
    else:

# 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


# Path: tlfp/tools/detect_anomalies.py
import sys, os
        import json
from .common import open_json

    for dos in dossiers:
        prev_step = ''
        for step in dos['steps']:
            step_name = ' • '.join((x for x in (step.get('stage'), step.get('institution'), step.get('step','')) if x))
            if procedure.get(prev_step, {}).get(step_name, False) is False:
                if verbose:
                    print('INCORRECT', prev_step, '->', step_name)
                    print(dos.get('url_dossier_senat'), '|',dos.get('url_dossier_assemblee'))
                    print()
                anomalies += 1

            #print(step_name, '      \t\t\t\t===>>', procedure.get(prev_step, {}).get(step_name))

            prev_step = step_name

    if verbose and anomalies:
        print(anomalies, 'anomalies (', len(dossiers), 'doslegs)')
    return anomalies

"""
TODO:
 - anomalies liens: /leg/tas for textes sénat
"""

if __name__ == '__main__':
    if not os.isatty(0):
        dossiers = json.loads(sys.stdin.read())
        if type(dossiers) is not list:
            dossiers = [dossiers]
    else:

        dossiers = open_json(sys.argv[1])