Salome HOME
Merge Python 3 porting.
[modules/gui.git] / src / SalomeApp / salome_pluginsmanager.py
index ba0dd119a8964f122df8a75841beeb608aef4ad6..c6bc59f1ecb0cc5207aa4df567daf3709a9c0ad6 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -77,7 +77,6 @@ In short to add a plugin:
 context attributes:
 
   - sg : the SALOME Swig interface
-  - studyId : the SALOME studyId that must be used to execute the plugin
   - study : the SALOME study object that must be used to execute the plugin
 
 """
@@ -103,14 +102,14 @@ plugins={}
 current_plugins_manager=None
 
 def initialize(module,name,basemenuname,menuname):
-  if not plugins.has_key(name):
+  if name not in plugins:
     if module:
       plugins[name]={}
     else:
       plugins[name]=[]
   if module:
     d=sgPyQt.getDesktop()
-    if plugins[name].has_key(d):return
+    if d in plugins[name]:return
     plugins[name][d]=PluginsManager(module,name,basemenuname,menuname)
   else:
     plugins[name].append(PluginsManager(module,name,basemenuname,menuname))
@@ -118,8 +117,7 @@ def initialize(module,name,basemenuname,menuname):
 class Context:
     def __init__(self,sgpyqt):
         self.sg=sgpyqt
-        self.studyId=salome.sg.getActiveStudyId()
-        self.study= salome.myStudyManager.GetStudyByID(self.studyId)
+        self.study=salome.myStudy
 
 def find_menu(smenu):
   lmenus=smenu.split("|")
@@ -149,8 +147,8 @@ logger=Logger("PluginsManager") #,color=GREEN)
 class PluginsManager:
     def __init__(self,module,name,basemenuname,menuname):
         self.name=name
-        self.basemenuname=unicode(basemenuname)
-        self.menuname=unicode(menuname)
+        self.basemenuname=basemenuname
+        self.menuname=menuname
         self.module=module
         self.registry={}
         self.handlers={}
@@ -164,13 +162,16 @@ class PluginsManager:
         # MODULES plugins are supposed to be located in the
         # installation folder of the module, in the subdirectory
         # "share/salome/plugins". We first look for these directories.
+        searched = []
         for key in os.environ.keys():
           if key.endswith("_ROOT_DIR"):
             rootpath=os.environ[key]
             dirpath=os.path.join(rootpath,PLUGIN_PATH_PATTERN)
-            if os.path.isdir(dirpath) and dirpath not in self.plugindirs:
+            if os.path.isdir(dirpath) and dirpath not in self.plugindirs + searched:
               logger.debug("Looking for plugins in the directory %s ..."%dirpath)
               walktree(dirpath,self.analyseFile)
+              if dirpath not in self.plugindirs and dirpath not in searched:
+                searched.append(dirpath)
 
         # USER plugins directory
         user_dir = os.path.expanduser("~/.config/salome/Plugins")
@@ -239,12 +240,7 @@ class PluginsManager:
 
     def importPlugins(self):
         """Execute the salome_plugins file that contains plugins definition """
-        studyId=sg.getActiveStudyId()
-        if studyId == 0:
-          self.menu.clear()
-          self.menu.menuAction().setVisible(False)
-          return
-        elif self.lasttime ==0 or salome.myStudy == None:
+        if self.lasttime ==0 or salome.myStudy == None:
           salome.salome_init(embedded=1)
 
         lasttime=0
@@ -281,7 +277,7 @@ class PluginsManager:
               sys.path.insert(0,directory)
               logger.debug("The directory %s has been added to PYTHONPATH"%directory)
             try:
-              execfile(plugins_file,globals(),{})
+              exec(compile(open(plugins_file).read(), plugins_file, 'exec'),globals(),{})
             except:
               logger.fatal("Error while loading plugins from file %s"%plugins_file)
               traceback.print_exc()
@@ -305,7 +301,7 @@ class PluginsManager:
                 submenus[str(menu.title())]=menu
             while len(names) > 1:
               name=names.pop(0)
-              if submenus.has_key(name):
+              if name in submenus:
                 amenu=submenus[name]
               else:
                 amenu=QMenu(name,parentMenu)