from configparser import ConfigParser from pathlib import Path import json _CODE_TO_TYPE_MAPPING = { "l": "Lampe", "x": "Connection", "c": "Contact", #"r": "Relais", # "a": "P24" Cette ligne est retirée car la gestion des types d'alimentation est personnalisée } def load_config(filepath: str) -> ConfigParser: config = ConfigParser() config.read(filepath) return config def get_cp_items(config: ConfigParser) -> tuple[list[tuple[str, int]], tuple[str, int]]: cp_items = list(map(lambda x: (str(x[0]), int(x[1])), config.items("CP"))) cp_items = [x for x in cp_items if x[1] > 0] s_idx = [x[0].lower() for x in cp_items].index("s") s_item = cp_items.pop(s_idx) return cp_items, s_item def process_alimentation_section(section_name, section_items, blocs): x, y = int(section_items["x"]), int(section_items["y"]) m_value = int(section_items.get('m', 0)) # Déterminer le type d'alimentation basé sur m type_val="Unknown" if m_value == 1: type_val = "P24" elif m_value == 2: type_val = "P0" elif m_value == 3: blocs[section_name] = { "type": "Pulse", "valeur": 24.0, "periode": 0.5, "x": x, "y": y } blocs[section_name]={"type": type_val, "x":x, "y": y} print(f"Section: {section_name}, m_value: {m_value}") def process_relay_section(section_name, section_items, blocs): x, y = int(section_items["x"]), int(section_items["y"]) m_value = int(section_items.get('m', 0)) # Déterminer le type de relais basé sur m type_val="Unknown" if m_value == 1: type_val = "Relais" elif m_value == 2: type_val = "RelaisCote" elif m_value == 3: blocs[section_name] = { "type": "Pulse", "valeur": 24.0, "periode": 0.5, "x": x, "y": y } blocs[section_name]={"type": type_val, "x":x, "y": y} print(f"Section: {section_name}, m_value: {m_value}") def process_lamp_section(section_name, section_items, blocs): x, y= int(section_items["x"]), int(section_items["y"]) blocs[section_name] = {"type": "Lamp", "x":x, "y": y} def process_connection_section(section_name, section_items, blocs): x, y= int (section_items["x"]), int(section_items["y"]) blocs[section_name] = {"type": "Noeud", "x":x, "y":y} def process_contact_section(section_name, section_items, blocs): x, y = int(section_items["x"]), int(section_items["y"]) m_value = int(section_items.get('m', 0)) # Déterminer le type du contact basé sur m type_val="Unknown" if m_value == 1 or m_value==2 or m_value==3 or m_value==4: type_val = "Contact" elif m_value == 5 or m_value==6 : type_val = "ContactDouble" elif m_value == 7 or m_value==8 or m_value==9: type_val = "ContactBasculeur" elif m_value==10 or m_value==11 or m_value==16 or m_value==17: type_val = "Levier" elif m_value==12 or m_value==13 or m_value== 14 or m_value==15: type_val= "Boutton" blocs[section_name]={"type": type_val, "x":x, "y": y} print(f"Section: {section_name}, m_value: {m_value}") def process_section(config: ConfigParser, blocs: dict): for section in config.sections(): items = dict(config.items(section)) if section.startswith('A'): # Identifier les sections de type [A] process_alimentation_section(section, items, blocs) elif section.startswith('R'): process_relay_section(section,items, blocs) elif section.startswith('L'): process_lamp_section(section,items, blocs) elif section.startswith('X'): process_connection_section(section, items, blocs) def main(filepath: str): filepath = Path(filepath) filename = filepath.stem config = load_config(filepath) blocs = {} cables = [] _coords_mapping: dict[tuple[str, str], str] = {} cp_items, s_item = get_cp_items(config) process_section(config, blocs) s_code, s_num = s_item for idx in range(int(s_num)): section_name = f"{s_code.upper()}{idx}" section_items = dict(config.items(section_name)) left = section_items["xa"], section_items["ya"] right = section_items["xb"], section_items["yb"] try: left_section_name = _coords_mapping[(left[0], left[1])] except KeyError as error: print(f"Left section name not found for coords: {error}") left_section_name = "UNKNOWN" try: right_section_name = _coords_mapping[(right[0], right[1])] except KeyError as error: print(f"Right section name not found for coords: {error}") right_section_name = "UNKNOWN" cable = [left_section_name, "g", right_section_name, "d"] cables.append(cable) with open(f"{filename}.json", "w") as f: json.dump({"blocs": blocs, "cables": cables}, f, indent=2) if __name__ == "__main__": import sys main(sys.argv[1])