Salome HOME
Allows extended keyword syntax settings for Parameters
[modules/adao.git] / src / daComposant / daCore / Interfaces.py
index 3c1f5d4b834f4fe4dff93ffb451714a3c0890945..2846d7bb01b6b2da7d32f9fbb5119afd2bb4415c 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2008-2018 EDF R&D
+# Copyright (C) 2008-2019 EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -118,11 +118,13 @@ class _TUIViewer(GenericCaseViewer):
             for k in __keys:
                 __v = __local[k]
                 if __v is None: continue
-                if   k == "Checked"  and not __v: continue
-                if   k == "Stored"   and not __v: continue
-                if   k == "ColMajor" and not __v: continue
-                if   k == "AvoidRC"  and __v: continue
-                if   k == "noDetails": continue
+                if   k == "Checked"              and not __v: continue
+                if   k == "Stored"               and not __v: continue
+                if   k == "ColMajor"             and not __v: continue
+                if   k == "InputFunctionAsMulti" and not __v: continue
+                if   k == "nextStep"             and not __v: continue
+                if   k == "AvoidRC"              and     __v: continue
+                if   k == "noDetails":                        continue
                 if isinstance(__v,Persistence.Persistence): __v = __v.values()
                 if callable(__v): __text = self._missing%__v.__name__+__text
                 if isinstance(__v,dict):
@@ -258,7 +260,7 @@ class _COMViewer(GenericCaseViewer):
                     __itype = r['INPUT_TYPE']
                     __idata = r['data']
                     if 'FROM' in __idata:
-                        # String, Script, Template, ScriptWithOneFunction, ScriptWithFunctions
+                        # String, Script, DataFile, Template, ScriptWithOneFunction, ScriptWithFunctions
                         __ifrom = __idata['FROM']
                         __idata.pop('FROM','')
                         if __ifrom == 'String' or __ifrom == 'Template':
@@ -266,6 +268,9 @@ class _COMViewer(GenericCaseViewer):
                         if __ifrom == 'Script':
                             __argumentsList.append([__itype,True])
                             __argumentsList.append(['Script',__idata['SCRIPT_FILE']])
+                        if __ifrom == 'DataFile':
+                            __argumentsList.append([__itype,True])
+                            __argumentsList.append(['DataFile',__idata['DATA_FILE']])
                         if __ifrom == 'ScriptWithOneFunction':
                             __argumentsList.append(['OneFunction',True])
                             __argumentsList.append(['Script',__idata.pop('SCRIPTWITHONEFUNCTION_FILE')])
@@ -356,9 +361,20 @@ class _SCDViewer(GenericCaseViewer):
                 __local.pop(__k)
             for __k,__v in __local.items():
                 if __k == "Concept": continue
-                if __k in ['ScalarSparseMatrix','DiagonalSparseMatrix','Matrix','OneFunction','ThreeFunctions'] and 'Script' in __local: continue
+                if __k in ['ScalarSparseMatrix','DiagonalSparseMatrix','Matrix','OneFunction','ThreeFunctions'] and 'Script' in __local and __local['Script'] is not None: continue
+                if __k in ['Vector','VectorSerie'] and 'DataFile' in __local and __local['DataFile'] is not None: continue
                 if __k == 'Algorithm':
                     __text += "study_config['Algorithm'] = %s\n"%(repr(__v))
+                elif __k == 'DataFile':
+                    __k = 'Vector'
+                    __f = 'DataFile'
+                    __v = "'"+repr(__v)+"'"
+                    for __lk in ['Vector','VectorSerie']:
+                        if __lk in __local and __local[__lk]: __k = __lk
+                    __text += "%s_config['Type'] = '%s'\n"%(__command,__k)
+                    __text += "%s_config['From'] = '%s'\n"%(__command,__f)
+                    __text += "%s_config['Data'] = %s\n"%(__command,__v)
+                    __text = __text.replace("''","'")
                 elif __k == 'Script':
                     __k = 'Vector'
                     __f = 'Script'
@@ -392,7 +408,7 @@ class _SCDViewer(GenericCaseViewer):
                     __text += "%s_config['From'] = '%s'\n"%(__command,__f)
                     __text += "%s_config['Data'] = %s\n"%(__command,__v)
                     __text = __text.replace("''","'")
