# 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):


import re
import sys
from lawfactory_utils.urls import download, enable_requests_cache
from .common import strip_text
re_reorder = re.compile(r"(Fait(?: (?:à|au) [^,]+,)? le \d.*?<[^>]*>)(.*)(<!-- end texte -->.*Par le Président de la République :)", re.S)
reorder = lambda x: re_reorder.sub(r"\2\1\3", x)
re_signataires = re.compile(r"^.*?((<!-- end texte -->|Fait(?: (?:à|au) [^,]+,)? le \d.*?<[^>]*>).*?)<(font|!-- end signataires).*$", re.S)
signataires = lambda x: re_signataires.sub(r"\1", x)
re_ministre = re.compile(r"le Président de la République|L[ea] (Premi[eè]re?|ministre|garde|secrétaire|haut-commissaire) ")
count_ministres = lambda x: len(re_ministre.findall(x))

def download_texte(url):
    text = download(url).text
    return clean_fioritures(clean_br(text))

def extract_texte(url):
    text = download_texte(url)
    if not re_texte.search(text):
        print("ERROR: could not find texte in JO", url, file=sys.stderr)
        return None
    return strip_text(extract_contenu(text))

def get_texte_length(url):
    text = extract_texte(url)
    if not text:
        return -1
    return len(text)

def extract_signataires(url):
    text_src = reorder(download_texte(url))
    if not re_signataires.search(text_src):
        print("ERROR: could not find signature in texte JO", url, file=sys.stderr)
        return None
    text_src = signataires(text_src)

# 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_jo.py
import re
import sys
from lawfactory_utils.urls import download, enable_requests_cache
from .common import strip_text

re_reorder = re.compile(r"(Fait(?: (?:à|au) [^,]+,)? le \d.*?<[^>]*>)(.*)(<!-- end texte -->.*Par le Président de la République :)", re.S)
reorder = lambda x: re_reorder.sub(r"\2\1\3", x)
re_signataires = re.compile(r"^.*?((<!-- end texte -->|Fait(?: (?:à|au) [^,]+,)? le \d.*?<[^>]*>).*?)<(font|!-- end signataires).*$", re.S)
signataires = lambda x: re_signataires.sub(r"\1", x)
re_ministre = re.compile(r"le Président de la République|L[ea] (Premi[eè]re?|ministre|garde|secrétaire|haut-commissaire) ")
count_ministres = lambda x: len(re_ministre.findall(x))

def download_texte(url):
    text = download(url).text
    return clean_fioritures(clean_br(text))

def extract_texte(url):
    text = download_texte(url)
    if not re_texte.search(text):
        print("ERROR: could not find texte in JO", url, file=sys.stderr)
        return None
    return strip_text(extract_contenu(text))

def get_texte_length(url):
    text = extract_texte(url)
    if not text:
        return -1
    return len(text)

def extract_signataires(url):
    text_src = reorder(download_texte(url))
    if not re_signataires.search(text_src):
        print("ERROR: could not find signature in texte JO", url, file=sys.stderr)
        return None
    text_src = signataires(text_src)

    return strip_text(text_src)