script pyCreateTh

This commit is contained in:
Alex38Lyon
2026-01-06 14:20:34 +01:00
parent 4ebc6e0a4f
commit b7010fc18d
67 changed files with 143450 additions and 141799 deletions
+57 -32
View File
@@ -4,7 +4,9 @@ general_fonctions.py for pyCreateTh.py
#############################################################################################
"""
import os, logging, sys, re, configparser, unicodedata, shutil
import Lib.global_data as global_data
from pathlib import Path
import Lib.global_data as global_Data
import tkinter as tk
from tkinter import filedialog
@@ -45,34 +47,56 @@ class Colors:
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
#################################################################################################
def safe_relpath(path):
abs_path = os.path.abspath(path)
ref_path = os.path.abspath(os.getcwd())
try:
valeur = "~\\" + os.path.relpath(path, ref_path)
return valeur
except ValueError:
max_depth = 4 # Profondeur maximale pour tronquer le chemin
# Disques différents, afficher le chemin relatif partiel depuis la racine commune
path_parts = abs_path.split(os.sep)
ref_parts = ref_path.split(os.sep)
while path_parts and ref_parts and path_parts[0] == ref_parts[0]:
path_parts.pop(0)
ref_parts.pop(0)
result = os.path.join(*path_parts) if path_parts else os.path.basename(path)
# fonction pour réduire l'affichage des chemins long #
#################################################################################################
def safe_relpath( path, base_dir=None, max_depth=4, max_name_len=50, prefix="~" ):
"""
Retourne un chemin lisible et sûr pour affichage (logs / UI).
# Si max_depth est défini, tronque le chemin
if max_depth is not None:
parts = result.split(os.sep)
if len(parts) > max_depth:
result = os.path.join("~\\" , *parts[-max_depth:])
return result
- Compatible Windows / Linux / macOS
- Tronque la profondeur du chemin
- Tronque le nom de fichier si trop long
- Ne lève jamais d'exception
"""
try:
path = Path(path).expanduser().resolve()
except Exception:
return str(path)
try:
base = Path(base_dir).expanduser().resolve() if base_dir else Path.cwd().resolve()
except Exception:
base = None
# 1️⃣ Nom du fichier (ou dossier) — tronqué si nécessaire
name = path.name
if len(name) > max_name_len:
stem = path.stem[: max_name_len - 6]
name = f"{stem}...{path.suffix}"
# 2️⃣ Tentative de chemin relatif
try:
if base:
rel = path.relative_to(base)
parts = list(rel.parts)
else:
raise ValueError
except Exception:
parts = list(path.parts)
# 3️⃣ Limitation de profondeur
if max_depth is not None and len(parts) > max_depth:
parts = parts[-max_depth:]
parts.insert(0, prefix)
# 4️⃣ Remplacement du nom si tronqué
if parts:
parts[-1] = name
# 5️⃣ Construction finale portable
return os.path.join(*parts)
#################################################################################################
@@ -216,19 +240,20 @@ def load_config(args, configIni="config.ini"):
if key.startswith('Copyright') and all(
k in config['Survey_Data'] for k in ['Copyright1', 'Copyright2', 'Copyright3']
):
global_data.Copyright = "\n".join([
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']
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'),
@@ -240,7 +265,7 @@ def load_config(args, configIni="config.ini"):
for key, value in app_keys.items():
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
@@ -422,6 +447,6 @@ def update_template_files(template_path, variables, output_path):
global_Data.error_count += 1
except Exception as e:
log.error(f"An error occurred (update_template_files): {Colors.ENDC}{e}")
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
+12
View File
@@ -31,6 +31,18 @@ stationNamesInTh2 = True
wallLinesInTh2 = True
kSmooth = 0.5
XVIScale = 100
parse_tro_files_by_explo = True
#################################################################################################
THERION_KEYWORDS = {
"centerline", "end centerline",
"date", "units", "flags", "team",
"calibrate", "data", "declination",
"fix", "station", "equate",
"cs", "extend", "infer",
"instrument", "grade", "clino", "compass", "length", "reverse", "topofil", "duplicate", "not",
}
#################################################################################################
totfile = """\t## Survey file:
@@ -5,6 +5,8 @@
# Copyright (c) 2020 Xavier Robert <xavier.robert@ird.fr>
# SPDX-License-Identifier: GPL-3.0-or-later
# Modifié Alex 2026 01 04 -> print remplacés par log.xxx
"""
Script to build Therion files
@@ -35,6 +37,12 @@ from __future__ import division
# Import modules
import sys, os, datetime
import os, logging, sys
from Lib.general_fonctions import Colors
import Lib.global_data as global_data
log = logging.getLogger("Logger")
########################
def builddictcave(thlang = u'en', icomments = True, icoupe = True, Errfiles = True,
@@ -258,7 +266,7 @@ def writethconfig(pdata, icomments, icoupe, thlang, dictcave,
f2w.closed
print(u'\tFile ' + pdata + u' written...')
log.info(u'\tFile ' + pdata + u' written...')
return
@@ -1072,7 +1080,7 @@ def writethc(pdata, cavename = None, istructure = True):
# close the config.thc file
f1w.closed
print(u'\tFile ' + pdata + u' written...')
log.info(u'\tFile ' + pdata + u' written...')
return
@@ -1102,10 +1110,10 @@ def checkfiles(pdata, Errorfiles = True):
raise NameError(u'ERROR : File {FileNa} does exist'.format(FileNa=str(pdata)))
#sys.exit('ERROR : File {FileNa} does exist'.format(FileNa=str(pdata)))
else:
print(u'WARNING: I have erased file %s' % pdata)
log.warning(f"I have erased the file {Colors.ENDC}{pdata}")
#######
#######}
if __name__ == "__main__":
# build dictionnaries
@@ -1130,12 +1138,12 @@ if __name__ == "__main__":
if not Errfiles :
checkfiles(thcpath + thcfnme)
else:
print(u'WARNING: I will erase previous ' + thcpath + thcfnme + u' files !')
log.warning(f"I will erase previous {Colors.ENDC}{thcpath} {thcfnme}{Colors.WARNING} files !")
else:
if not Errfiles :
checkfiles(thcfnme)
else:
print(u'WARNING: I will erase previous ' + thcfnme + u' files !')
log.warning(f"I will erase previous {Colors.ENDC}{thcfnme}{Colors.WARNING} files !")
if thconfigfnme[-9:] != u'.thconfig':
thconfigfnme = thconfigfnme + u'.thconfig'
@@ -1145,12 +1153,12 @@ if __name__ == "__main__":
if not Errfiles:
checkfiles(thconfigpath + thconfigfnme)
else:
print(u'WARNING: I will erase previous ' + thconfigpath + thconfigfnme + u' files !')
log.warning(f"I will erase previous {Colors.ENDC}{thconfigpath} {thconfigfnme}{Colors.WARNING} files !")
else:
if not Errfiles :
checkfiles(thconfigfnme)
else:
print(u'WARNING: I will erase previous ' + thconfigfnme + u' files !')
log.warning(f"I will erase previous {Colors.ENDC}{thconfigfnme}{Colors.WARNING} files !")
# build thc file
if thcfile :
+29 -25
View File
@@ -5,6 +5,7 @@
# Copyright (c) 2020 Xavier Robert <xavier.robert@ird.fr>
# SPDX-License-Identifier: GPL-3.0-or-later
# Modifié Alex 2025 07 01
# Modifié Alex 2026 01 04 -> print remplacés par log.xxx
"""
!---------------------------------------------------------!
@@ -54,7 +55,8 @@ from .vtopotools import *
from .datathwritetools import *
from .buildthconfig import *
from Lib.general_fonctions import Colors
import os, logging, sys
from Lib.general_fonctions import Colors, safe_relpath
import Lib.global_data as global_data
log = logging.getLogger("Logger")
@@ -159,12 +161,12 @@ def tro2th(fle_tro_fnme = None, fle_th_fnme = None,
elif thlang in [u'en',u'EN', u'En', u'eN']: thlang = u'en'
else: raise NameError(u'ERROR: Language %s not implemented\n'
u' Use "en" instead' % thlang )
print(u'____________________________________________________________\n\n\t\tTRO 2 THERION\n____________________________________________________________\n')
log.info(u'____________________________________________________________\n\n\t\tTRO 2 THERION\n____________________________________________________________\n')
if thlang == u'fr':
print(u'\nEcrit par Xavier Robert, Groupe spéléo Vulcain - Lyon, France\n')
log.info(u'\nÉcrit par Xavier Robert, Groupe spéléo Vulcain - Lyon, France\n')
elif thlang == u'en':
print(u'\nWritten by Xavier Robert, Groupe spéléo Vulcain - Lyon, France\n')
print(u'____________________________________________________________\n\n')
log.info(u'\nWritten by Xavier Robert, Groupe spéléo Vulcain - Lyon, France\n')
log.info(u'____________________________________________________________\n\n')
coordsyst = None
coordinates = None
@@ -178,20 +180,20 @@ def tro2th(fle_tro_fnme = None, fle_th_fnme = None,
if fle_th_fnme is None:
# convert tro file to th file
print('1')
cavename, coordinates, coordsyst, fle_th_fnme = convert_tro(fle_tro_fnme, fle_tro_encoding=fle_tro_encoding,
log.info('1')
entrance, cavename, coordinates, coordsyst, fle_th_fnme = convert_tro(fle_tro_fnme, fle_tro_encoding=fle_tro_encoding,
icomments = icomments, icoupe = icoupe, istructure = istructure,
thlang = thlang, Errorfiles = Errorfiles)
else:
print(2)
cavename, coordinates, coordsyst, fle_th_fnme = convert_tro(fle_tro_fnme, fle_th_fnme, cavename,
log.info(2)
entrance, cavename, coordinates, coordsyst, fle_th_fnme = convert_tro(fle_tro_fnme, fle_th_fnme, cavename,
icomments = icomments, icoupe = icoupe, istructure = istructure,
thlang = thlang, Errorfiles = Errorfiles)
if thlang == u'fr': print(u'\tFichier Therion %s construit à partir des données %s' %(fle_th_fnme, fle_tro_fnme))
elif thlang == u'en': print(u'\tFile %s built from %s' %(fle_th_fnme, fle_tro_fnme))
if thlang == u'fr': log.info(u'\tFichier Therion %s construit à partir des données %s' %(fle_th_fnme, fle_tro_fnme))
elif thlang == u'en': log.info(u'\tFile %s built from %s' %(fle_th_fnme, fle_tro_fnme))
else:
if thlang == u'fr': print(u'\tPas de fichier .tro en entrée, pas de fichier de données .th créé...')
elif thlang == u'en': print(u'\tNo .tro File input, no .th data file created...')
if thlang == u'fr': log.info(u'\tPas de fichier .tro en entrée, pas de fichier de données .th créé...')
elif thlang == u'en': log.info(u'\tNo .tro File input, no .th data file created...')
# Build here the new structure:
if istructure: build_structure(u'cave', Errorfiles = True)
@@ -255,22 +257,22 @@ def tro2th(fle_tro_fnme = None, fle_th_fnme = None,
f3w = open(cavename.replace(u' ', u'_') + '/' + cavename.replace(u' ', u'_') + '-tot.th', 'w')
write_thtot(f3w, cavename, icomments, thlang)
f3w.closed
print(u'\tFile ' + cavename.replace(u' ', u'_') + u'/' + cavename.replace(u' ', u'_') + u'-tot.th written...\n')
log.info(u'\tFile ' + cavename.replace(u' ', u'_') + u'/' + cavename.replace(u' ', u'_') + u'-tot.th written...\n')
# build cavename-maps.th file
f4w = open(cavename.replace(u' ', u'_') + u'/' + cavename.replace(u' ', u'_') + '-maps.th', 'w')
write_thmaps(f4w, cavename, icomments, thlang)
f4w.closed
print(u'\tFile ' + cavename.replace(u' ', u'_') + u'/' + cavename.replace(u' ', u'_') + u'-maps.th written...\n\n')
log.info(u'\tFile ' + cavename.replace(u' ', u'_') + u'/' + cavename.replace(u' ', u'_') + u'-maps.th written...\n\n')
# build Legends/entrances-coordinates.th file
f5w = open(cavename.replace(u' ', u'_') + '/Legends/entrances_coordinates.th', 'w')
write_thcoords(f5w, cavename, coordinates, coordsyst, icomments, thlang)
f5w.closed
print(u'\tFile ' + cavename.replace(u' ', u'_') + u'/Legends/entrances_coordinates.th written...\n\n')
log.info(u'\tFile ' + cavename.replace(u' ', u'_') + u'/Legends/entrances_coordinates.th written...\n\n')
print(u'____________________________________________________________')
print(u'')
log.info(u'____________________________________________________________')
log.info(u'')
return
@@ -311,7 +313,7 @@ def build_structure(cavename, Errorfiles = True):
# Stop
raise NameError(u'ERROR : Folder {FileNa} does exist'.format(FileNa=str(cavename.replace(u' ', u'_'))))
else:
print(u'WARNING: I have erased folder %s' % cavename.replace(u' ', u'_'))
log.warning(f"I have erased the folder {Colors.ENDC}{cavename.replace(u' ', u'_')}")
if not os.path.exists(cavename.replace(u' ', u'_') + u'/Data'): os.mkdir(cavename.replace(u' ', u'_') + u'/Data')
if not os.path.exists(cavename.replace(u' ', u'_') + u'/Legends'): os.mkdir(cavename.replace(u' ', u'_') + u'/Legends')
if not os.path.exists(cavename.replace(u' ', u'_') + u'/Outputs'):
@@ -350,7 +352,7 @@ def mkfle_output_txt(cavename):
f1w.write(u'Folder where Therion outputs are exported \n\n')
# close the cavename/Outputs/outputs.txt file
f1w.closed
print(u'\tFile ' + cavename.replace(u' ', u'_') + u'/Outputs/outputs.txt written...')
log.info(u'\tFile ' + cavename.replace(u' ', u'_') + u'/Outputs/outputs.txt written...')
return
@@ -381,6 +383,7 @@ def convert_tro(fle_tro_fnme, fle_tro_encoding=None, fle_th_fnme = None, cavenam
cavename : Name of the cave from the .tro file
coordinates : Coordinates of the entrance
coordsyst : Coordinates system used by the .tro file
entrance : Entrance station
USAGE:
cavename, coordsyst = convert_tro(fle_tro_fnme, [fle_th_fnme = fle_th_fnme, cavename = cavename, Errorfiles = Errorfiles])
@@ -399,8 +402,8 @@ def convert_tro(fle_tro_fnme, fle_tro_encoding=None, fle_th_fnme = None, cavenam
#alt=0.
# open the .tro survey
if thlang == u'fr': log.info(f"Travail sur le fichier VisualTopo: {Colors.ENDC}{fle_tro_fnme}")
elif thlang == u'en':log.info(f"Processing VisualTopo file: {Colors.ENDC}{fle_tro_fnme}")
log.info(f"Processing VisualTopo file: {Colors.ENDC}{safe_relpath(fle_tro_fnme)}")
# print(' ')
fle_tro = open(fle_tro_fnme, 'r', encoding=fle_tro_encoding)
# read the .tro file
@@ -429,13 +432,13 @@ def convert_tro(fle_tro_fnme, fle_tro_encoding=None, fle_th_fnme = None, cavenam
# read the header
coordinates = None
cavename, coordinates, coordsyst, club, entrance, versionfle = read_vtopo_header(lines)
if cavename is None or cavename == '' or cavename == ' ':
cavename = u'cave'
if fle_th_fnme is None:
fle_th_fnme = cavename.replace(u' ', u'_') + u'.th'
print (fle_th_fnme)
log.info (fle_th_fnme)
if fle_th_fnme[-3:] != u'.th':
fle_th_fnme = fle_th_fnme + u'.th'
@@ -449,6 +452,7 @@ def convert_tro(fle_tro_fnme, fle_tro_encoding=None, fle_th_fnme = None, cavenam
# open the .th file
if istructure: fle_th = open (cavename.replace(u' ', u'_') + '/Data/' + fle_th_fnme, 'w')
else: fle_th = open (fle_th_fnme, 'w')
# write the .th header
writeheader_th(fle_th, cavename, entrance)
@@ -488,7 +492,7 @@ def convert_tro(fle_tro_fnme, fle_tro_encoding=None, fle_th_fnme = None, cavenam
if thlang == u'fr': log.info(f"Fichier Therion {Colors.ENDC}{fle_th_fnme}{Colors.INFO} écrit !")
elif thlang == u'en': log.info(f"Therion file {Colors.ENDC}{fle_th_fnme}{Colors.INFO} written!")
return cavename, coordinates, coordsyst, fle_th_fnme
return entrance, cavename, coordinates, coordsyst, fle_th_fnme
#################################################################################################
+18 -9
View File
@@ -23,8 +23,6 @@ def compile_template(template, template_args, totReadMeError = "", **kwargs ):
tmpdir = tempfile.mkdtemp()
config = template.format(**template_args, tmpdir=tmpdir.replace("\\", "/"))
log.debug(f"{config}\n")
config_file = join(tmpdir, "config.thconfig")
@@ -51,13 +49,15 @@ def compile_template(template, template_args, totReadMeError = "", **kwargs ):
try:
with open(log_file, "r", encoding="cp1252", errors="replace") as f:
logfile = f.read()
except Exception as log_err:
log.warning(f"Could not read Therion log: {Colors.ENDC}{log_err}")
# Analyse du code retour
if result.returncode != 0 or "press any key" in result.stdout.lower():
log.error(f"Therion compilation failed with return code: {Colors.ENDC}{result.returncode}\n{Colors.WHITE}{result.stdout}")
totReadMeError += f"\tTherion compilation failed with return code: {result.returncode}\n"
selector_value = template_args['selector']
log.error(f"Therion compilation {Colors.ENDC}{selector_value}{Colors.ERROR}, failed with return code: {Colors.ENDC}{result.returncode}\n{Colors.WHITE}{result.stdout}")
totReadMeError += f"\tTherion compilation {selector_value}, failed with return code: {result.returncode}\n"
global_data.error_count += 1
return "Therion error", tmpdir, totReadMeError
@@ -178,16 +178,24 @@ def compile_file_th(filepath, **kwargs):
#################################################################################################
# Attention fonctionne pour la version therion en français ! à voir pour les autres langues
lengthre = re.compile(r".*Longueur totale de la topographie = \s*(\S+)m")
depthre = re.compile(r".*Longueur totale verticale =\s*(\S+)m")
fr_lengthre = re.compile(r".*Longueur totale de la topographie = \s*(\S+)m")
fr_depthre = re.compile(r".*Longueur totale verticale =\s*(\S+)m")
en_lengthre = re.compile(r".*Total length of survey legs = \s*(\S+)m")
en_depthre = re.compile(r".*Total vertical length of survey legs =\s*(\S+)m")
def get_stats_from_log(log):
lenmatch = lengthre.findall(log)
depmatch = depthre.findall(log)
lenmatch = fr_lengthre.findall(log)
depmatch = fr_depthre.findall(log)
if len(lenmatch) == 0 and len(depmatch) == 0:
lenmatch = en_lengthre.findall(log)
depmatch = en_depthre.findall(log)
if len(lenmatch) == 1 and len(depmatch) == 1:
return {"length": lenmatch[0], "depth": depmatch[0]}
return {"length": 0, "depth": 0}
return {"length": 0.0, "depth": 0.0}
#################################################################################################
@@ -198,4 +206,5 @@ def get_syscoord_from_log(log):
if len(lenmatch) == 1:
return {"syscoord": lenmatch[0]}
return {"syscoord": 0}