# Path: tlfp/tools/parse_texte.py
# class TextParsingFailedException(Exception):
# def word_to_number(word):
# def non_recursive_find_all(node, should_be_parsed, should_be_ignored):
# def clean_extra_expose_des_motifs(html):
# def clean_extra_expose_des_motifs_v2(html):
# def romans(n):
# def lower_but_first(text):
# def clean_full_upcase(text):
# def clean_html(t):
# def check_section_is_not_a_duplicate(section_id, articles):
# def add_to_articles(dic, all_articles):
# def normalize_1(name, one):
# def normalize_section_title(line, line_soup, has_multiple_expose):
# def clean_article_name(text):
# def parse(url, resp=None, DEBUG=False, include_annexes=False):
#     def pr_js(article):
#     def should_be_parsed(x):
#     def should_be_ignored(x):
#     NEW_LINE_MARKER = 'NEW_LINE_MARKER'
#     READ_DISABLED = -1 # the text is not detected yet
#     READ_TEXT = 0 # read the text
#     READ_TITLE = 1 # titles lecture
#     READ_ALINEAS = 2 # alineas lecture
# 
# Path: tlfp/tools/common.py
# def log_print(file=None, only_log=False):
#         def __init__(self, file):
#         def write(self, message):
#         def __getattr__(self, attr):
# def open_csv(dirpath, filename, delimiter=";"):
# def open_json(dirpath, filename=None):
# def download_daily(url_or_collecter, filename, output_directory):
# def print_json(dico, filename=None):
# def debug_file(data, filename):
# def format_date(d):
# def format_display_date(d):
# def real_lower(text):
# def clean_accents(text):
# def clean_text_for_diff(text):
# def compute_approx_similarity(text1, text2):
# def compute_similarity(text1, text2):
# def compute_similarity_by_articles(text1, text2):
# def identify_room(url_or_institution, legislature):
# def national_assembly_text_legislature(url_text):
# def personalize_link(link, obj, urlapi):
# def slug_groupe(g):
#     def __init__(self, sourcedir, load_parls=False):
#     def get_procedure(self):
#     def get_parlementaires(self):
#     def get_parlementaire(self, urlapi, slug):
#     def get_groupes(self):
#     def get_historique_groupes_senat(self):
#     def get_senateur_groupe(self, slug, object_date, urlapi):
#     def add_groupe(self, groupes, gpe, urlapi):
# def get_text_id(texte_url):
#     class Logger:
# class Context(object):
# 
# Path: tlfp/tools/process_conscons.py
# def get_decision_length(url):
#     decision_txt = extract_full_decision(url)
#     if not decision_txt:
#         return -1
#     return len(decision_txt)
# 
# Path: tlfp/tools/process_jo.py
# def count_signataires(url):
#     signataires_text = extract_signataires(url)
#     if not signataires_text:
#         return -1
#     return count_ministres(signataires_text)
# 
# def get_texte_length(url):
#     text = extract_texte(url)
#     if not text:
#         return -1
#     return len(text)
# 
# Path: tlfp/tools/update_procedure.py
# def detect_auteur_depot_from_url(url):
#     if '/propositions/' in url or '/PIONAN' in url:
#         return "Députés"
#     elif '/leg/ppl' in url:
#         return "Sénateurs"
#     return "Gouvernement"


import glob, os, sys, csv, traceback
from lawfactory_utils.urls import enable_requests_cache
from senapy.dosleg import opendata
from senapy.dosleg.parser import guess_legislature
from tlfp.parse_one import *
from tlfp.tools import parse_texte
from tlfp.tools.common import upper_first, format_date, datize, strip_text, open_json
from tlfp.tools.process_conscons import get_decision_length
from tlfp.tools.process_jo import count_signataires, get_texte_length
from tlfp.tools.update_procedure import detect_auteur_depot_from_url
    dos['Initiative du texte'], dos['Institution de dépôt'] = get_initiative(parsed_dos['steps'])
    dos['Étapes échouées'] = count_echecs(parsed_dos['steps'])
    dos['CMP'] = get_CMP_type(parsed_dos['steps'])
    cc_step = [step['source_url'] for step in parsed_dos['steps'] if step.get('stage') == 'constitutionnalité']
    # dos['Taille de la décision du CC'] = get_decision_length(cc_step[0]) if cc_step else ''
    dos['URL CC'] = cc_step[0] if cc_step else ''
    # dos['Signataires au JO'] = count_signataires(parsed_dos['url_jo']) if 'url_jo' in parsed_dos else ''
    dos['URL JO'] = parsed_dos['url_jo'] if 'url_jo' in parsed_dos else ''
    dos["Législature de promulgation"] = parsed_dos.get('assemblee_legislature', '')

    last_depot = find_last_depot(parsed_dos['steps'])
    last_text = None
    for step in reversed(parsed_dos['steps']):
        last_text = step
        if step.get('step') == 'hemicycle':
            break
        if step.get('step') == 'commission':
            raise Exception('commission as last step')

    if 'source_url' in last_text:
        try:
            articles = parse_texte.parse(last_text['source_url'])
            if articles and articles[0].get('definitif'):
                dos['Nombre de caractères final'] = read_text(parse_texte.parse(last_text['source_url']))
            else:
                dos['Nombre de caractères final'] = get_texte_length(parsed_dos['url_jo']) if 'url_jo' in parsed_dos else ''
        except:
            print("WARNING: Nombre de caractères final impossible to evaluate")

    try:

