def shared( markdown_dir: str = paths.shared_markdown_dir, typescript: bool = True, client_output_dir: str = paths.shared_client_models_dir, python: bool = True, api_output_dir: str = paths.shared_api_models_dir, ) -> None: # pragma: no cover """ Generate shared definitions. """ files = glob.glob(os.path.join(markdown_dir, "*.md")) with typer.progressbar(files) as progress: for filename in progress: if filename[-6:] == "poc.md": continue typer.echo(f"Processing {filename}...") md = load_md(filename) if typescript: outputs = render_markdown_as_typescript(md) for name, content in outputs: write(os.path.join(client_output_dir, name), content) if python: outputs = render_markdown_as_python(md) for name, content in outputs: write(os.path.join(api_output_dir, name), content) typer.echo(f"Processed {len(files)} shared definitions.")
def thematiques( markdown_file: str = typer.Option(paths.thematique_markdown_file, "--markdown", "-md"), output_dir: str = typer.Option(paths.thematique_client_output_dir, "--output", "-o"), output_typescript: bool = typer.Option(True, "--typescript", "-ts"), output_json: bool = typer.Option(False, "--json"), ) -> None: """ Convert 'thematiques' markdown to code. """ markdown = load_md(markdown_file) thematiques = build_thematiques(markdown) if output_json: data = json.dumps(thematiques, indent=4) filename = os.path.join(output_dir, "thematiques.json") write(filename, data) if output_typescript: typescript = render_thematiques_as_typescript(thematiques) filename = os.path.join(output_dir, "thematiques.ts") write(filename, typescript) typer.echo(f"Rendered {len(thematiques)} 'thematiques' in {output_dir}.")
def mesures( doc_file: str = typer.Option("../referentiels/sources/citergie.docx", "--docx", "-d"), correspondance_xlsx: str = typer.Option( "../referentiels/sources/correspondance_citergie_climat_pratique.xlsx", "--correspondance", "-c", ), output_dir: str = typer.Option("../referentiels/markdown/mesures_citergie", "--output", "-o"), ) -> None: """ Convert source docx file to 'mesures' markdown files. """ typer.echo(f"Loading docx file: '{doc_file}'...") document = load_docx(doc_file) typer.echo(f"Reading citergie document...") mesures = docx_to_mesures(document) typer.echo(f"Found {len(mesures)} 'mesures'!") typer.echo(f"Reading correspondance table...") add_climat_pratic(mesures, correspondance_xlsx) with typer.progressbar(mesures) as progress: for mesure in progress: filename = os.path.join(output_dir, f"{mesure['id']}.md") md = mesure_to_markdown_legacy(mesure) write(filename, md) typer.echo( f"All {len(mesures)} 'mesures' were exported in '{output_dir}' as markdown files." )
def indicateurs_citergie( indicateurs_xlsx: str = typer.Option( "../referentiels/sources/indicateurs_citergie.xlsx", "--indicateurs", "-i"), correspondance_xlsx: str = typer.Option( "../referentiels/sources/correspondance_citergie_climat_pratique.xlsx", "--correspondance", "-c", ), output_dir: str = typer.Option( "../referentiels/markdown/indicateurs_citergie", "--output", "-o"), ) -> None: """ Convert source xlsx files to 'indicateurs' markdown files. """ typer.echo(f"Parsing files...") indicators = parse_indicators_xlsx(indicateurs=indicateurs_xlsx, correspondance=correspondance_xlsx) mds = indicators_to_markdowns_legacy(indicators) with typer.progressbar(mds.items()) as progress: for number, md in progress: filename = os.path.join(output_dir, f"{number}.md") write(filename, md) typer.echo( f"All {len(mds)} 'indicateurs' were exported in '{output_dir}' as markdown files." )
def indicateurs_thematiques( indicateurs_dir=paths.indicateurs_markdown_dir, thematiques_file=paths.thematique_markdown_file, ) -> None: """ Regenerate (overwrite) markdown files using thematiques """ markdown = load_md(thematiques_file) thematiques = build_thematiques(markdown) thematiques_lookup = {thematiques[id]: id for id in thematiques.keys()} indicateur_files = glob.glob(os.path.join(indicateurs_dir, "*.md")) for filename in indicateur_files: typer.echo(f"Processing {filename}...") md = load_md(filename) indicators = build_indicators(md) for indicator in indicators: # -- extract this for future regen function on indicator -- climat_pratic = indicator["climat_pratic"] indicator["climat_pratic_ids"] = [ thematiques_lookup[name] for name in climat_pratic ] # --- md = indicators_to_markdown_legacy_2(indicators) write(filename, md)
def mesures_thematiques( mesures_dir=paths.mesures_markdown_dir, thematiques_file=paths.thematique_markdown_file, ) -> None: """ Regenerate (overwrite) markdown files using thematiques """ markdown = load_md(thematiques_file) thematiques = build_thematiques(markdown) thematiques_lookup = {thematiques[id]: id for id in thematiques.keys()} mesures_files = glob.glob(os.path.join(mesures_dir, "*.md")) with typer.progressbar(mesures_files) as progress: for filename in progress: md = load_md(filename) mesure = build_mesure(md) # -- extract this for future regen function on mesures -- climat_pratic = mesure["climat_pratic"] mesure["climat_pratic_id"] = thematiques_lookup[climat_pratic] # --- md = mesure_to_markdown(mesure) write(filename, md) typer.echo(f"All {len(mesures_files)} 'mesures' were regenerated.")
def indicateurs_universal( indicateurs_old_dir="../referentiels/markdown/indicateurs_citergie", indicateurs_dir="../referentiels/markdown/indicateurs", ) -> None: """ Regenerate (overwrite) markdown files in a new format """ old_files = glob.glob(os.path.join(indicateurs_old_dir, "*.md")) for filename in old_files: typer.echo(f"Processing {filename}...") md = load_md(filename) indicateurs = build_indicators(md) new_md = "" for indicateur in indicateurs: # -- extract this for future regen function on indicator -- indicateur["id"] = "cae-" + str(indicateur["id"]) indicateur["actions"] = [] for mesure_id in indicateur["mesures"]: indicateur["actions"].append("climat_air_energie/" + mesure_id) indicateur["obligation_cae"] = indicateur["obligation_citergie"] indicateur["programmes"] = ["climat_air_energie"] if indicateur["pcaet"]: indicateur["programmes"].append("pcaet") new_md += indicateur_to_markdown(indicateur) + "\n" # --- base_name = os.path.basename(filename) number = re.findall(r"\d+", base_name)[0] new_filename = f"cae_{int(number):03d}.md" write(os.path.join(indicateurs_dir, new_filename), new_md)
def orientations( referentiel_file: str = "../referentiels/sources/referentiel_eci_v3_v4_sobr_principes_nettoyes.xlsx", output_dir: str = orientations_markdown_dir, ) -> None: orientations = parse_referentiel_eci_xlsx(referentiel_file) with typer.progressbar(orientations) as progress: for orientation in progress: markdown = action_to_markdown(orientation) filename = os.path.join(output_dir, f"{orientation['id']}.md") write(filename, markdown) typer.echo( f"All {len(orientations)} 'orientations' were exported in '{output_dir}' as markdown files." )
def mesures_nested_actions(mesures_dir=paths.mesures_markdown_dir, ) -> None: """ Regenerate (overwrite) markdown files using nested actions """ mesures_files = glob.glob(os.path.join(mesures_dir, "*.md")) count = 0 with typer.progressbar(mesures_files) as progress: for filename in progress: md = load_md(filename) # todo make this recursive. mesure = build_mesure(md) for action in mesure["actions"]: if "description" not in action.keys(): continue action["actions"] = [] description = str(action["description"]) stripped_description = "" lines = description.splitlines() for line in lines: if line.startswith("- "): index = len(action["actions"]) + 1 action["actions"].append({ "id": f"{action['id']}.{index}", "nom": line.lstrip("- "), }) elif action[ "actions"]: # the line does is not an item, add it to the current action description line = line.strip() if line: stripped_description += f"{line}\n" if action[ "actions"]: # we have consumed the description lines to create sub actions count += len(action["actions"]) action["description"] = stripped_description md = mesure_to_markdown(mesure) # temp_name = os.path.join("../referentiels/markdown/mesures_temp", os.path.basename(filename)) write(filename, md) typer.echo( f"All {len(mesures_files)} 'mesures' were regenerated extracting {count} 'tasks'." )
def actions( mesures_markdown_dir=paths.mesures_markdown_dir, orientations_markdown_dir=paths.orientations_markdown_dir, client_output_dir=paths.shared_client_data_dir, shared_api_data_dir=paths.shared_api_data_dir, output_typescript=True, output_python=True, ) -> None: # citergie files = sorted_files(mesures_markdown_dir, "md") actions_citergie = [] for file in files: md = load_md(file) action = build_action(md) actions_citergie.append(action) relativize_ids(actions_citergie, "citergie") citergie = referentiel_from_actions(actions_citergie, id="citergie", name="Climat Air Énergie") # economie circulaire files = sorted_files(orientations_markdown_dir, "md") actions_economie_circulaire = [] for file in files: md = load_md(file) action = build_action(md) actions_economie_circulaire.append(action) relativize_ids(actions_economie_circulaire, "economie_circulaire") economie_circulaire = referentiel_from_actions( actions_economie_circulaire, id="economie_circulaire", name="Economie circulaire", ) all_actions = actions_citergie + actions_economie_circulaire all_actions = clean_thematiques(all_actions) all_actions = propagate_thematiques(all_actions) all_actions = remove_top_nodes(all_actions) if output_typescript: # actions list (soon to be deprecated) typescript = render_actions_as_typescript(all_actions) filename = os.path.join(client_output_dir, "actions_referentiels.ts") write(filename, typescript) # two referentiels typescript = render_actions_as_typescript( [citergie, economie_circulaire]) filename = os.path.join(client_output_dir, "referentiels.ts") write(filename, typescript) if output_python: python = render_actions_as_python([citergie, economie_circulaire]) filename = os.path.join(shared_api_data_dir, "referentiels.py") write(filename, python)
def indicateurs_eci( indicateurs_xlsx: str = "../referentiels/sources/indicateurs_eci_proposition_4.xlsx", output_dir: str = typer.Option("../referentiels/markdown/indicateurs", "--output", "-o"), ) -> None: """ Convert source xlsx files to 'indicateurs' markdown files. """ typer.echo(f"Parsing files...") indicateurs = parse_indicateurs_eci_xlsx(indicateurs_xlsx=indicateurs_xlsx) with typer.progressbar(indicateurs) as progress: for indicateur in progress: md = indicateur_to_markdown(indicateur) filename = os.path.join( output_dir, f"{indicateur['id'].replace('-', '_')}.md") write(filename, md) typer.echo( f"All {len(indicateurs)} 'indicateurs' were exported in '{output_dir}' as markdown files." )
def domaines( doc_file: str = "../referentiels/sources/citergie.docx", output_dir: str = "../referentiels/markdown/mesures_citergie", ) -> None: """ Convert source docx file to 'actions' markdown files. """ typer.echo(f"Loading docx file: '{doc_file}'...") document = load_docx(doc_file) typer.echo(f"Reading citergie document...") actions = docx_to_parent_actions(document) typer.echo(f"Found {len(actions)} 'domaines'!") with typer.progressbar(actions) as progress: for action in progress: filename = os.path.join(output_dir, f"{action['id']}.md") md = action_to_markdown(action) write(filename, md) typer.echo( f"All {len(actions)} 'domaines' were exported in '{output_dir}' as markdown files." )
def indicateurs( indicateurs_markdown_dir: str = typer.Option( paths.indicateurs_markdown_dir, "--markdown", "-md"), client_output_dir: str = paths.shared_client_data_dir, typescript: bool = True, ) -> None: """ Convert 'indicateurs' markdown files to code. """ files = sorted_files(indicateurs_markdown_dir, "md") indicators = [] for filename in files: typer.echo(f"Processing {filename}...") md = load_md(filename) indicators.extend(indicateurs_builder(md)) if typescript: rendered = render_indicators_as_typescript(indicators) filename = os.path.join(client_output_dir, "indicateurs_referentiels.ts") write(filename, rendered) typer.echo(f"Rendered {len(files)} 'indicateurs' in {client_output_dir}.")
def correspondance_table( orientations_dir=orientations_markdown_dir, correspondance_file: str = "../referentiels/sources/dteci_correspondance.json", output_dir: str = "../referentiels/data", ) -> None: """ Regenerate (overwrite) markdown files in a new format """ files = sorted_files(orientations_dir, "md") actions_economie_circulaire = [] for file in files: md = load_md(file) action = build_action(md) action["climat_pratic_id"] = "eci" actions_economie_circulaire.append(action) # relativize_ids(actions_economie_circulaire, 'economie_circulaire') economie_circulaire = referentiel_from_actions( actions_economie_circulaire, id="economie_circulaire", name="Economie circulaire", ) def actionById(id: str, actions: List[dict]) -> dict: for action in actions: if action["id"] == id: return action elif id.startswith(action["id"]): return actionById(id, action["actions"]) def parentId(action: dict) -> str: ns = action["id"].split(".") ns.pop() return ".".join(ns) with open(correspondance_file, encoding="utf-8") as file: axes = json.load(file) for axe in axes: for orientation in axe["orientations"]: for niveau in orientation["niveaux"]: id = niveau["id"] action = actionById(id, economie_circulaire["actions"]) print(id) if not action or "indicateur" not in niveau.keys(): continue indicateur = niveau["indicateur"] # handle oui non if "question" in indicateur.keys(): question = indicateur["question"] if action["actions"]: # ex 3.1.1 indicateur[ "raison"] = f"{len(action['actions'])} actions pour un seul niveau en oui non" continue question["oui"]["faite"] = [action["id"]] # handle many oui non elif "questions" in indicateur.keys(): questions = indicateur["questions"] if not action["actions"]: indicateur[ "raison"] = "Pas de sous actions pour plusieurs oui non" continue noms = [action["nom"] for action in action["actions"]] for question in questions.keys(): choice, score = process.extractOne(question, noms) chosen = [ action for action in action["actions"] if action["nom"] == choice ][0] questions[question]["oui"]["faite"] = [ chosen["id"] ] questions[question]["oui"]["raison"] = ( f'"{action["id"]} {option["nom"]}" ' f'ressemble à {score}% à "{choice}"') # handle fonction elif "fonction" in indicateur.keys(): indicateur[ "raison"] = "Pas de correspondance pour une fonction" # handle interval elif "interval" in indicateur.keys(): indicateur[ "raison"] = "Pas de correspondance pour des intervalles de valeurs" # handle intervalles elif "intervalles" in indicateur.keys(): indicateur[ "raison"] = "Pas de correspondance pour des intervalles de valeurs" # handle checkboxes elif "choix" in indicateur.keys(): choix = indicateur["choix"] if not action["actions"]: indicateur[ "raison"] = f"pas de sous actions à {action['id']} pour ce niveau à choix multiple" continue if len(choix) > len(action["actions"]): indicateur[ "raison"] = f"plus d'options ({len(choix)}) que d'actions ({len(action['actions'])})" continue noms = [action["nom"] for action in action["actions"]] for option in choix: choice, score = process.extractOne( option["nom"], noms) chosen = [ action for action in action["actions"] if action["nom"] == choice ][0] option["faite"] = [chosen["id"]] option[ "raison"] = f'"{action["id"]} {option["nom"]}" ressemble à {score}% à "{choice}"' # handle dropdown elif "liste" in indicateur.keys(): liste = indicateur["liste"] if not action["actions"]: indicateur[ "raison"] = f"pas de sous actions à {action['id']} pour ce niveau à liste" continue if len(liste) > len(action["actions"]): indicateur[ "raison"] = f"plus d'options ({len(liste)}) que d'actions ({len(action['actions'])})" continue noms = [action["nom"] for action in action["actions"]] for option in liste: choice, score = process.extractOne( option["nom"], noms) chosen = [ action for action in action["actions"] if action["nom"] == choice ][0] i = int(chosen["id"].split(".")[-1]) option["faite"] = [chosen["id"]] option["faite"] = [ f"{parentId(chosen)}.{n}" for n in range(1, i + 1) ] option[ "raison"] = f'"{action["id"]} {option["nom"]}" ressemble à {score}% à "{choice}"' print(option["faite"]) write( os.path.join(output_dir, "correspondance_table.json"), json.dumps(axes, indent=4, ensure_ascii=False), )