Salome HOME
updated copyright message
[modules/kernel.git] / src / KERNEL_PY / kernel / studyedit.py
index 98aad628287c203f0d0cff3b1b22bfd0c9d582bb..ad6cb485553bde361ecfc53e09ba090dae06b6c8 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -31,41 +31,32 @@ This module provides a new class :class:`StudyEditor` to complement
 
 import re
 
-import salome
-from salome.kernel.logger import Logger
 from salome.kernel import termcolor
-logger = Logger("salome.kernel.studyedit", color = termcolor.PURPLE)
+from salome.kernel.logger import Logger
 
-_editor = None
-_DEFAULT_CONTAINER = "FactoryServer"
 
-# The codec to use for strings that are displayed in Salome study tree is Latin-1
-ENCODING_FOR_SALOME_STUDY = "iso-8859-1"
+logger = Logger("salome.kernel.studyedit", color=termcolor.PURPLE)
+
+_DEFAULT_CONTAINER = "FactoryServer"
 
-## Return a \b StudyEditor instance to edit the study. 
-#  \ingroup studyedit
-def getStudyEditor():
-    """
-    Return a :class:`StudyEditor` instance to edit the study.
-    """
-    global _editor
-    if _editor is None:
-        _editor = StudyEditor()
-    return _editor
 
 ## This class provides utility methods to complement \b Study and
 #  \b StudyBuilder classes. Those methods may be moved in those classes
 #  in the future.
 #  The preferred way to get a StudyEditor object is through the method
-#  \b getStudyEditor which allows to reuse existing instances.
+#  \b getStudyEditor which allows to reuse existing instance or through
+#  the global module attribute \b EDITOR.
+#
+#  \param lcc This instance attribute provides access to the SALOME life cycle
+#  CORBA service.
 #
-#  \param study This instance attribute contains the underlying \b Study object.
-#  It can be used to access the study but the attribute itself should not
+#  \param study This instance attribute provides access to the \b Study object.
+#  It can be used to access the study but the attribute itself cannot
 #  be modified.
 #
-#  \param builder This instance attribute contains the underlying \b StudyBuilder
+#  \param builder This instance attribute provides access to the \b StudyBuilder
 #  object. It can be used to edit the study but the attribute itself
-#  should not be modified.
+#  cannot be modified.
 #  \ingroup studyedit
 class StudyEditor:
     """
@@ -75,26 +66,49 @@ class StudyEditor:
     The preferred way to get a StudyEditor object is through the method
     :meth:`getStudyEditor` which allows to reuse existing instances.
 
+    .. attribute:: lcc
+    
+       This instance attribute provides access to the SALOME life cycle
+       CORBA service.
+
     .. attribute:: study
     
-       This instance attribute contains the underlying :class:`Study` object.
-       It can be used to access the study but the attribute itself should not
+       This instance attribute provides access to the :class:`Study` object.
+       It can be used to access the study but the attribute itself cannot
        be modified.
 
     .. attribute:: builder
 
-       This instance attribute contains the underlying :class:`StudyBuilder`
+       This instance attribute provides access to the :class:`StudyBuilder`
        object. It can be used to edit the study but the attribute itself
-       should not be modified.
+       cannot be modified.
 
     """
-    def __init__(self):
+
+    @property
+    def study(self):
+        """Attribute that provides access to the study."""
+        import salome
         salome.salome_init()
-        self.study = salome.myStudy
-        if self.study is None:
-            raise Exception("Can't create StudyEditor object: "
-                            "Study doesn't exist")
-        self.builder = self.study.NewBuilder()
+        if salome.myStudy is None:
+            raise Exception("Study doesn't exist")
+        return salome.myStudy
+
+    @property
+    def builder(self):
+        """Attribute that provides access to the study builder."""
+        import salome
+        salome.salome_init()
+        if salome.myStudy is None:
+            raise Exception("Study doesn't exist")
+        return salome.myStudy.NewBuilder()
+
+    @property
+    def lcc(self):
+        """Attribute that providess access to the SALOME life cycle CORBA service."""
+        import salome
+        salome.salome_init()
+        return salome.lcc
 
     ## Find a component corresponding to the Salome module \b moduleName in
     #  the study. If none is found, create a new component and associate it
@@ -173,7 +187,7 @@ class StudyEditor:
             #attr = self.builder.FindOrCreateAttribute( sComponent, "AttributeParameter" )
             #attr.SetString( "ENGINE_NAME", engineName )
 
