# Path: tlfp/tools/json2arbo.py
# def mkdirs(d):
# def get_step_id(nstep, step):
# def process(dos, OUTPUT_DIR):
#     def log_err(txt):
# 
# Path: tlfp/tools/prepare_articles.py
# def getParentFolder(root, f):
# def unifyStatus(status):
# def create_step(step_id, article=None, echec_type=None):
# def mark_missing_articles_as_deleted(articles, old_step_id, step_id, last_match_with_previous_step, current_match):
# def check_text_match_dosleg(url, senat_id):
# def reordered_steps(procedure):
# def process(procedure):
# 
# Path: tlfp/tools/update_procedure.py
# def detect_auteur_depot_from_url(url):
# def add_auteur_depot(step):
# def remove_interventions_too_small(step, intervs):
# def process(procedure, articles, intervs={}):
# 
# Path: tlfp/tools/prepare_amendements.py
# def process(OUTPUT_DIR, procedure):
#     def simplify_sort(sort):
#     def first_author(signataires):
#     def find_groupe(amd, typeparl, urlapi):
#     def add_link(links, pA, pB, weight=1):
#     def sort_amendements(texte, amendements):
#         def solveorder(art):
#     CACHE_BUSTING = 'cache=%d' % time()
#         CACHE_BUSTING = 'cache=lfdll-prod' # fixed cache busting for promulgated laws
# 
# Path: tlfp/tools/prepare_interventions.py
# def clean_orga(orga):
# def init_section(dic, key, inter, order):
# def add_intervs(dic, key, inter):
# def save_hash(i, dico, val=None):
# def get_hash(i, dico):
# def process(OUTPUT_DIR, procedure):
#     def save_gm(i):
#     def get_gm(i):
#     def save_rap(i):
#     def get_rap(i):
#     def save_o_g(i, gpe):
#     def get_o_g(i):
# DEBUG = '--debug' in sys.argv
# 
# Path: tlfp/tools/reorder_interventions_and_correct_procedure.py
# def list_get_or_none(arr, i):
# def process(OUTPUT_DIR, procedure):
# 
# Path: tlfp/tools/compute_stats.py
# def count_words(content):
# def has_been_censored(dos):
# def find_amendements(path):
# def read_alineas(art):
# def read_text(step):
# def step_word_count(step):
# def step_text_length(step):
# def count_initial_depots(steps):
# def count_navettes(steps):
# def count_texts(steps):
# def read_articles(step):
# def count_censored_articles(step):
# def find_first_and_last_steps(dos, include_CC=True):
# def add_amendments_stats(stats, amendements):
# def process(output_dir, dos):
# 
# Path: tlfp/tools/add_links.py
# STATS_BLACKLIST = [
#     'loi',
#     'ordonnance',
#     'décret',
#     'arrété',
#     'circulaire',
#     'directive',
#     'règlement'
# ]
# def get_code(candidat):
# def process(dos):
# 
# Path: tlfp/tools/common.py
# def debug_file(data, filename):
#     if '--debug' in sys.argv:
#         path = os.path.join('debug', filename)
#         print_json(data, path)
#         print('   DEBUG - dumped', path)
# 
# 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 os, shutil, io
from .tools import json2arbo, prepare_articles, update_procedure, \
    prepare_amendements, prepare_interventions, reorder_interventions_and_correct_procedure, \
    compute_stats, add_links
from .tools.common import debug_file, print_json
        err_log = os.path.join(api_dir, err_dir, textid)
        if os.path.exists(err_log):
            os.remove(err_log)


def process(dos, OUTPUT_DIR, log=io.StringIO()):
    dos['id'] = dos.get('senat_id', dos.get('assemblee_id'))

    output_dir = os.path.join(OUTPUT_DIR, dos['id'] + '_tmp')
    final_output_dir = os.path.join(OUTPUT_DIR, dos['id'])
    print('     writing to:', dos['id'] + '_tmp')

    shutil.rmtree(output_dir, ignore_errors=True)

    # avoid duplicate titles
    if 'short_title' in dos:
        if " de loi organique" in dos['long_title']:
            dos['short_title'] += " (texte organique)"
    # AN doslegs have no short_titles
    else:
        dos['short_title'] = dos['long_title']

    debug_file(dos, 'before_add_links.json')
    dos = add_links.process(dos)

    # add texte.json and write all the text files tree
    debug_file(dos, 'before_json2arbo.json')
    dos = json2arbo.process(dos, output_dir + '/procedure')

    print(' - process article versions')

