+ return self.__filestring
+
+# ==============================================================================
+class ImportDetector(object):
+ """
+ Détection des caractéristiques de fichiers ou objets en entrée
+ """
+ __slots__ = (
+ "__url", "__usr", "__root", "__end")
+ def __enter__(self): return self
+ def __exit__(self, exc_type, exc_val, exc_tb): return False
+ #
+ def __init__(self, __url, UserMime=""):
+ if __url is None:
+ raise ValueError("The name or url of the file object has to be specified.")
+ if __url is bytes:
+ self.__url = __url.decode()
+ else:
+ self.__url = str(__url)
+ if UserMime is bytes:
+ self.__usr = UserMime.decode().lower()
+ else:
+ self.__usr = str(UserMime).lower()
+ (self.__root, self.__end) = os.path.splitext(self.__url)
+ #
+ mimetypes.add_type('application/numpy.npy', '.npy')
+ mimetypes.add_type('application/numpy.npz', '.npz')
+ mimetypes.add_type('application/dymola.sdf', '.sdf')
+ if sys.platform.startswith("win"):
+ mimetypes.add_type('text/plain', '.txt')
+ mimetypes.add_type('text/csv', '.csv')
+ mimetypes.add_type('text/tab-separated-values', '.tsv')
+ #
+ # File related tests
+ # ------------------
+ def is_local_file(self):
+ if os.path.isfile(os.path.realpath(self.__url)):
+ return True
+ else:
+ return False
+ def is_not_local_file(self):
+ if not os.path.isfile(os.path.realpath(self.__url)):
+ return True
+ else:
+ return False
+ def raise_error_if_not_local_file(self):
+ if not os.path.isfile(os.path.realpath(self.__url)):
+ raise ValueError("The name or the url of the file object doesn't seem to exist. The given name is:\n \"%s\""%str(self.__url))
+ else:
+ return False
+ #
+ # Directory related tests
+ # -----------------------
+ def is_local_dir(self):
+ if os.path.isdir(self.__url):
+ return True
+ else:
+ return False
+ def is_not_local_dir(self):
+ if not os.path.isdir(self.__url):
+ return True
+ else:
+ return False
+ def raise_error_if_not_local_dir(self):
+ if not os.path.isdir(self.__url):
+ raise ValueError("The name or the url of the directory object doesn't seem to exist. The given name is:\n \"%s\""%str(self.__url))
+ else:
+ return False
+ #
+ # Mime related functions
+ # ------------------------
+ def get_standard_mime(self):
+ (__mtype, __encoding) = mimetypes.guess_type(self.__url, strict=False)
+ return __mtype
+ def get_user_mime(self):
+ __fake = "fake."+self.__usr.lower()
+ (__mtype, __encoding) = mimetypes.guess_type(__fake, strict=False)
+ return __mtype
+ def get_comprehensive_mime(self):
+ if self.get_standard_mime() is not None:
+ return self.get_standard_mime()
+ elif self.get_user_mime() is not None:
+ return self.get_user_mime()
+ else:
+ return None
+ #
+ # Name related functions
+ # ----------------------
+ def get_user_name(self):
+ return self.__url
+ def get_absolute_name(self):
+ return os.path.abspath(os.path.realpath(self.__url))
+ def get_extension(self):
+ return self.__end
+
+class ImportFromFile(object):
+ """
+ Obtention de variables disrétisées en 1D, définies par une ou des variables
+ nommées, et sous la forme d'une série de points éventuellement indexés. La
+ lecture d'un fichier au format spécifié (ou intuité) permet de charger ces
+ fonctions depuis :
+ - des fichiers textes en colonnes de type TXT, CSV, TSV...
+ - des fichiers de données binaires NPY, NPZ, SDF...
+ La lecture du fichier complet ne se fait que si nécessaire, pour assurer la
+ performance tout en disposant de l'interprétation du contenu. Les fichiers
+ textes doivent présenter en première ligne (hors commentaire ou ligne vide)
+ les noms des variables de colonnes. Les commentaires commencent par un "#".
+ """
+ __slots__ = (
+ "_filename", "_colnames", "_colindex", "_varsline", "_format",
+ "_delimiter", "_skiprows", "__url", "__filestring", "__header",
+ "__allowvoid", "__binaryformats", "__supportedformats")
+ def __enter__(self): return self
+ def __exit__(self, exc_type, exc_val, exc_tb): return False
+ #
+ def __init__(self, Filename=None, ColNames=None, ColIndex=None, Format="Guess", AllowVoidNameList=True):
+ """
+ Verifie l'existence et les informations de définition du fichier. Les
+ noms de colonnes ou de variables sont ignorées si le format ne permet
+ pas de les indiquer.
+ Arguments :
+ - Filename : nom du fichier
+ - ColNames : noms de la ou des colonnes/variables à lire
+ - ColIndex : nom unique de la colonne/variable servant d'index
+ - Format : format du fichier et/ou des données inclues
+ - AllowVoidNameList : permet, si la liste de noms est vide, de
+ prendre par défaut toutes les colonnes
+ """
+ self.__binaryformats =(
+ "application/numpy.npy",
+ "application/numpy.npz",
+ "application/dymola.sdf",
+ )
+ self.__url = ImportDetector( Filename, Format)
+ self.__url.raise_error_if_not_local_file()
+ self._filename = self.__url.get_absolute_name()
+ PlatformInfo.checkFileNameConformity( self._filename )
+ #
+ self._format = self.__url.get_comprehensive_mime()
+ #
+ self.__header, self._varsline, self._skiprows = self.__getentete()
+ #
+ if self._format == "text/csv" or Format.upper() == "CSV":
+ self._format = "text/csv"
+ self.__filestring = "".join(self.__header)
+ if self.__filestring.count(",") > 1:
+ self._delimiter = ","
+ elif self.__filestring.count(";") > 1:
+ self._delimiter = ";"
+ else:
+ self._delimiter = None
+ elif self._format == "text/tab-separated-values" or Format.upper() == "TSV":
+ self._format = "text/tab-separated-values"
+ self._delimiter = "\t"
+ else:
+ self._delimiter = None
+ #
+ if ColNames is not None: self._colnames = tuple(ColNames)
+ else: self._colnames = None
+ #
+ if ColIndex is not None: self._colindex = str(ColIndex)
+ else: self._colindex = None
+ #
+ self.__allowvoid = bool(AllowVoidNameList)
+
+ def __getentete(self, __nblines = 3):
+ "Lit l'entête du fichier pour trouver la définition des variables"
+ # La première ligne non vide non commentée est toujours considérée
+ # porter les labels de colonne, donc pas des valeurs
+ __header, __varsline, __skiprows = [], "", 1
+ if self._format in self.__binaryformats:
+ pass
+ else:
+ with open(self._filename,'r') as fid:
+ __line = fid.readline().strip()
+ while "#" in __line or len(__line) < 1:
+ __header.append(__line)
+ __skiprows += 1
+ __line = fid.readline().strip()
+ __varsline = __line
+ for i in range(max(0,__nblines)):
+ __header.append(fid.readline())
+ return (__header, __varsline, __skiprows)
+
+ def __getindices(self, __colnames, __colindex, __delimiter=None ):
+ "Indices de colonnes correspondants à l'index et aux variables"
+ if __delimiter is None:
+ __varserie = self._varsline.strip('#').strip().split()
+ else:
+ __varserie = self._varsline.strip('#').strip().split(str(__delimiter))
+ #
+ if __colnames is not None:
+ __usecols = []
+ __colnames = tuple(__colnames)
+ for v in __colnames:
+ for i, n in enumerate(__varserie):
+ if v == n: __usecols.append(i)
+ __usecols = tuple(__usecols)
+ if len(__usecols) == 0:
+ if self.__allowvoid:
+ __usecols = None
+ else:
+ raise ValueError("Can not found any column corresponding to the required names %s"%(__colnames,))
+ else:
+ __usecols = None
+ #
+ if __colindex is not None:
+ __useindex = None
+ __colindex = str(__colindex)
+ for i, n in enumerate(__varserie):
+ if __colindex == n: __useindex = i
+ else:
+ __useindex = None
+ #
+ return (__usecols, __useindex)
+
+ def getsupported(self):
+ self.__supportedformats = {}
+ self.__supportedformats["text/plain"] = True
+ self.__supportedformats["text/csv"] = True
+ self.__supportedformats["text/tab-separated-values"] = True
+ self.__supportedformats["application/numpy.npy"] = True
+ self.__supportedformats["application/numpy.npz"] = True
+ self.__supportedformats["application/dymola.sdf"] = PlatformInfo.has_sdf
+ return self.__supportedformats
+
+ def getvalue(self, ColNames=None, ColIndex=None ):
+ "Renvoie la ou les variables demandees par la liste de leurs noms"
+ # Uniquement si mise à jour
+ if ColNames is not None: self._colnames = tuple(ColNames)
+ if ColIndex is not None: self._colindex = str(ColIndex)
+ #
+ __index = None
+ if self._format == "application/numpy.npy":
+ __columns = numpy.load(self._filename)
+ #
+ elif self._format == "application/numpy.npz":
+ __columns = None
+ with numpy.load(self._filename) as __allcolumns:
+ if self._colnames is None:
+ self._colnames = __allcolumns.files
+ for nom in self._colnames: # Si une variable demandée n'existe pas
+ if nom not in __allcolumns.files:
+ self._colnames = tuple( __allcolumns.files )
+ for nom in self._colnames:
+ if nom in __allcolumns.files:
+ if __columns is not None:
+ # Attention : toutes les variables doivent avoir la même taille
+ __columns = numpy.vstack((__columns, numpy.reshape(__allcolumns[nom], (1,-1))))
+ else:
+ # Première colonne
+ __columns = numpy.reshape(__allcolumns[nom], (1,-1))
+ if self._colindex is not None and self._colindex in __allcolumns.files:
+ __index = numpy.array(numpy.reshape(__allcolumns[self._colindex], (1,-1)), dtype=bytes)
+ elif self._format == "text/plain":
+ __usecols, __useindex = self.__getindices(self._colnames, self._colindex)
+ __columns = numpy.loadtxt(self._filename, usecols = __usecols, skiprows=self._skiprows)
+ if __useindex is not None:
+ __index = numpy.loadtxt(self._filename, dtype = bytes, usecols = (__useindex,), skiprows=self._skiprows)
+ if __usecols is None: # Si une variable demandée n'existe pas
+ self._colnames = None
+ #
+ elif self._format == "application/dymola.sdf" and PlatformInfo.has_sdf:
+ import sdf
+ __content = sdf.load(self._filename)
+ __columns = None
+ if self._colnames is None:
+ self._colnames = [__content.datasets[i].name for i in range(len(__content.datasets))]
+ for nom in self._colnames:
+ if nom in __content:
+ if __columns is not None:
+ # Attention : toutes les variables doivent avoir la même taille
+ __columns = numpy.vstack((__columns, numpy.reshape(__content[nom].data, (1,-1))))
+ else:
+ # Première colonne
+ __columns = numpy.reshape(__content[nom].data, (1,-1))
+ if self._colindex is not None and self._colindex in __content:
+ __index = __content[self._colindex].data
+ #
+ elif self._format == "text/csv":
+ __usecols, __useindex = self.__getindices(self._colnames, self._colindex, self._delimiter)
+ __columns = numpy.loadtxt(self._filename, usecols = __usecols, delimiter = self._delimiter, skiprows=self._skiprows)
+ if __useindex is not None:
+ __index = numpy.loadtxt(self._filename, dtype = bytes, usecols = (__useindex,), delimiter = self._delimiter, skiprows=self._skiprows)
+ if __usecols is None: # Si une variable demandée n'existe pas
+ self._colnames = None
+ #
+ elif self._format == "text/tab-separated-values":
+ __usecols, __useindex = self.__getindices(self._colnames, self._colindex, self._delimiter)
+ __columns = numpy.loadtxt(self._filename, usecols = __usecols, delimiter = self._delimiter, skiprows=self._skiprows)
+ if __useindex is not None:
+ __index = numpy.loadtxt(self._filename, dtype = bytes, usecols = (__useindex,), delimiter = self._delimiter, skiprows=self._skiprows)
+ if __usecols is None: # Si une variable demandée n'existe pas
+ self._colnames = None
+ else:
+ raise ValueError("Unkown file format \"%s\" or no reader available"%self._format)
+ if __columns is None: __columns = ()
+ #
+ def toString(value):
+ try:
+ return value.decode()
+ except ValueError:
+ return value
+ if __index is not None:
+ __index = tuple([toString(v) for v in __index])
+ #
+ return (self._colnames, __columns, self._colindex, __index)
+
+ def getstring(self):
+ "Renvoie le fichier texte complet"
+ if self._format in self.__binaryformats:
+ return ""
+ else:
+ with open(self._filename,'r') as fid:
+ return fid.read()
+
+ def getformat(self):
+ return self._format
+
+class ImportScalarLinesFromFile(ImportFromFile):
+ """
+ Importation de fichier contenant des variables scalaires nommées. Le
+ fichier comporte soit 2, soit 4 colonnes, obligatoirement nommées "Name",
+ "Value", "Minimum", "Maximum" si les noms sont précisés. Sur chaque ligne
+ est indiqué le nom, la valeur, et éventuelement deux bornes min et max (ou
+ None si nécessaire pour une borne).
+
+ Seule la méthode "getvalue" est changée.
+ """
+ def __enter__(self): return self
+ def __exit__(self, exc_type, exc_val, exc_tb): return False
+ #
+ def __init__(self, Filename=None, ColNames=None, ColIndex=None, Format="Guess"):
+ ImportFromFile.__init__(self, Filename, ColNames, ColIndex, Format)
+ if self._format not in ["text/plain", "text/csv", "text/tab-separated-values"]:
+ raise ValueError("Unkown file format \"%s\""%self._format)
+ #
+ def getvalue(self, VarNames = None, HeaderNames=()):
+ "Renvoie la ou les variables demandees par la liste de leurs noms"
+ if VarNames is not None: __varnames = tuple(VarNames)
+ else: __varnames = None
+ #
+ if "Name" in self._varsline and "Value" in self._varsline and "Minimum" in self._varsline and "Maximum" in self._varsline:
+ __ftype = "NamValMinMax"
+ __dtypes = {'names' : ('Name', 'Value', 'Minimum', 'Maximum'),
+ 'formats': ('S128', 'g', 'g', 'g')}
+ __usecols = (0, 1, 2, 3)
+ def __replaceNoneN( s ):
+ if s.strip() == b'None': return numpy.NINF
+ else: return s
+ def __replaceNoneP( s ):
+ if s.strip() == b'None': return numpy.PINF
+ else: return s
+ __converters = {2: __replaceNoneN, 3: __replaceNoneP}
+ elif "Name" in self._varsline and "Value" in self._varsline and ("Minimum" not in self._varsline or "Maximum" not in self._varsline):
+ __ftype = "NamVal"
+ __dtypes = {'names' : ('Name', 'Value'),
+ 'formats': ('S128', 'g')}
+ __converters = None
+ __usecols = (0, 1)
+ elif len(HeaderNames)>0 and numpy.all([kw in self._varsline for kw in HeaderNames]):
+ __ftype = "NamLotOfVals"
+ __dtypes = {'names' : HeaderNames,
+ 'formats': tuple(['S128',]+['g']*(len(HeaderNames)-1))}
+ __usecols = tuple(range(len(HeaderNames)))
+ def __replaceNone( s ):
+ if s.strip() == b'None': return numpy.NAN
+ else: return s
+ __converters = dict()
+ for i in range(1,len(HeaderNames)):
+ __converters[i] = __replaceNone
+ else:
+ raise ValueError("Can not find names of columns for initial values. Wrong first line is:\n \"%s\""%__firstline)
+ #
+ if self._format == "text/plain":
+ __content = numpy.loadtxt(self._filename, dtype = __dtypes, usecols = __usecols, skiprows = self._skiprows, converters = __converters)
+ elif self._format in ["text/csv", "text/tab-separated-values"]:
+ __content = numpy.loadtxt(self._filename, dtype = __dtypes, usecols = __usecols, skiprows = self._skiprows, converters = __converters, delimiter = self._delimiter)
+ else:
+ raise ValueError("Unkown file format \"%s\""%self._format)
+ #
+ __names, __thevalue, __bounds = [], [], []
+ for sub in __content:
+ if len(__usecols) == 4:
+ na, va, mi, ma = sub
+ if numpy.isneginf(mi): mi = None # Réattribue les variables None
+ elif numpy.isnan(mi): mi = None # Réattribue les variables None
+ if numpy.isposinf(ma): ma = None # Réattribue les variables None
+ elif numpy.isnan(ma): ma = None # Réattribue les variables None
+ elif len(__usecols) == 2 and __ftype == "NamVal":
+ na, va = sub
+ mi, ma = None, None
+ else:
+ nsub = list(sub)
+ na = sub[0]
+ for i, v in enumerate(nsub[1:]):
+ if numpy.isnan(v): nsub[i+1] = None
+ va = nsub[1:]
+ mi, ma = None, None
+ na = na.decode()
+ if (__varnames is None or na in __varnames) and (na not in __names):
+ # Ne stocke que la premiere occurence d'une variable
+ __names.append(na)
+ __thevalue.append(va)
+ __bounds.append((mi,ma))
+ #
+ __names = tuple(__names)
+ __thevalue = numpy.array(__thevalue)
+ __bounds = tuple(__bounds)
+ #
+ return (__names, __thevalue, __bounds)
+
+# ==============================================================================
+class EficasGUI(object):
+ """
+ Lancement autonome de l'interface EFICAS/ADAO
+ """
+ def __init__(self, __addpath = None):
+ # Chemin pour l'installation (ordre important)
+ self.__msg = ""
+ self.__path_settings_ok = False
+ #----------------
+ if "EFICAS_ROOT" in os.environ:
+ __EFICAS_ROOT = os.environ["EFICAS_ROOT"]
+ __path_ok = True
+ else:
+ self.__msg += "\nKeyError:\n"+\
+ " the required environment variable EFICAS_ROOT is unknown.\n"+\
+ " You have either to be in SALOME environment, or to set\n"+\
+ " this variable in your environment to the right path \"<...>\"\n"+\
+ " to find an installed EFICAS application. For example:\n"+\
+ " EFICAS_ROOT=\"<...>\" command\n"
+ __path_ok = False
+ try:
+ import adao
+ __path_ok = True and __path_ok
+ except ImportError:
+ self.__msg += "\nImportError:\n"+\
+ " the required ADAO library can not be found to be imported.\n"+\
+ " You have either to be in ADAO environment, or to be in SALOME\n"+\
+ " environment, or to set manually in your Python 3 environment the\n"+\
+ " right path \"<...>\" to find an installed ADAO application. For\n"+\
+ " example:\n"+\
+ " PYTHONPATH=\"<...>:${PYTHONPATH}\" command\n"
+ __path_ok = False
+ try:
+ import PyQt5
+ __path_ok = True and __path_ok
+ except ImportError:
+ self.__msg += "\nImportError:\n"+\
+ " the required PyQt5 library can not be found to be imported.\n"+\
+ " You have either to have a raisonable up-to-date Python 3\n"+\
+ " installation (less than 5 years), or to be in SALOME environment.\n"
+ __path_ok = False
+ #----------------
+ if not __path_ok:
+ self.__msg += "\nWarning:\n"+\
+ " It seems you have some troubles with your installation.\n"+\
+ " Be aware that some other errors may exist, that are not\n"+\
+ " explained as above, like some incomplete or obsolete\n"+\
+ " Python 3, or incomplete module installation.\n"+\
+ " \n"+\
+ " Please correct the above error(s) before launching the\n"+\
+ " standalone EFICAS/ADAO interface.\n"
+ logging.debug("Some of the ADAO/EFICAS/QT5 paths have not been found")
+ self.__path_settings_ok = False
+ else:
+ logging.debug("All the ADAO/EFICAS/QT5 paths have been found")
+ self.__path_settings_ok = True
+ #----------------
+ if self.__path_settings_ok:
+ sys.path.insert(0,__EFICAS_ROOT)
+ sys.path.insert(0,os.path.join(adao.adao_py_dir,"daEficas"))
+ if __addpath is not None and os.path.exists(os.path.abspath(__addpath)):
+ sys.path.insert(0,os.path.abspath(__addpath))
+ logging.debug("All the paths have been correctly set up")
+ else:
+ print(self.__msg)
+ logging.debug("Errors in path settings have been found")
+
+ def gui(self):
+ if self.__path_settings_ok:
+ logging.debug("Launching standalone EFICAS/ADAO interface...")
+ from daEficas import prefs
+ from InterfaceQT4 import eficas_go
+ eficas_go.lanceEficas(code=prefs.code)
+ else:
+ logging.debug("Can not launch standalone EFICAS/ADAO interface for path errors.")