Salome HOME
Minor print update
[modules/adao.git] / src / daComposant / daCore / Interfaces.py
index 3c1f5d4b834f4fe4dff93ffb451714a3c0890945..8dfc2b83d2e941cb4d2fe989610df48a37c0cd76 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,12 @@ 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 == "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):
@@ -392,7 +393,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'):
                     if bool(__v):
                         __text += "%s_config['%s'] = '%s'\n"%(__command,__k,int(bool(__v)))
                 elif __k in ('AvoidRC', 'noDetails'):
@@ -515,6 +516,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 +562,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 +585,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 +603,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 +620,7 @@ class ImportDetector(object):
             return self.get_user_mime()
         else:
             return None
+    #
     # Name related functions
     # ----------------------
     def get_user_name(self):
@@ -632,7 +638,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 +647,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 +664,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()
         #
@@ -690,7 +702,7 @@ class ImportFromFile(object):
     def __getentete(self, __nblines = 3):
         "Lit l'entête du fichier pour trouver la définition des variables"
         __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:
@@ -736,6 +748,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,6 +767,7 @@ 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:
@@ -766,6 +789,23 @@ class ImportFromFile(object):
             if __useindex is not None:
                 __index = numpy.loadtxt(self._filename, dtype = bytes, usecols = (__useindex,), skiprows=self._skiprows)
         #
+        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)
@@ -778,7 +818,7 @@ class ImportFromFile(object):
             if __useindex is not None:
                 __index = numpy.loadtxt(self._filename, dtype = bytes, usecols = (__useindex,), delimiter = self._delimiter, skiprows=self._skiprows)
         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 +832,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 +905,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 +927,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')