Salome HOME
updated copyright message
[modules/kernel.git] / src / KERNEL_PY / kernel / studyedit.py
index ccc2e06800dc1ef5f859bc00c8d70fcfe639cb86..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
@@ -33,115 +33,82 @@ import re
 
 from salome.kernel import termcolor
 from salome.kernel.logger import Logger
-import salome
 
 
 logger = Logger("salome.kernel.studyedit", color=termcolor.PURPLE)
 
-_editors = {}
 _DEFAULT_CONTAINER = "FactoryServer"
 
-## Return the ID of the active study. In GUI mode, this function is equivalent
-#  to salome.sg.getActiveStudyId(). Outside GUI, it returns <b> salome.myStudyId </b>
-#  variable.
-#  \ingroup studyedit
-def getActiveStudyId():
-    """
-    Return the ID of the active study. In GUI mode, this function is equivalent
-    to ``salome.sg.getActiveStudyId()``. Outside GUI, it returns
-    ``salome.myStudyId`` variable.
-    """
-    salome.salome_init()
-    # Warning: we don't use salome.getActiveStudy() here because it doesn't
-    # work properly when called from Salome modules (multi-study interpreter
-    # issue)
-    if salome.hasDesktop():
-        return salome.sg.getActiveStudyId()
-    else:
-        return salome.myStudyId
-
-def getActiveStudy():
-    return getStudyFromStudyId(getActiveStudyId())
-
-def getStudyFromStudyId(studyId):
-    salome.salome_init()
-    study = salome.myStudyManager.GetStudyByID(studyId)
-    return study
-
-def getStudyIdFromStudy(study):
-    studyId = study._get_StudyId()
-    return studyId
-
-## Return a \b StudyEditor instance to edit the study with ID studyId. 
-#  If \b studyId is \b None, return an editor for the current study.
-#  \ingroup studyedit
-def getStudyEditor(studyId = None):
-    """
-    Return a :class:`StudyEditor` instance to edit the study with ID
-    `studyId`. If `studyId` is :const:`None`, return an editor for the current
-    study.
-    """
-    if studyId is None:
-        studyId = getActiveStudyId()
-    if studyId not in _editors:
-        _editors[studyId] = StudyEditor(studyId)
-    return _editors[studyId]
 
 ## 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 parameter \b studyId defines the ID of the study to
-#  edit. If it is \em None, the edited study will be the current study.
+#  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 studyId This instance attribute contains the ID of the edited study. 
-#  This attribute should not be modified.
+#  \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:
     """
     This class provides utility methods to complement :class:`Study` and
     :class:`StudyBuilder` classes. Those methods may be moved in those classes
-    in the future. The parameter `studyId` defines the ID of the study to
-    edit. If it is :const:`None`, the edited study will be the current study.
+    in the future.
     The preferred way to get a StudyEditor object is through the method
     :meth:`getStudyEditor` which allows to reuse existing instances.
 
-    .. attribute:: studyId
+    .. attribute:: lcc
     
-       This instance attribute contains the ID of the edited study. This
-       attribute should not be modified.
+       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, studyId = None):
+
+    @property
+    def study(self):
+        """Attribute that provides access to the study."""
+        import salome
+        salome.salome_init()
+        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 studyId is None:
-            studyId = getActiveStudyId()
-        self.studyId = studyId
-        self.study = salome.myStudyManager.GetStudyByID(studyId)
-        if self.study is None:
-            raise Exception("Can't create StudyEditor object: "
-                            "Study %d doesn't exist" % studyId)
-        self.builder = self.study.NewBuilder()
+        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
@@ -220,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." %
@@ -243,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." %
@@ -494,7 +461,7 @@ class StudyEditor:
             else:
                 self.builder.RemoveObject(item)
             ok = True
-        except:
+        except Exception:
             ok = False
         return ok
 
@@ -653,3 +620,18 @@ class StudyEditor:
         """
         attr = self.builder.FindOrCreateAttribute(sObject, "AttributePixMap")
         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