-            engine = salome.lcc.FindOrLoadComponent(containerName, moduleName)
+            engine = self.lcc.FindOrLoadComponent(containerName, moduleName)
             if engine is None:
                 raise Exception("Cannot load engine %s in container %s. See "
                                 "logs of container %s for more details." %
@@ -196,8 +210,8 @@ class StudyEditor:
         # engine name will be stored separately from the module name.
         #attr = self.builder.FindOrCreateAttribute( sComponent, "AttributeParameter" )
         #engineName = attr.GetString( "ENGINE_NAME" )
-        engine = salome.lcc.FindOrLoadComponent(containerName,
-                                                sComponent.GetComment())
+        engine = self.lcc.FindOrLoadComponent(containerName,
+                                              sComponent.GetComment())
         if engine is None:
             raise Exception("Cannot load component %s in container %s. See "
                             "logs of container %s for more details." %
@@ -291,19 +305,19 @@ class StudyEditor:
     #  \return new SObject created in the study.
     #
     #  See \b setItem() for the description of the other parameters.
-    def createItem(self, fatherItem, name, fileType = None, fileName = None,
-                   comment = None, icon = None, IOR = None, typeId = None):
+    def createItem(self, fatherItem, name, fileType=None, fileName=None,
+                   comment=None, icon=None, IOR=None, typeId=None):
         """
         Create a new object named `name` under `fatherItem` in the study, with
         the given attributes. If an object named `name` already exists under
         the father object, the new object is created with a new name `name_X`
         where X is the first available index.
-        
+
         :type  fatherItem: SObject
         :param fatherItem: item under which the new item will be added.
-                
+
         :return: new SObject created in the study
-        
+
         See :meth:`setItem` for the description of the other parameters.
         """
         aSObject = self.builder.NewObject(fatherItem)
@@ -318,11 +332,10 @@ class StudyEditor:
             aSObj = aChildIterator.Value()
             aChildIterator.Next()
             aName = aSObj.GetName()
-            if re.match(aNameRE,aName):
+            if re.match(aNameRE, aName):
                 aTmp = aName[aLength:]
-                if re.match(anIdRE,aTmp):
-                    import string
-                    anId = string.atol(aTmp[1:])
+                if re.match(anIdRE, aTmp):
+                    anId = int(aTmp[1:])
                     if aMaxId < anId:
                         aMaxId = anId
                         pass
@@ -332,16 +345,16 @@ class StudyEditor:
                     pass
                 pass
             pass
-        
+
         aMaxId = aMaxId + 1
         aName = name
         if aMaxId > 0:
             aName = aName + aDelim + str(aMaxId)
             pass
-        
+
         self.setItem(aSObject, aName, fileType, fileName, comment, icon,
                      IOR, typeId)
-    
+
         return aSObject
 
     ## Modify the attributes of an item in the study. Unspecified attributes
@@ -448,7 +461,7 @@ class StudyEditor:
             else:
                 self.builder.RemoveObject(item)
             ok = True
-        except:
+        except Exception:
             ok = False
         return ok
 
@@ -492,21 +505,19 @@ class StudyEditor:
 
     ## Return the name of the object sObject
     def getName(self, sObject):
-        val = sObject.GetName()
-        return unicode(val, ENCODING_FOR_SALOME_STUDY)
+        return sObject.GetName()
 
     ## Set the name of the object sObject
     def setName(self, sObject, name):
-        self.builder.SetName(sObject, name.encode(ENCODING_FOR_SALOME_STUDY))
+        self.builder.SetName(sObject, name)
 
     ## Return the comment of the object sObject
     def getComment(self, sObject):
-        val = sObject.GetComment()
-        return unicode(val, ENCODING_FOR_SALOME_STUDY)
+        return sObject.GetComment()
 
     ## Set the comment of the object sObject
     def setComment(self, sObject, comment):
-        self.builder.SetComment(sObject, comment.encode(ENCODING_FOR_SALOME_STUDY))
+        self.builder.SetComment(sObject, comment)
 
     ## Return the value of the attribute named \b attributeName on the object
     #  sObject, or \b default if the attribute doesn't exist.
@@ -556,8 +567,7 @@ class StudyEditor:
         Return the value of the attribute "AttributeFileType" of the object
         `sObject`, or an empty string if it is not set.
         """
-        val = self.getAttributeValue(sObject, "AttributeFileType", "")
-        return unicode(val, ENCODING_FOR_SALOME_STUDY)
+        return self.getAttributeValue(sObject, "AttributeFileType", "")
 
     ## Set the attribute "AttributeFileType" of the object sObject to the
     #  value value.
@@ -567,7 +577,7 @@ class StudyEditor:
         value `value`.
         """
         self.setAttributeValue(sObject, "AttributeFileType",
-                               value.encode(ENCODING_FOR_SALOME_STUDY))
+                               value)
 
     ## Return the value of the attribute "AttributeExternalFileDef" of the
     #  object sObject, or an empty string if it is not set.
@@ -576,8 +586,7 @@ class StudyEditor:
         Return the value of the attribute "AttributeExternalFileDef" of the
         object `sObject`, or an empty string if it is not set.
         """
-        val = self.getAttributeValue(sObject, "AttributeExternalFileDef", "")
-        return unicode(val, ENCODING_FOR_SALOME_STUDY)
+        return self.getAttributeValue(sObject, "AttributeExternalFileDef", "")
 
     ## Set the attribute "AttributeExternalFileDef" of the object sObject
     #  to the value value.
@@ -587,7 +596,7 @@ class StudyEditor:
         to the value `value`.
         """
         self.setAttributeValue(sObject, "AttributeExternalFileDef",
-                               value.encode(ENCODING_FOR_SALOME_STUDY))
+                               value)
 
     ## Return the value of the attribute "AttributePixMap" of the object
     #  sObject, or an empty string if it is not set.
@@ -600,7 +609,7 @@ class StudyEditor:
         found, attr = self.builder.FindAttribute(sObject, "AttributePixMap")
         if found and attr.HasPixMap():
             value = attr.GetPixMap()
-        return unicode(value, ENCODING_FOR_SALOME_STUDY)
+        return value
 
     ## Set the attribute "AttributePixMap" of the object sObject to the
     #  value value.
@@ -610,4 +619,19 @@ class StudyEditor:
         value `value`.
         """
         attr = self.builder.FindOrCreateAttribute(sObject, "AttributePixMap")
-        attr.SetPixMap(value.encode(ENCODING_FOR_SALOME_STUDY))
+        attr.SetPixMap(value)
+
+## Singleton study editor instance.
+#  \ingroup studyedit
+EDITOR = StudyEditor()
+"""Singleton study editor instance."""
+
+## Return a \b StudyEditor instance to edit the study. 
+#  \deprecated This function is kept for backward compatibility. Use \a EDITOR instead.
+#  \ingroup studyedit
+def getStudyEditor():
+    """
+    Return a :class:`StudyEditor` instance to edit the study.
+    """
+    global EDITOR
+    return EDITOR