-                elif __k in ('Stored', 'Checked', 'ColMajor'):
+                elif __k in ('Stored', 'Checked', 'ColMajor', 'InputFunctionAsMulti', 'nextStep'):
                     if bool(__v):
                         __text += "%s_config['%s'] = '%s'\n"%(__command,__k,int(bool(__v)))
                 elif __k in ('AvoidRC', 'noDetails'):
@@ -515,6 +531,7 @@ class ImportFromScript(object):
             __basename = os.path.basename(__filename).rstrip(".py")
         else:
             __basename = __filename.rstrip(".py")
+        PlatformInfo.checkFileNameImportability( __basename+".py" )
         self.__basename = __basename
         self.__filenspace = __import__(__basename, globals(), locals(), [])
         self.__filestring = open(__filename,'r').read()
@@ -560,12 +577,13 @@ class ImportDetector(object):
         #
         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 f
+    # File related tests
     # ------------------
     def is_local_file(self):
         if os.path.isfile(os.path.realpath(self.__url)):
@@ -582,6 +600,7 @@ class ImportDetector(object):
             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):
@@ -599,6 +618,7 @@ class ImportDetector(object):
             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):
@@ -615,6 +635,7 @@ class ImportDetector(object):
             return self.get_user_mime()
         else:
             return None
+    #
     # Name related functions
     # ----------------------
     def get_user_name(self):
@@ -632,7 +653,7 @@ class ImportFromFile(object):
     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...
+        - 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)
@@ -641,7 +662,7 @@ class ImportFromFile(object):
     __slots__ = (
         "_filename", "_colnames", "_colindex", "_varsline", "_format",
         "_delimiter", "_skiprows", "__url", "__filestring", "__header",
-        "__allowvoid")
+        "__allowvoid", "__binaryformats", "__supportedformats")
     def __enter__(self): return self
     def __exit__(self, exc_type, exc_val, exc_tb): return False
     #
@@ -658,9 +679,15 @@ class ImportFromFile(object):
             - 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()
         #
@@ -673,6 +700,8 @@ class ImportFromFile(object):
                 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"
@@ -689,8 +718,10 @@ class ImportFromFile(object):
 
     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 ("application/numpy.npy", "application/numpy.npz"):
+        if self._format in self.__binaryformats:
             pass
         else:
             with open(self._filename,'r') as fid:
@@ -699,7 +730,7 @@ class ImportFromFile(object):
                     __header.append(__line)
                     __skiprows += 1
                     __line = fid.readline().strip()
-                __varsline = __line # Première ligne non commentée non vide
+                __varsline = __line
                 for i in range(max(0,__nblines)):
                     __header.append(fid.readline())
         return (__header, __varsline, __skiprows)
@@ -736,6 +767,16 @@ class ImportFromFile(object):
         #
         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
@@ -745,11 +786,15 @@ class ImportFromFile(object):
         __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:
@@ -765,20 +810,43 @@ class ImportFromFile(object):
             __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\""%self._format)
+            raise ValueError("Unkown file format \"%s\" or no reader available"%self._format)
         if __columns is None: __columns = ()
         #
         def toString(value):
@@ -792,9 +860,12 @@ class ImportFromFile(object):
         return (self._colnames, __columns, self._colindex, __index)
 
     def getstring(self):
-        "Renvoie le fichier complet"
-        with open(self._filename,'r') as fid:
-            return fid.read()
+        "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
@@ -862,7 +933,7 @@ class ImportScalarLinesFromFile(ImportFromFile):
         else:
             raise ValueError("Unkown file format \"%s\""%self._format)
         #
-        __names, __background, __bounds = [], [], []
+        __names, __thevalue, __bounds = [], [], []
         for sub in __content:
             if len(__usecols) == 4:
                 na, va, mi, ma = sub
@@ -884,15 +955,15 @@ class ImportScalarLinesFromFile(ImportFromFile):
             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)
-                __background.append(va)
+                __thevalue.append(va)
                 __bounds.append((mi,ma))
         #
         __names      = tuple(__names)
-        __background = numpy.array(__background)
+        __thevalue = numpy.array(__thevalue)
         __bounds     = tuple(__bounds)
         #
-        return (__names, __background, __bounds)
+        return (__names, __thevalue, __bounds)
 
 # ==============================================================================
 if __name__ == "__main__":
-    print('\n AUTODIAGNOSTIC \n')
+    print('\n AUTODIAGNOSTIC\n')