# Path: tlfp/tools/parse_texte.py
# class TextParsingFailedException(Exception):
# def word_to_number(word):
# def non_recursive_find_all(node, should_be_parsed, should_be_ignored):
# def clean_extra_expose_des_motifs(html):
# def clean_extra_expose_des_motifs_v2(html):
# def romans(n):
# def lower_but_first(text):
# def clean_full_upcase(text):
# def clean_html(t):
# def check_section_is_not_a_duplicate(section_id, articles):
# def add_to_articles(dic, all_articles):
# def normalize_1(name, one):
# def normalize_section_title(line, line_soup, has_multiple_expose):
# def clean_article_name(text):
# def parse(url, resp=None, DEBUG=False, include_annexes=False):
#     def pr_js(article):
#     def should_be_parsed(x):
#     def should_be_ignored(x):
#     NEW_LINE_MARKER = 'NEW_LINE_MARKER'
#     READ_DISABLED = -1 # the text is not detected yet
#     READ_TEXT = 0 # read the text
#     READ_TITLE = 1 # titles lecture
#     READ_ALINEAS = 2 # alineas lecture
# 
# Path: tlfp/tools/common.py
# def log_print(file=None, only_log=False):
#         def __init__(self, file):
#         def write(self, message):
#         def __getattr__(self, attr):
# def open_csv(dirpath, filename, delimiter=";"):
# def open_json(dirpath, filename=None):
# def download_daily(url_or_collecter, filename, output_directory):
# def print_json(dico, filename=None):
# def debug_file(data, filename):
# def format_date(d):
# def format_display_date(d):
# def real_lower(text):
# def clean_accents(text):
# def clean_text_for_diff(text):
# def compute_approx_similarity(text1, text2):
# def compute_similarity(text1, text2):
# def compute_similarity_by_articles(text1, text2):
# def identify_room(url_or_institution, legislature):
# def national_assembly_text_legislature(url_text):
# def personalize_link(link, obj, urlapi):
# def slug_groupe(g):
#     def __init__(self, sourcedir, load_parls=False):
#     def get_procedure(self):
#     def get_parlementaires(self):
#     def get_parlementaire(self, urlapi, slug):
#     def get_groupes(self):
#     def get_historique_groupes_senat(self):
#     def get_senateur_groupe(self, slug, object_date, urlapi):
#     def add_groupe(self, groupes, gpe, urlapi):
# def get_text_id(texte_url):
#     class Logger:
# class Context(object):
# 
# Path: tlfp/tools/process_conscons.py
# def get_decision_length(url):
#     decision_txt = extract_full_decision(url)
#     if not decision_txt:
#         return -1
#     return len(decision_txt)
# 
# Path: tlfp/tools/process_jo.py
# def count_signataires(url):
#     signataires_text = extract_signataires(url)
#     if not signataires_text:
#         return -1
#     return count_ministres(signataires_text)
# 
# def get_texte_length(url):
#     text = extract_texte(url)
#     if not text:
#         return -1
#     return len(text)
# 
# Path: tlfp/tools/update_procedure.py
# def detect_auteur_depot_from_url(url):
#     if '/propositions/' in url or '/PIONAN' in url:
#         return "Députés"
#     elif '/leg/ppl' in url:
#         return "Sénateurs"
#     return "Gouvernement"


# Path: tlfp/tools/make_metrics_csv.py
import glob, os, sys, csv, traceback
from lawfactory_utils.urls import enable_requests_cache
from senapy.dosleg import opendata
from senapy.dosleg.parser import guess_legislature
from tlfp.parse_one import *
from tlfp.tools import parse_texte
from tlfp.tools.common import upper_first, format_date, datize, strip_text, open_json
from tlfp.tools.process_conscons import get_decision_length
from tlfp.tools.process_jo import count_signataires, get_texte_length
from tlfp.tools.update_procedure import detect_auteur_depot_from_url

    dos['Initiative du texte'], dos['Institution de dépôt'] = get_initiative(parsed_dos['steps'])
    dos['Étapes échouées'] = count_echecs(parsed_dos['steps'])
    dos['CMP'] = get_CMP_type(parsed_dos['steps'])
    cc_step = [step['source_url'] for step in parsed_dos['steps'] if step.get('stage') == 'constitutionnalité']
    # dos['Taille de la décision du CC'] = get_decision_length(cc_step[0]) if cc_step else ''
    dos['URL CC'] = cc_step[0] if cc_step else ''
    # dos['Signataires au JO'] = count_signataires(parsed_dos['url_jo']) if 'url_jo' in parsed_dos else ''
    dos['URL JO'] = parsed_dos['url_jo'] if 'url_jo' in parsed_dos else ''
    dos["Législature de promulgation"] = parsed_dos.get('assemblee_legislature', '')

    last_depot = find_last_depot(parsed_dos['steps'])
    last_text = None
    for step in reversed(parsed_dos['steps']):
        last_text = step
        if step.get('step') == 'hemicycle':
            break
        if step.get('step') == 'commission':
            raise Exception('commission as last step')

    if 'source_url' in last_text:
        try:
            articles = parse_texte.parse(last_text['source_url'])
            if articles and articles[0].get('definitif'):
                dos['Nombre de caractères final'] = read_text(parse_texte.parse(last_text['source_url']))
            else:
                dos['Nombre de caractères final'] = get_texte_length(parsed_dos['url_jo']) if 'url_jo' in parsed_dos else ''
        except:
            print("WARNING: Nombre de caractères final impossible to evaluate")

    try:

        input_text_length = read_text(parse_texte.parse(last_depot['source_url']))