# 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
    enable_requests_cache()

    dossiers_json = find_parsed_doslegs(args[0])

    # silence the output if we use the --quiet flag
    with log_print(only_log=not verbose) as log:
        c = 0
        fixed = 0
        for dos in senat_csv:
            if verbose:
                print()
                print(dos['URL du dossier'])

            senat_id = dos['URL du dossier'].split('/')[-1].replace('.html', '')

            # sometimes the Senate forget to mark the dosleg as promulgated, let's fix it
            if not dos['Date de promulgation']:
                if senat_id in dossiers_json and dossiers_json[senat_id].get('url_jo'):
                    parsed_dos = dossiers_json[senat_id]
                    yy, mm, dd = parsed_dos.get('end').split('-')
                    dos['Date de promulgation'] = '%s/%s/%s' % (dd, mm, yy)
                    dos['État du dossier'] = 'promulgué'
                    # dos['Numéro de la loi'] # TODO: law_name not yet parsed in anpy
                else:
                    continue

            dos['Année initiale'] = annee(dos['Date initiale'])
            dos['Date initiale'] = format_date(dos['Date initiale'])
            dos['Législature initiale'] = guess_legislature(dos['Date initiale'])
            dos['Année de promulgation'] = annee(dos['Date de promulgation'])

# 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

    enable_requests_cache()

    dossiers_json = find_parsed_doslegs(args[0])

    # silence the output if we use the --quiet flag
    with log_print(only_log=not verbose) as log:
        c = 0
        fixed = 0
        for dos in senat_csv:
            if verbose:
                print()
                print(dos['URL du dossier'])

            senat_id = dos['URL du dossier'].split('/')[-1].replace('.html', '')

            # sometimes the Senate forget to mark the dosleg as promulgated, let's fix it
            if not dos['Date de promulgation']:
                if senat_id in dossiers_json and dossiers_json[senat_id].get('url_jo'):
                    parsed_dos = dossiers_json[senat_id]
                    yy, mm, dd = parsed_dos.get('end').split('-')
                    dos['Date de promulgation'] = '%s/%s/%s' % (dd, mm, yy)
                    dos['État du dossier'] = 'promulgué'
                    # dos['Numéro de la loi'] # TODO: law_name not yet parsed in anpy
                else:
                    continue

            dos['Année initiale'] = annee(dos['Date initiale'])
            dos['Date initiale'] = format_date(dos['Date initiale'])
            dos['Législature initiale'] = guess_legislature(dos['Date initiale'])
            dos['Année de promulgation'] = annee(dos['Date de promulgation'])

            dos['Date de promulgation'] = format_date(dos['Date de promulgation'])