# -*- 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
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):
__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'):
__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()
#
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)):
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):
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):
return self.get_user_mime()
else:
return None
+ #
# Name related functions
# ----------------------
def get_user_name(self):
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)
__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
#
- 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()
#
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:
#
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
__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 __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)
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):
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
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
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')