+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Modules Python
+import sys
+from os import path, listdir, system, chdir, environ, remove
+from argparse import ArgumentParser
+import shutil
+import re
+
+import salome
+from execution.telemac_cas import TelemacCas, get_dico
+
+# Adding Eficas to PYTHONPATH
+sys.path.append(environ["EFICAS_NOUVEAU_ROOT"])
+try:
+ import Telemac.prefs
+except ImportError as excp:
+ print("Add the path to eficas to PYTHONPATH")
+if hasattr(Telemac.prefs, 'encoding'):
+ # Hack pour changer le codage par defaut des strings
+ import sys
+ reload(sys)
+ sys.setdefaultencoding(prefs.encoding)
+ del sys.setdefaultencoding
+ # Fin hack
+
+HEADER = '\033[95m'
+OKBLUE = '\033[94m'
+OKGREEN = '\033[92m'
+WARNING = '\033[93m'
+FAIL = '\033[91m'
+ENDC = '\033[0m'
+BOLD = '\033[1m'
+UNDERLINE = '\033[4m'
+
+PREFIX = {'telemac2d':'t2d',
+ 'telemac3d':'t3d',
+ 'tomawac':'tom',
+ 'sisyphe':'sis',
+ 'artemis':'art',
+ 'waqtel':'waq'}
+
+def diff_cas(cas1, cas2):
+ """
+ Diff between two cases will return True if cas.get for each keyword (in
+ both files) returns the same thing
+
+ @param cas1 (TelemacCas) cas1
+ @param cas2 (TelemacCas) cas2
+
+ @returns True if identical
+ """
+
+ # Get the list of keys from cas1 and cas2
+ keys = list(cas1.values.keys())
+ keys.extend(key for key in cas2.values.keys() if key not in keys)
+
+ diff = True
+ for key in keys:
+ val1 = cas1.get(key)
+ val2 = cas2.get(key)
+ if val1 != val2:
+ diff = False
+ print("Differences for {}\n{}: {}\n{}:"\
+ .format(key,
+ path.basename(cas1.file_name),
+ val1,
+ path.basename(cas2.file_name),
+ val2,
+ ))
+
+ return diff
+
+
+def read_write_eficas(module, steering_file, eficas_steering):
+ """
+ Import a steergin file in Eficas and save it back
+
+ @param module Name of the telemac-mascaret module
+ @param steering_file Name of the steering file
+ @param eficas_file Name of the steering file written by Eficas
+ """
+ from InterfaceQT4.eficas_go import getEficasSsIhm
+ code = 'TELEMAC'
+ my_eficas = getEficasSsIhm(code='TELEMAC',labelCode=module)
+
+ handler = my_eficas.fileOpen(steering_file)
+ if not handler:
+ raise Exception(steering_file, "Eficas crashed")
+ if not handler.isJdcValid():
+ report = handler.getJdcRapport()
+ raise Exception(steering_file, report)
+
+ handler.fileSaveAs(eficas_steering)
+
+def validate_catalog(module, root_dir):
+ """
+ Validate a given Catalog for a given module
+
+ @param module Name of the module
+ @param root_dir Telemac root path
+ """
+ print(" "*2, "~> For module", module)
+ examples_dir = path.join(root_dir, 'examples', module)
+ output_dir = path.join(root_dir, 'examples', 'eficas')
+
+ telemac_dico = path.join(root_dir, 'sources', module, module+'.dico')
+
+ crashed = []
+ different = []
+ for example in sorted(listdir(examples_dir)):
+ example_dir = path.join(examples_dir, example)
+ chdir(example_dir)
+ print(" "*3, "~> In example folder ", example_dir)
+ for case in sorted(listdir(example_dir)):
+ if case.endswith('.cas') and \
+ "_reecrit" not in case and \
+ case[0:3] == PREFIX[module] and \
+ "_reecrit" not in case and \
+ "_ori" not in case:
+ # Adding lang extension (.fr for translated french case)
+ print(" "*4, "~> For test case ", case)
+ root, _ = path.splitext(case)
+
+ lang = ''
+ eficas_case = root + "_reecrit.cas"
+
+ # Import and export in eficas
+ try:
+ read_write_eficas(module, case, eficas_case)
+ except Exception as e:
+ print(e)
+ crashed.append(case)
+ print(" "*8+FAIL+"FAILED"+ENDC)
+ print(" "*8+"Crashed in eficas")
+ continue
+
+ ori_cas = TelemacCas(case, telemac_dico)
+ eficas_cas = TelemacCas(eficas_case, telemac_dico)
+
+ isdiff = diff_cas(ori_cas, eficas_cas)
+
+ if not isdiff:
+ different.append(case)
+ print(" "*8+FAIL+"FAILED"+ENDC)
+ print(" "*8+"Diff in steering case")
+ continue
+
+ # Clean up of files
+ remove(eficas_case)
+
+ # Passed the test case
+ print(" "*8+OKGREEN+"PASSED"+ENDC)
+
+ if crashed != []:
+ print("The following test in", module,
+ " crashed in eficas:", crashed)
+ if different != []:
+ print("The following test in", module,
+ " have a difference with normal run:", different)
+
+salome.salome_init()
+
+#-----------------
+#-- Eficas
+#-----------------
+
+for module in ['telemac2d', 'telemac3d']:
+ # Testing loading of catalog
+ validate_catalog(module, environ["HOMETEL"])
+