# Path: tlfp/tools/json2arbo.py
# def mkdirs(d):
# def get_step_id(nstep, step):
# def process(dos, OUTPUT_DIR):
#     def log_err(txt):
# 
# Path: tlfp/tools/prepare_articles.py
# def getParentFolder(root, f):
# def unifyStatus(status):
# def create_step(step_id, article=None, echec_type=None):
# def mark_missing_articles_as_deleted(articles, old_step_id, step_id, last_match_with_previous_step, current_match):
# def check_text_match_dosleg(url, senat_id):
# def reordered_steps(procedure):
# def process(procedure):
# 
# Path: tlfp/tools/update_procedure.py
# def detect_auteur_depot_from_url(url):
# def add_auteur_depot(step):
# def remove_interventions_too_small(step, intervs):
# def process(procedure, articles, intervs={}):
# 
# Path: tlfp/tools/prepare_amendements.py
# def process(OUTPUT_DIR, procedure):
#     def simplify_sort(sort):
#     def first_author(signataires):
#     def find_groupe(amd, typeparl, urlapi):
#     def add_link(links, pA, pB, weight=1):
#     def sort_amendements(texte, amendements):
#         def solveorder(art):
#     CACHE_BUSTING = 'cache=%d' % time()
#         CACHE_BUSTING = 'cache=lfdll-prod' # fixed cache busting for promulgated laws
# 
# Path: tlfp/tools/prepare_interventions.py
# def clean_orga(orga):
# def init_section(dic, key, inter, order):
# def add_intervs(dic, key, inter):
# def save_hash(i, dico, val=None):
# def get_hash(i, dico):
# def process(OUTPUT_DIR, procedure):
#     def save_gm(i):
#     def get_gm(i):
#     def save_rap(i):
#     def get_rap(i):
#     def save_o_g(i, gpe):
#     def get_o_g(i):
# DEBUG = '--debug' in sys.argv
# 
# Path: tlfp/tools/reorder_interventions_and_correct_procedure.py
# def list_get_or_none(arr, i):
# def process(OUTPUT_DIR, procedure):
# 
# Path: tlfp/tools/compute_stats.py
# def count_words(content):
# def has_been_censored(dos):
# def find_amendements(path):
# def read_alineas(art):
# def read_text(step):
# def step_word_count(step):
# def step_text_length(step):
# def count_initial_depots(steps):
# def count_navettes(steps):
# def count_texts(steps):
# def read_articles(step):
# def count_censored_articles(step):
# def find_first_and_last_steps(dos, include_CC=True):
# def add_amendments_stats(stats, amendements):
# def process(output_dir, dos):
# 
# Path: tlfp/tools/add_links.py
# STATS_BLACKLIST = [
#     'loi',
#     'ordonnance',
#     'décret',
#     'arrété',
#     'circulaire',
#     'directive',
#     'règlement'
# ]
# def get_code(candidat):
# def process(dos):
# 
# Path: tlfp/tools/common.py
# def debug_file(data, filename):
#     if '--debug' in sys.argv:
#         path = os.path.join('debug', filename)
#         print_json(data, path)
#         print('   DEBUG - dumped', path)
# 
# 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/format_data_for_frontend.py
import os, shutil, io
from .tools import json2arbo, prepare_articles, update_procedure, \
    prepare_amendements, prepare_interventions, reorder_interventions_and_correct_procedure, \
    compute_stats, add_links
from .tools.common import debug_file, print_json

        err_log = os.path.join(api_dir, err_dir, textid)
        if os.path.exists(err_log):
            os.remove(err_log)


def process(dos, OUTPUT_DIR, log=io.StringIO()):
    dos['id'] = dos.get('senat_id', dos.get('assemblee_id'))

    output_dir = os.path.join(OUTPUT_DIR, dos['id'] + '_tmp')
    final_output_dir = os.path.join(OUTPUT_DIR, dos['id'])
    print('     writing to:', dos['id'] + '_tmp')

    shutil.rmtree(output_dir, ignore_errors=True)

    # avoid duplicate titles
    if 'short_title' in dos:
        if " de loi organique" in dos['long_title']:
            dos['short_title'] += " (texte organique)"
    # AN doslegs have no short_titles
    else:
        dos['short_title'] = dos['long_title']

    debug_file(dos, 'before_add_links.json')
    dos = add_links.process(dos)

    # add texte.json and write all the text files tree
    debug_file(dos, 'before_json2arbo.json')
    dos = json2arbo.process(dos, output_dir + '/procedure')

    print(' - process article versions')

    json2arbo.mkdirs(os.path.join(output_dir, 'viz'))