Salome HOME
reindent + copyright + merge manuel avec la V9_dev sauf repertoires metier
[tools/eficas.git] / Noyau / N_utils.py
index 2b624a475ef6f6216339be17165ec3edc1cad1c3..5e1ba24a9004291cbfb991d1434e77c7d81fbebb 100644 (file)
@@ -1,5 +1,5 @@
 # coding=utf-8
-# Copyright (C) 2007-2013   EDF R&D
+# Copyright (C) 2007-2021   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
 """
 
 # Modules Python
+from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import division
+try :
+    from builtins import str
+    from builtins import object
+except :
+    pass
 import sys
 
 # Modules EFICAS
-from N_Exception import AsException
-from N_types import is_int, is_float, is_complex, is_str, is_sequence, is_assd
-from strfunc import get_encoding
+from .N_Exception import AsException
+from .N_types import isInt, isFloat, isComplex, isStr, isSequence, isASSD
+from .strfunc import getEncoding
 
 SEP = '_'
 
-try:
-    # Si la version de Python possède la fonction _getframe
-    # on l'utilise.
+try :
     cur_frame = sys._getframe
-except:
-    # Sinon on l'émule
-    def cur_frame(offset=0):
-        """ Retourne la frame d execution effective eventuellement en remontant
-            de offset niveaux dans la pile d execution
-            Si il y a moins de offset niveaux retourne None
-        """
-        try:
-            1 / 0
-        except:
-            frame = sys.exc_info()[2].tb_frame.f_back
-        while offset > 0:
-            if frame == None:
-                return None
-            frame = frame.f_back
-            offset = offset - 1
-        return frame
-
-
-def callee_where(niveau=4):
+except :
+    print ('pb avec la version de python pour cur_frame = sys._getframe')
+
+def calleeWhere(niveau=4):
     """
        recupere la position de l appel
     """
-    frame = cur_frame(niveau)
+    frame = sys._getframe(niveau)
     if frame == None:
         return 0, "inconnu", 0, {}
     try:
         # Python 2.7 compile function does not accept unicode filename, so we encode it
         # with the current locale encoding in order to have a correct traceback.
         # Here, we convert it back to unicode.
-        filename = unicode(frame.f_code.co_filename, get_encoding())
-        return frame.f_lineno, filename, frame.f_code.co_firstlineno, frame.f_locals
+        import six
+        filename = six.text_type(frame.f_code.co_filename, getEncoding())
+        return frame.fLineNo, filename, frame.f_code.co_firstlineno, frame.f_locals
     except:
         return 0, "inconnu", 0, {}
 
@@ -77,15 +68,15 @@ def AsType(a):
        Retourne le type d'un concept (a) à partir
        des caractéristiques de l'objet Python
     """
-    if is_sequence(a):
+    if isSequence(a):
         return AsType(a[0])
-    if is_assd(a):
+    if isASSD(a):
         return type(a)
-    if is_float(a):
+    if isFloat(a):
         return "R"
-    if is_int(a):
+    if isInt(a):
         return "I"
-    if is_str(a):
+    if isStr(a):
         return "TXM"
     if a == None:
         return None
@@ -93,9 +84,9 @@ def AsType(a):
 
 
 def prbanner(s):
-    print "*" * (len(s) + 10)
-    print "*" * 5 + s + "*" * 5
-    print "*" * (len(s) + 10)
+    print(("*" * (len(s) + 10)))
+    print(("*" * 5 + s + "*" * 5))
+    print(("*" * (len(s) + 10)))
 
 
 def repr_float(valeur):
@@ -167,7 +158,7 @@ def repr_float(valeur):
     return s
 
 
-def import_object(uri):
+def importObject(uri):
     """Load and return a python object (class, function...).
     Its `uri` looks like "mainpkg.subpkg.module.object", this means
     that "mainpkg.subpkg.module" is imported and "object" is
@@ -182,13 +173,13 @@ def import_object(uri):
     try:
         __import__(modname)
         mod = sys.modules[modname]
-    except ImportError, err:
+    except ImportError as err:
         raise ImportError(
-            u"can not import module : %s (%s)" % (modname, str(err)))
+            "can not import module : %s (%s)" % (modname, str(err)))
     try:
         object = getattr(mod, objname)
-    except AttributeError, err:
-        raise AttributeError(u"object (%s) not found in module '%s'. "
+    except AttributeError as err:
+        raise AttributeError("object (%s) not found in module '%s'. "
                              "Module content is: %s" % (objname, modname, tuple(dir(mod))))
     return object
 
@@ -224,8 +215,8 @@ class Enum(object):
 
     def exists(self, value):
         """Tell if value is in the enumeration"""
-        return self.get_id(value) is not None
+        return self.getId(value) is not None
 
-    def get_id(self, value):
+    def getId(self, value):
         """Return the key associated to the given value"""
         return self._dict_keys.get(value, None)