pyCreateTh update

This commit is contained in:
Alex38Lyon committed 2026-11-19 18:51:48 +01:00
1 parent f900462feb
commit 98fb6dc75e
10 files changed
+2164 -2660

No files matched your search

+216 -228
View File
@@ -2,12 +2,13 @@
!#############################################################################################
# #
# general_fonctions.py for pyCreateTh.py #
# #
# #
!#############################################################################################
Alex 2025 06 09
"""
import os, logging, sys, re, configparser, unicodedata, shutil
import os, logging, sys, re, configparser, unicodedata, shutil # noqa: I001
from pathlib import Path
import Lib.global_data as global_Data
from datetime import datetime
@@ -21,36 +22,38 @@ log = logging.getLogger("Logger")
# Couleurs ANSI par niveau de log
#################################################################################################
COLOR_CODES = {
logging.DEBUG: "\033[94m", # Bleu
logging.INFO: "\033[92m", # Vert
logging.WARNING: "\033[95m", # MAGENTA
logging.ERROR: "\033[91m", # Rouge
logging.DEBUG: "\033[94m", # Bleu
logging.INFO: "\033[92m", # Vert
logging.WARNING: "\033[95m", # MAGENTA
logging.ERROR: "\033[91m", # Rouge
logging.CRITICAL: "\033[1;91m", # Rouge vif
}
RESET = "\033[0m"
#################################################################################################
# Codes de couleur ANSI
class Colors:
BLACK = '\033[90m'
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
WHITE = '\033[97m'
ERROR = '\033[91m'
WARNING = '\033[95m'
HEADER = '\033[96m'
DEBUG = '\033[94m' # Bleu
INFO = '\033[92m' # Vert
CRITICAL = '\033[1;91m', # Rouge vif
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
BLACK = "\033[90m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
CYAN = "\033[96m"
WHITE = "\033[97m"
ERROR = "\033[91m"
WARNING = "\033[95m"
HEADER = "\033[96m"
DEBUG = "\033[94m" # Bleu
INFO = "\033[92m" # Vert
CRITICAL = ("\033[1;91m",) # Rouge vif
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
#################################################################################################
# fonction pour réduire l'affichage des chemins long #
@@ -68,12 +71,12 @@ def safe_relpath(path, base_dir=None, max_depth=4, max_name_len=50, prefix="~"):
# ── 1. Résolution du chemin ──────────────────────────────────────────────
try:
path = Path(path).expanduser().resolve()
except Exception:
except Exception: # noqa: BLE001
return str(path)
try:
base = Path(base_dir).expanduser().resolve() if base_dir else Path.cwd().resolve()
except Exception:
except Exception: # noqa: BLE001
base = None
# ── 2. Nom du fichier / dossier (tronqué) ─────────────────────────────────
@@ -89,7 +92,7 @@ def safe_relpath(path, base_dir=None, max_depth=4, max_name_len=50, prefix="~"):
parts = list(rel.parts)
else:
raise ValueError
except Exception:
except Exception: # noqa: BLE001
parts = list(path.parts)
# ── 4. Cas path == base → "." ────────────────────────────────────────────
@@ -108,11 +111,10 @@ def safe_relpath(path, base_dir=None, max_depth=4, max_name_len=50, prefix="~"):
# ── 7. Construction finale sûre ──────────────────────────────────────────
try:
return os.path.join(*parts)
except Exception:
except Exception: # noqa: BLE001
return name
#################################################################################################
# Coloration des messages d'aide d'arg #
#################################################################################################
@@ -124,27 +126,27 @@ def colored_help(parser):
parser (argparse.ArgumentParser): Le parseur d'arguments.
Returns:
None
"""
# Captures the help output
help_text = parser.format_help()
# Coloration des différentes parties
colored_help_text = help_text.replace(
'usage:', f'{Colors.ERROR}usage:{Colors.ENDC}'
).replace(
'options:', f'{Colors.GREEN}options:{Colors.ENDC}'
).replace('positional arguments:', f'{Colors.BLUE}positional arguments:{Colors.ENDC}'
).replace(', --help', f'{Colors.BLUE}, --help:{Colors.ENDC}'
).replace('elp:', f'{Colors.BLUE}elp{Colors.ENDC}')
colored_help_text = (
help_text.replace("usage:", f"{Colors.ERROR}usage:{Colors.ENDC}")
.replace("options:", f"{Colors.GREEN}options:{Colors.ENDC}")
.replace("positional arguments:", f"{Colors.BLUE}positional arguments:{Colors.ENDC}")
.replace(", --help", f"{Colors.BLUE}, --help:{Colors.ENDC}")
.replace("elp:", f"{Colors.BLUE}elp{Colors.ENDC}")
)
# Surligner les arguments
for action in parser._actions:
if action.option_strings:
# Colorer les options (--xyz)
for opt in action.option_strings:
colored_help_text = colored_help_text.replace(opt, f'{Colors.BLUE}{opt}{Colors.ENDC}').replace('--help', f'{Colors.BLUE}--help:{Colors.ENDC}')
colored_help_text = colored_help_text.replace(opt, f"{Colors.BLUE}{opt}{Colors.ENDC}").replace("--help", f"{Colors.BLUE}--help:{Colors.ENDC}")
# Imprimer le texte coloré
print(colored_help_text)
sys.exit(1)
@@ -164,25 +166,25 @@ def sanitize_filename(thName):
Returns:
str: The cleaned and compatible string.
"""
# Unicode normalization to replace accented characters with their non-accented equivalents
thName = unicodedata.normalize('NFKD', thName).encode('ASCII', 'ignore').decode('ASCII')
thName = unicodedata.normalize("NFKD", thName).encode("ASCII", "ignore").decode("ASCII")
# Replace parentheses with underscores
thName = thName.replace('(', '_').replace(')', '_')
thName = thName.replace("(", "_").replace(")", "_")
# Replace illegal characters with an underscore
thName = re.sub(r'[<>:"/\\|?*\']', '_', thName) # Illegal on Windows
thName = re.sub(r'\s+', '_', thName) # Spaces to underscores
thName = re.sub(r'[^a-zA-Z0-9._-]', '_', thName) # Keep only allowed chars
thName = re.sub(r'[<>:"/\\|?*\']', "_", thName) # Illegal on Windows
thName = re.sub(r"\s+", "_", thName) # Spaces to underscores
thName = re.sub(r"[^a-zA-Z0-9._-]", "_", thName) # Keep only allowed chars
# Convert to lowercase, then capitalize the first letter
# thName = thName.lower().capitalize()
# thName = thName.capitalize()
# Suppression des underscores en début et fin
thName = thName.strip('_')
thName = thName.strip("_")
return thName or "default_filename" # Avoid empty result
@@ -197,24 +199,24 @@ def select_file_tk_window():
"""
# Créer une instance de la fenêtre tkinter
root = tk.Tk()
# Cacher la fenêtre principale
root.withdraw()
# Afficher la boite de dialogue de sélection de fichier
file_path = filedialog.askopenfilename(
title="Select your file",
filetypes=[
("Compatibles files", "*.th *.mak *.dat *.tro *.trox"),
("MAK files", "*.mak"),
("TH files", "*.th"),
("DAT files", "*.dat"),
("TRO files", "*.tro"),
("TROX files", "*.trox"),
("All files", "*.*")]
)
filetypes=[
("Compatibles files", "*.th *.mak *.dat *.tro *.trox"),
("MAK files", "*.mak"),
("TH files", "*.th"),
("DAT files", "*.dat"),
("TRO files", "*.tro"),
("TROX files", "*.trox"),
("All files", "*.*"),
],
)
return file_path # Retourner le chemin complet du fichier sélectionné
@@ -237,58 +239,53 @@ def load_config_old(args, configIni="config.ini"):
config.read(config_file, encoding="utf-8")
survey_keys = {
'Author': 'Author',
'Copyright1': 'Copyright',
'Copyright2': 'Copyright',
'Copyright3': 'Copyright',
'Copyright_Short': None,
'map_comment': 'mapComment',
'club': 'club',
'thanksto': 'thanksto',
'datat': 'datat',
'wpage': 'wpage',
'cs': 'cs'
"Author": "Author",
"Copyright1": "Copyright",
"Copyright2": "Copyright",
"Copyright3": "Copyright",
"Copyright_Short": None,
"map_comment": "mapComment",
"club": "club",
"thanksto": "thanksto",
"datat": "datat",
"wpage": "wpage",
"cs": "cs",
}
for key, attr in survey_keys.items():
if 'Survey_Data' in config and key in config['Survey_Data']:
if key.startswith('Copyright') and all(
k in config['Survey_Data'] for k in ['Copyright1', 'Copyright2', 'Copyright3']
):
global_Data.Copyright = "\n".join([
config['Survey_Data']['Copyright1'],
config['Survey_Data']['Copyright2'],
config['Survey_Data']['Copyright3']
])
global_Data.CopyrightShort = config['Survey_Data']['Copyright_Short']
if "Survey_Data" in config and key in config["Survey_Data"]:
if key.startswith("Copyright") and all(k in config["Survey_Data"] for k in ["Copyright1", "Copyright2", "Copyright3"]):
global_Data.Copyright = "\n".join([config["Survey_Data"]["Copyright1"], config["Survey_Data"]["Copyright2"], config["Survey_Data"]["Copyright3"]])
global_Data.CopyrightShort = config["Survey_Data"]["Copyright_Short"]
elif attr:
setattr(global_Data, attr, config['Survey_Data'][key])
setattr(global_Data, attr, config["Survey_Data"][key])
app_keys = {
'template_path': 'templatePath',
'station_by_scrap': ('stationByScrap', int),
'final_therion_exe': ('finalTherionExe', lambda x: x.lower() == 'true'),
'parse_tro_files_by_explo': ('parse_tro_files_by_explo', lambda x: x.lower() == 'true'),
'therion_path': 'therionPath',
'survey_prefix_name': 'SurveyPrefixName',
'shot_lines_in_th2_files': ('linesInTh2', lambda x: x.lower() == 'true'),
'station_name_in_th2_files': ('stationNamesInTh2', lambda x: x.lower() == 'true'),
'wall_lines_in_th2_files': ('wallLinesInTh2', lambda x: x.lower() == 'true'),
'kSmooth': ('kSmooth', float)
"template_path": "templatePath",
"station_by_scrap": ("stationByScrap", int),
"final_therion_exe": ("finalTherionExe", lambda x: x.lower() == "true"),
"parse_tro_files_by_explo": ("parse_tro_files_by_explo", lambda x: x.lower() == "true"),
"therion_path": "therionPath",
"survey_prefix_name": "SurveyPrefixName",
"shot_lines_in_th2_files": ("linesInTh2", lambda x: x.lower() == "true"),
"station_name_in_th2_files": ("stationNamesInTh2", lambda x: x.lower() == "true"),
"wall_lines_in_th2_files": ("wallLinesInTh2", lambda x: x.lower() == "true"),
"kSmooth": ("kSmooth", float),
}
for key, value in app_keys.items():
if 'Application_Data' in config and key in config['Application_Data']:
if "Application_Data" in config and key in config["Application_Data"]:
attr, caster = (value, str) if isinstance(value, str) else value
setattr(global_Data, attr, caster(config['Application_Data'][key]))
setattr(global_Data, attr, caster(config["Application_Data"][key]))
return config_file
except Exception as e:
except Exception as e: # noqa: BLE001
log.critical(f"Reading {configIni} file error: {Colors.ENDC}{e}")
exit(0)
sys.exit(0)
#################################################################################################
def load_config(args, configIni="config.ini", profile="Default"):
"""
Charge un fichier de configuration .ini et initialise les variables globales.
@@ -300,7 +297,6 @@ def load_config(args, configIni="config.ini", profile="Default"):
Exemple : Survey_Data:PSM
"""
try:
# Chemin potentiel du fichier config
config_file = os.path.join(os.path.dirname(args.file), configIni)
if not os.path.isfile(config_file):
@@ -333,51 +329,39 @@ def load_config(args, configIni="config.ini", profile="Default"):
# ----------------------------------------------------------
survey_keys = {
'Author': 'Author',
'Copyright1': 'Copyright',
'Copyright2': 'Copyright',
'Copyright3': 'Copyright',
'Copyright_Short': None,
'map_comment': 'mapComment',
'club': 'club',
'thanksto': 'thanksto',
'datat': 'datat',
'wpage': 'wpage',
'cs': 'cs'
"Author": "Author",
"Copyright1": "Copyright",
"Copyright2": "Copyright",
"Copyright3": "Copyright",
"Copyright_Short": None,
"map_comment": "mapComment",
"club": "club",
"thanksto": "thanksto",
"datat": "datat",
"wpage": "wpage",
"cs": "cs",
}
# ==========================================================
# Variables de substitution
# ==========================================================
variables = {
"YEAR": str(datetime.now().year)
}
variables = {"YEAR": str(datetime.now().year)} # noqa: DTZ005
def expand(value):
try:
return value.format(**variables)
except Exception:
except Exception: # noqa: BLE001
return value
# ==========================================================
# Lecture Survey_Data
# ==========================================================
survey_keys = {
'Author': 'Author',
'map_comment': 'mapComment',
'club': 'club',
'thanksto': 'thanksto',
'datat': 'datat',
'wpage': 'wpage',
'cs': 'cs'
}
survey_keys = {"Author": "Author", "map_comment": "mapComment", "club": "club", "thanksto": "thanksto", "datat": "datat", "wpage": "wpage", "cs": "cs"}
for key, attr in survey_keys.items():
if key in survey_cfg:
value = expand(survey_cfg[key])
setattr(global_Data, attr, value)
@@ -389,75 +373,56 @@ def load_config(args, configIni="config.ini", profile="Default"):
copyright_lines = []
for key in ("Copyright1", "Copyright2", "Copyright3"):
if key in survey_cfg:
copyright_lines.append(
expand(survey_cfg[key])
)
copyright_lines.append(expand(survey_cfg[key]))
if copyright_lines:
global_Data.Copyright = "\n".join(
copyright_lines
)
global_Data.Copyright = "\n".join(copyright_lines)
if "Copyright_Short" in survey_cfg:
global_Data.CopyrightShort = expand(
survey_cfg["Copyright_Short"]
)
global_Data.CopyrightShort = expand(survey_cfg["Copyright_Short"])
# ----------------------------------------------------------
# Lecture Application_Data
# ----------------------------------------------------------
app_keys = {
'template_path': 'templatePath',
'station_by_scrap': ('stationByScrap', int),
'final_therion_exe': ('finalTherionExe', lambda x: x.lower() == 'true'),
'parse_tro_files_by_explo': ('parse_tro_files_by_explo', lambda x: x.lower() == 'true'),
'therion_path': 'therionPath',
'survey_prefix_name': 'SurveyPrefixName',
'shot_lines_in_th2_files': ('linesInTh2', lambda x: x.lower() == 'true'),
'station_name_in_th2_files': ('stationNamesInTh2', lambda x: x.lower() == 'true'),
'wall_lines_in_th2_files': ('wallLinesInTh2', lambda x: x.lower() == 'true'),
'kSmooth': ('kSmooth', float)
"template_path": "templatePath",
"station_by_scrap": ("stationByScrap", int),
"final_therion_exe": ("finalTherionExe", lambda x: x.lower() == "true"),
"parse_tro_files_by_explo": ("parse_tro_files_by_explo", lambda x: x.lower() == "true"),
"therion_path": "therionPath",
"survey_prefix_name": "SurveyPrefixName",
"shot_lines_in_th2_files": ("linesInTh2", lambda x: x.lower() == "true"),
"station_name_in_th2_files": ("stationNamesInTh2", lambda x: x.lower() == "true"),
"wall_lines_in_th2_files": ("wallLinesInTh2", lambda x: x.lower() == "true"),
"kSmooth": ("kSmooth", float),
}
if 'Application_Data' in config:
if "Application_Data" in config:
for key, value in app_keys.items():
if key not in config['Application_Data']:
if key not in config["Application_Data"]:
continue
attr, caster = (
(value, str)
if isinstance(value, str)
else value
)
attr, caster = (value, str) if isinstance(value, str) else value
setattr(
global_Data,
attr,
caster(config['Application_Data'][key])
)
setattr(global_Data, attr, caster(config["Application_Data"][key]))
# log.info(f"Configuration profile : {profile}")
return config_file, profile
except Exception as e:
log.critical(f"Reading config file : {Colors.ENDC}{configIni}{Colors.ERROR} error : {Colors.ENDC}{e}" )
exit(0)
except Exception as e: # noqa: BLE001
log.critical(f"Reading config file : {Colors.ENDC}{configIni}{Colors.ERROR} error : {Colors.ENDC}{e}")
sys.exit(0)
#################################################################################################
# Supprime les codes ANSI (pour l'écriture dans les fichiers)
#################################################################################################
def strip_ansi_codes(text):
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
return ansi_escape.sub('', text)
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
return ansi_escape.sub("", text)
#################################################################################################
@@ -486,11 +451,11 @@ class FileFormatter(logging.Formatter):
args=(),
exc_info=record.exc_info,
func=record.funcName,
sinfo=record.stack_info
sinfo=record.stack_info,
)
return super().format(record_copy)
#################################################################################################
# Fonction de configuration du logger
#################################################################################################
@@ -498,9 +463,9 @@ def setup_logger(logfile="app.log", debug_log=False):
logger = logging.getLogger("Logger")
logger.setLevel(logging.DEBUG)
logger.handlers.clear()
min_level = logging.DEBUG if debug_log else logging.INFO
# Console stderr handler — affichage à l'écran avec couleurs
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(min_level)
@@ -514,7 +479,7 @@ def setup_logger(logfile="app.log", debug_log=False):
file_formatter = FileFormatter("%(asctime)s - %(levelname)s - %(message)s") # <-- Ta classe personnalisée
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
return logger
@@ -533,103 +498,105 @@ def copy_template_if_not_exists(template_path, destination_path):
try:
if not os.path.exists(destination_path):
# If the destination folder does not exist, copy the template
shutil.copytree(template_path, destination_path)
shutil.copytree(template_path, destination_path)
log.info(f"The folder {Colors.ENDC}{template_path}{Colors.GREEN} has been copied to {Colors.ENDC}{safe_relpath(destination_path)}{Colors.GREEN}")
else:
log.warning(f"The folder '{Colors.ENDC}{safe_relpath(destination_path)}{Colors.WARNING}' already exists. No files were copied.")
except Exception as e:
except Exception as e: # noqa: BLE001
log.critical(f"Copy template error: {Colors.ENDC}{e}")
exit(0)
#################################################################################################
sys.exit(0)
#################################################################################################
def add_copyright_header(file_path, copyright_text):
# Lire le contenu du fichier
with open(file_path, 'r', encoding="utf-8") as file:
with open(file_path, "r", encoding="utf-8") as file:
content = file.readlines()
# Vérifier si le copyright est déjà présent
if not any("copyright" in line.lower() for line in content):
# Ajouter le copyright en en-tête
content.insert(0, f"{copyright_text}\n")
# Réécrire le fichier avec le copyright ajouté
with open(file_path, 'w', encoding="utf-8") as file:
file.writelines(content)
# Réécrire le fichier avec le copyright ajouté
with open(file_path, "w", encoding="utf-8") as file:
file.writelines(content)
#################################################################################################
def copy_file_with_copyright(th_file, destination_path, copyright_text):
# Vérifier si le fichier existe
if os.path.exists(th_file):
# Créer le dossier de destination s'il n'existe pas
os.makedirs(destination_path, exist_ok=True)
_destFile = sanitize_filename(os.path.basename(th_file)[:-3]) + ".th"
# Copier le fichier vers le dossier de destination
dest_file = os.path.join(destination_path, _destFile)
shutil.copy(th_file, dest_file)
# Ajouter le copyright dans l'en-tête si nécessaire
add_copyright_header(dest_file, copyright_text)
log.debug(f"File {Colors.ENDC}{safe_relpath(th_file)}{Colors.GREEN} has been copied to {Colors.ENDC}{safe_relpath(destination_path)}{Colors.GREEN} with the copyright header added.{Colors.ENDC}")
log.debug(
f"File {Colors.ENDC}{safe_relpath(th_file)}{Colors.GREEN} has been copied to {Colors.ENDC}{safe_relpath(destination_path)}{Colors.GREEN} with the copyright header added.{Colors.ENDC}"
)
else:
log.error(f"The file .th does not exist {Colors.ENDC}{safe_relpath(th_file)}")
global_Data.error_count += 1
#################################################################################################
# Remplir les template avec les variables vers output_path # #
# Remplir les template avec les variables vers output_path # #
#################################################################################################
def update_template_files(template_path, variables, output_path):
"""
Process a Therion template file by replacing variables.
Args:
template_path (str): Path to the original template file
variables (dict): Dictionary of variables to replace
output_path (str): Path for the new configuration file
Returns:
None
"""
try:
# Read the content of the template file
with open(template_path, 'r', encoding='utf-8') as file:
with open(template_path, "r", encoding="utf-8") as file:
content = file.read()
# Replace variables
for var, value in variables.items():
# Use regex to replace {variable} with its value
pattern = r'\{' + re.escape(var) + r'\}'
pattern = r"\{" + re.escape(var) + r"\}"
content = re.sub(pattern, str(value), content)
# Write the new file
with open(output_path, 'w', encoding='utf-8') as file:
with open(output_path, "w", encoding="utf-8") as file:
file.write(content)
log.info(f"Update template successfully: {Colors.ENDC}{safe_relpath(output_path)}")
# Delete the original template file
os.remove(template_path)
except FileNotFoundError:
log.error(f"Template file {Colors.ENDC}{template_path}{Colors.ERROR} not found")
global_Data.error_count += 1
except PermissionError:
log.error(f"Insufficient permissions to write the file")
log.error("Insufficient permissions to write the file")
global_Data.error_count += 1
except Exception as e:
except Exception as e: # noqa: BLE001
log.error(f"An error occurred (update_template_files : {Colors.ENDC}{os.path.basename(template_path)}{Colors.ERROR}) : {Colors.ENDC}{e}")
global_Data.error_count += 1
#################################################################################################
#################################################################################################
def load_text_file_utf8(filepath, short_filename):
"""Loads a text file with various encodings and converts it to UTF-8.
@@ -642,41 +609,62 @@ def load_text_file_utf8(filepath, short_filename):
"""
encodings_to_try = [
'utf-8-sig', # UTF-8 avec BOM
'utf-8', # UTF-8 standard
'windows-1252', # ANSI Windows Europe de l’Ouest
'iso-8859-15', # ISO-8859-15 (latin9), remplace iso-8859-1 (latin1)
'iso-8859-1',
"utf-8-sig", # UTF-8 avec BOM
"utf-8", # UTF-8 standard
"windows-1252", # ANSI Windows Europe de l’Ouest
"iso-8859-15", # ISO-8859-15 (latin9), remplace iso-8859-1 (latin1)
"iso-8859-1",
]
for enc in encodings_to_try:
try:
with open(filepath, 'r', encoding=enc) as f:
with open(filepath, "r", encoding=enc) as f:
content = f.read()
log.info(f"Open source file: {Colors.ENDC}{short_filename}{Colors.GREEN}, with encoding: {Colors.ENDC}{enc}{Colors.GREEN} and conversion to {Colors.ENDC}utf-8")
log.info(
f"Open source file: {Colors.ENDC}{short_filename}{Colors.GREEN}, with encoding: {Colors.ENDC}{enc}{Colors.GREEN} and conversion to {Colors.ENDC}utf-8"
)
message_log = f"* Source file: {short_filename}, encoding: {enc}, conversion to utf-8\n"
return content, message_log, enc
except UnicodeDecodeError as e:
log.debug(f"Failed {Colors.ENDC}{enc}{Colors.DEBUG} for {Colors.ENDC}{short_filename}{Colors.DEBUG}: {Colors.ENDC}{e}")
continue
except Exception as e:
except Exception as e: # noqa: BLE001
log.critical(f"Unexpected error while reading {Colors.ENDC}{short_filename}{Colors.CRITICAL}: {e}")
exit(0)
sys.exit(0)
return None, "", None
# Dernier recours : lecture binaire + forçage
try:
with open(filepath, 'rb') as f:
with open(filepath, "rb") as f:
raw = f.read()
content = raw.decode('windows-1252', errors='replace')
content = raw.decode("windows-1252", errors="replace")
log.warning(f"Force-reading {Colors.ENDC}{short_filename}{Colors.WARNING} with character replacement (windows-1252)")
message = f"* Force-reading source file: {short_filename} with character replacement (windows-1252)\n"
return content, message, 'windows-1252'
except Exception as e:
log.critical(f"Failed to read file {Colors.ENDC}{short_filename}{Colors.CRITICAL}: {Colors.ENDC}{e}")
exit(0)
message = f"* Force-reading source file: {short_filename} with character replacement (windows-1252)\n"
return content, message, "windows-1252"
except Exception as e: # noqa: BLE001
log.critical(f"Failed to read file {Colors.ENDC}{short_filename}{Colors.CRITICAL}: {Colors.ENDC}{e}")
sys.exit(0)
return None, "", None
#################################################################################################
def read_text_file(filename):
"""Lecture avec prise en charge de plusieurs encodages."""
for encoding in ("utf-8-sig", "cp1252", "latin-1"):
try:
with open(filename, "r", encoding=encoding) as f:
content = f.read()
if encoding != "utf-8-sig":
log.info(f"Fichier : {Colors.ENDC}{safe_relpath(filename)}{Colors.INFO} lu en {Colors.ENDC}{encoding}")
return content
except UnicodeDecodeError:
continue
raise UnicodeError(f"{Colors.ERROR}Impossible de décoder {Colors.ENDC}{filename}")